2024-09-26 11:10:51 +02:00
|
|
|
#
|
|
|
|
# -----------------
|
|
|
|
# Copyright © 2024 ACK Cyfronet AGH, Poland.
|
|
|
|
# -----------------
|
|
|
|
#
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
|
|
|
|
def getDefaultLogger(name):
|
|
|
|
"""
|
|
|
|
Retrieves or creates a logger with the specified name and sets it up with a file handler.
|
|
|
|
|
|
|
|
The logger is configured to write log messages to the file path specified by the
|
2024-10-15 11:55:59 +02:00
|
|
|
'APP_LOG_FILE' environment variable. If the environment variable is not set,
|
2024-10-15 16:50:30 +02:00
|
|
|
the logger will write to the file 'base-logger-log.log' in the current
|
2024-10-15 11:55:59 +02:00
|
|
|
working directory. The logger uses the 'INFO' level as the default logging level
|
|
|
|
and writes log entries in the following format:
|
2024-09-26 11:10:51 +02:00
|
|
|
|
|
|
|
'YYYY-MM-DD HH:MM:SS,ms LEVEL logger_name message'
|
|
|
|
|
|
|
|
If the logger does not already have handlers, a file handler is created, and the
|
|
|
|
logging output is appended to the file. The log format includes the timestamp with
|
|
|
|
milliseconds, log level, logger name, and the log message.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
-----------
|
|
|
|
name : str
|
|
|
|
The name of the logger. This can be the name of the module or any identifier
|
|
|
|
that you want to associate with the logger.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
--------
|
|
|
|
logger : logging.Logger
|
|
|
|
A logger instance with the specified name. The logger is configured with a
|
2024-10-15 08:38:55 +02:00
|
|
|
file handler that writes to the file specified by the 'APP_LOG_FILE'
|
2024-10-15 16:50:30 +02:00
|
|
|
environment variable, or to 'base-logger-log.log' if the environment
|
2024-10-15 11:55:59 +02:00
|
|
|
variable is not set.
|
2024-09-26 11:10:51 +02:00
|
|
|
|
|
|
|
Example:
|
|
|
|
--------
|
|
|
|
logger = getDefaultLogger(__name__)
|
|
|
|
logger.info("This is an info message.")
|
2024-09-30 23:59:43 +02:00
|
|
|
try:
|
2024-10-03 08:40:42 +02:00
|
|
|
# some code causing exception
|
|
|
|
except Exception:
|
|
|
|
logger.exception('An error occurred')
|
2024-09-26 11:10:51 +02:00
|
|
|
|
2024-10-15 11:55:59 +02:00
|
|
|
Notes:
|
|
|
|
------
|
|
|
|
- The 'APP_LOG_FILE' environment variable should specify the full path to the log file.
|
2024-10-15 16:50:30 +02:00
|
|
|
- If 'APP_LOG_FILE' is not set, logs will be written to 'base-logger-log.log'.
|
2024-10-15 11:55:59 +02:00
|
|
|
|
2024-09-26 11:10:51 +02:00
|
|
|
"""
|
|
|
|
logger = logging.getLogger(name)
|
|
|
|
if not logger.hasHandlers():
|
2024-10-15 16:50:30 +02:00
|
|
|
file_handler = logging.FileHandler(os.environ.get('APP_LOG_FILE', 'base-logger-log.log'), mode='a')
|
2024-09-26 11:10:51 +02:00
|
|
|
formatter = logging.Formatter('%(asctime)s,%(msecs)d %(levelname)s %(name)s %(message)s')
|
|
|
|
file_handler.setFormatter(formatter)
|
|
|
|
logger.addHandler(file_handler)
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
return logger
|