Skip to content

Commit

Permalink
now configuring root logger globally in task.py and making it output …
Browse files Browse the repository at this point in the history
…different levels of stuff to the console and to the output file
  • Loading branch information
wangpatrick57 committed Oct 19, 2024
1 parent 5f49d10 commit 64608e3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
4 changes: 1 addition & 3 deletions scripts/pat_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ INTENDED_DBDATA_HARDWARE=ssd
. ./experiments/load_per_machine_envvars.sh

# space for testing. uncomment this to run individual commands from the script (copy pasting is harder because there are envvars)
python3 task.py tune protox agent hpo tpch --scale-factor $SCALE_FACTOR --num-samples 2 --max-concurrent 2 --workload-timeout 15 --query-timeout 1 --tune-duration-during-hpo 0.01 --intended-dbdata-hardware $INTENDED_DBDATA_HARDWARE --dbdata-parent-dpath $DBDATA_PARENT_DPATH --build-space-good-for-boot
python3 task.py tune protox agent tune tpch --scale-factor $SCALE_FACTOR --tune-duration-during-tune 0.02
python3 task.py tune protox agent replay tpch --scale-factor $SCALE_FACTOR
python3 task.py benchmark tpch data $SCALE_FACTOR
exit 0

# benchmark
Expand Down
29 changes: 24 additions & 5 deletions task.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,33 @@
def task(ctx: click.Context) -> None:
"""💩💩💩 CMU-DB Database Gym: github.com/cmu-db/dbgym 💩💩💩"""
dbgym_config_path = Path(os.getenv("DBGYM_CONFIG_PATH", "dbgym_config.yaml"))
ctx.obj = DBGymConfig(dbgym_config_path)
dbgym_cfg = DBGymConfig(dbgym_config_path)
ctx.obj = dbgym_cfg

# The root logger is set up globally here. Do not reconfigure the root logger anywhere else.
_set_up_root_logger(dbgym_cfg)

if __name__ == "__main__":
logging.basicConfig(
format="%(asctime)s:%(name)s:%(levelname)s - %(message)s", level=logging.INFO
)

def _set_up_root_logger(dbgym_cfg: DBGymConfig) -> None:
format = "%(levelname)s:%(asctime)s [%(filename)s:%(lineno)s] %(message)s"

# Set this so that the root logger captures everything.
logging.getLogger().setLevel(logging.DEBUG)

# Only make it output warnings or higher to the console.
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.WARNING)
console_handler.setFormatter(logging.Formatter(format))
logging.getLogger().addHandler(console_handler)

# Let it output everything to the output file.
file_handler = logging.FileHandler(dbgym_cfg.cur_task_runs_artifacts_path(mkdir=True) / "output.log")
file_handler.setFormatter(logging.Formatter(format))
file_handler.setLevel(logging.DEBUG)
logging.getLogger().addHandler(file_handler)


if __name__ == "__main__":
task.add_command(benchmark_group)
task.add_command(manage_group)
task.add_command(dbms_group)
Expand Down
19 changes: 4 additions & 15 deletions tune/protox/env/artifact_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@ class ArtifactManager(object):
"""
This class manages the following artifacts of Proto-X: info for replaying and TensorBoard output.
Initializing this class sets up the root logger. However, to use the root logger, you should
directly use the logging library.
Note that the root logger is set up globally inside the upper-most task.py file. Do not reconfigure it here.
"""

# The output log is the file that the root logger writes to
OUTPUT_LOG_FNAME = "output.log"
REPLAY_INFO_LOG_FNAME = "replay_info.log"
REPLAY_LOGGER_NAME = "replay_logger"

Expand All @@ -82,22 +80,13 @@ def __init__(
self.tuning_steps_dpath = self.log_dpath / "tuning_steps"
self.tuning_steps_dpath.mkdir(parents=True, exist_ok=True)

# Setup the root and replay loggers
formatter = "%(levelname)s:%(asctime)s [%(filename)s:%(lineno)s] %(message)s"
logging.basicConfig(format=formatter, level=logging.DEBUG, force=True)
output_log_handler = logging.FileHandler(
self.log_dpath / ArtifactManager.OUTPUT_LOG_FNAME
)
output_log_handler.setFormatter(logging.Formatter(formatter))
output_log_handler.setLevel(logging.DEBUG)
logging.getLogger().addHandler(output_log_handler)

# Create replay logger
replay_info_log_handler = logging.FileHandler(
self.tuning_steps_dpath / ArtifactManager.REPLAY_INFO_LOG_FNAME
)
replay_info_log_handler.setFormatter(logging.Formatter(formatter))
replay_info_log_handler.setFormatter(logging.Formatter("%(levelname)s:%(asctime)s [%(filename)s:%(lineno)s] %(message)s"))
replay_info_log_handler.setLevel(logging.INFO)
logging.getLogger(ArtifactManager.REPLAY_LOGGER_NAME)
logging.getLogger(ArtifactManager.REPLAY_LOGGER_NAME).addHandler(replay_info_log_handler)

# Setup the writer.
self.writer: Union[SummaryWriter, None] = None
Expand Down

0 comments on commit 64608e3

Please sign in to comment.