Skip to content

Logging

A class to handle logging messages with different verbosity levels.

__init__(logger_settings=None, logfile_name=None, file_verbosity='detailed', console_verbosity='summary', max_lines=10000, log_process_id=False)

Initializes the logger with configuration settings.

If logger_settings are provided, it will override the individual parameters.

Parameters:

Name Type Description Default
logger_settings Optional[dict]

A dictionary containing logger settings. If provided, it should include the same keys as the individual parameters below:

None
logfile_name Optional[str]

The name of the log file. If None, no file logging will be done.

None
file_verbosity Optional[str]

Verbosity level for file logging

'detailed'
console_verbosity (Optional[str]

Verbosity level for console logging

'summary'
max_lines (Optional[int]

Maximum number of lines to keep in the log file

10000
log_process_id Optional[bool]

If True, include the process ID in log messages. Defaults to False.

False

clear_fatal_error()

Clear a previously logged fatal error.

Returns:

Name Type Description
result bool

True if the file was deleted, False if it did not exist.

get_fatal_error()

Returns True if a fatal error was previously reported, false otherwise.

initialise_settings(logger_settings=None, logfile_name=None, file_verbosity='detailed', console_verbosity='summary', max_lines=10000, log_process_id=False)

Set / reset the logger configuration settings.

If logger_settings are provided, it will override the individual parameters.

Parameters:

Name Type Description Default
logger_settings Optional[dict]

A dictionary containing logger settings. If provided, it should include the same keys as the individual parameters below:

None
logfile_name Optional[str]

The name of the log file. If None, no file logging will be done.

None
file_verbosity Optional[str]

Verbosity level for file logging

'detailed'
console_verbosity (Optional[str]

Verbosity level for console logging

'summary'
max_lines (Optional[int]

Maximum number of lines to keep in the log file

10000
log_process_id Optional[bool]

If True, include the process ID in log messages. Defaults to False.

False

log_fatal_error(message, report_stack=False, calling_function=None)

Log a fatal error, send an email if configured to so and then exit the program.

Parameters:

Name Type Description Default
message str

The error message to log.

required
report_stack Optional[bool]

If True, include the stack trace in the log message.

False
calling_function Optional[str]

The name of the function that called this method, if known. If None, the calling function will be determined automatically.

None

Raises:

Type Description
SystemExit

Exits the program with a status code of 1 after logging the fatal error.

log_message(message, verbosity='summary')

Writes a log message to the console and/or a file based on verbosity settings.

Parameters:

Name Type Description Default
message str

The message to log.

required
verbosity str

The verbosity level for the message. Must be one of "none", "error", "warning", "summary", "detailed", "debug", or "all".

'summary'

Raises:

Type Description
ValueError

If an invalid verbosity level is provided.

register_email_settings(email_settings)

Registers email settings for sending emails.

Use the SCConfigManager.get_email_settings() method to get a dictionary containing the email settings. Otherwise, you can pass a dictionary directly to this method with these keys: SendEmailsTo: str, the email address to send emails to SMTPServer: str, the SMTP server address SMTPPort: int, the SMTP server port (optional, defaults to 587) SMTPUsername: str, the username for the SMTP server SMTPPassword: str, the password for the SMTP server (preferably an App Password) SubjectPrefix: str, a prefix for email subjects (optional, default to None)

Parameters:

Name Type Description Default
email_settings Optional[dict]

A dictionary containing email settings. If empty or None, no email settings will be registered.

required

Raises:

Type Description
TypeError

If email_settings is not a dictionary.

ValueError

If any required keys are missing from the email_settings dictionary.

send_email(subject, body, test_mode=False)

Sends an email using the SMTP server previously specified in register_email_settings().

Parameters:

Name Type Description Default
subject str

The subject of the email.

required
body str

The body of the email. This argument can be one of 4 things: 1. A string containing the HTML body of the email 2. A string or Path containing the path to an HTML file to read the body from 3. A string containing the text body of the email 4. A string or Path containing the path to an text file to read the body from

required
test_mode bool

If True, the email will not be sent, but a message will be logged indicating that the email would have been sent. Defaults to False.

False

Returns:

Name Type Description
result bool

True if the email was sent successfully, False otherwise.

set_fatal_error(message)

Create a fatal error tracking file and write the message to it.

Parameters:

Name Type Description Default
message str

The error message to write to the fatal error file.

required

trim_logfile()

Trims the log file to the maximum number of lines specified.