Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 30 additions & 40 deletions disruption_py/settings/log_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,20 @@ class LogSettings:

Attributes
----------
log_file_path : str, optional
file_path : str, optional
Path to the log file. If None, no log file will be created.
By default, a log file will be created in a temporary folder.
file_log_level : str
file_level : str
Logging level for the log file (default is "DEBUG").
Possible values are:
"TRACE", "DEBUG", "VERBOSE" (custom), "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL".
See: https://loguru.readthedocs.io/en/stable/api/logger.html#levels
log_file_write_mode : str, optional
The write mode for the log file. Default is "w".
log_to_console : bool
Whether to log messages to the console (default is True).
console_log_level : str or int, optional
console_level : str or int, optional
The log level for the console. Default is None, so log level will be determined
dynamically based on the number of shots.
Possible values are:
"TRACE", "DEBUG", "VERBOSE" (custom), "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL".
See: https://loguru.readthedocs.io/en/stable/api/logger.html#levels
use_custom_logging : bool
Whether to use custom logging. If set to true, no logging setup will be done.
Default is False.
warning_threshold : int
If number of shots is greater than this threshold, the console log level will
be "WARNING". Default is 1000.
Expand All @@ -65,14 +58,9 @@ class LogSettings:
Internal flag to prevent multiple setups (default is False).
"""

log_file_path: str = os.path.join(get_temporary_folder(), "output.log")
file_log_level: str = "DEBUG"
log_file_write_mode: str = "w"

log_to_console: bool = True
console_log_level: str = None

use_custom_logging: bool = False
file_path: str = os.path.join(get_temporary_folder(), "output.log")
file_level: str = "DEBUG"
console_level: str = None

warning_threshold: int = 1000
success_threshold: int = 500
Expand All @@ -97,7 +85,7 @@ def reset_handlers(self, num_shots: int = None):
console_format = "{time:HH:mm:ss.SSS} " + message_format
file_format = "{time:YYYY-MM-DD HH:mm:ss.SSS} " + message_format

if self.console_log_level is None:
if self.console_level is None:
# Determine console log level dynamically based on the number of shots
console_level = "VERBOSE"
if num_shots and num_shots > self.warning_threshold:
Expand All @@ -106,30 +94,29 @@ def reset_handlers(self, num_shots: int = None):
console_level = "SUCCESS"
elif num_shots and num_shots > self.info_threshold:
console_level = "INFO"
elif isinstance(self.console_log_level, str):
console_level = self.console_log_level.upper()
elif isinstance(self.console_level, str):
console_level = self.console_level.upper()
else:
console_level = self.console_log_level
console_level = self.console_level

# Add console handler
if self.log_to_console:
logger.add(
lambda msg: tqdm.write(msg, end=""),
level=console_level,
format=console_format,
colorize=True,
enqueue=True,
backtrace=False,
diagnose=True,
)
logger.add(
lambda msg: tqdm.write(msg, end=""),
level=console_level,
format=console_format,
colorize=True,
enqueue=True,
backtrace=False,
diagnose=True,
)

# Add file handler if log file path is provided
if self.log_file_path is not None:
if self.file_path is not None:
logger.add(
self.log_file_path,
level=self.file_log_level,
self.file_path,
level=self.file_level,
format=file_format,
mode=self.log_file_write_mode,
mode="w",
enqueue=True,
backtrace=False,
diagnose=True,
Expand All @@ -139,7 +126,7 @@ def setup_logging(self):
"""
Set up logging with custom styles and levels.
"""
if self.use_custom_logging or self._logging_has_been_setup:
if self._logging_has_been_setup:
return

# Set custom style and add a VERBOSE level. This only needs to be done
Expand Down Expand Up @@ -174,8 +161,8 @@ def setup_logging(self):
u=os.getenv("USER"),
h=os.uname().nodename,
)
if self.log_file_path is not None:
logger.info("Logging: {l}", l=self.log_file_path)
if self.file_path is not None:
logger.info("Logging: {l}", l=self.file_path)
logger.debug(
"Repository: {url}{append}{commit}",
url="https://github.com/MIT-PSFC/disruption-py",
Expand Down Expand Up @@ -208,7 +195,10 @@ def resolve_log_settings(
return log_settings

if isinstance(log_settings, (str, int)):
return LogSettings(console_log_level=log_settings)
return LogSettings(console_level=log_settings)

if isinstance(log_settings, dict):
return LogSettings(**log_settings)

if log_settings is None:
return LogSettings()
Expand Down
2 changes: 1 addition & 1 deletion disruption_py/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def get_shots_data(
return None

# Dynamically set the console log level based on the number of shots
if log_settings.console_log_level is None:
if log_settings.console_level is None:
log_settings.reset_handlers(num_shots=len(shotlist_list))

# log start
Expand Down
9 changes: 3 additions & 6 deletions examples/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@
num_processes=1,
log_settings=LogSettings(
# default None: "output.log" in temporary session folder
log_file_path=None,
file_log_level="DEBUG",
log_file_write_mode="w",
log_to_console=True,
file_path=None,
file_level="DEBUG",
# default None: VERBOSE, or higher based on number of shots
console_log_level=None,
use_custom_logging=False,
console_level=None,
),
)
8 changes: 3 additions & 5 deletions tests/utils/eval_against_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ def get_fresh_data(
retrieval_settings=retrieval_settings,
output_setting="dict",
log_settings=LogSettings(
log_to_console=True,
log_file_path=log_file_path,
log_file_write_mode="w",
file_log_level="DEBUG",
console_log_level=console_log_level,
file_path=log_file_path,
file_level="DEBUG",
console_level=console_log_level,
),
)

Expand Down