ISEPOS-2280 Added base loggers scripts
This commit is contained in:
55
python/logging/base_logger.py
Normal file
55
python/logging/base_logger.py
Normal file
@@ -0,0 +1,55 @@
|
||||
#
|
||||
# -----------------
|
||||
# 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
|
||||
'DEFAULT_LOG_PATH' environment variable. It uses the 'INFO' level as the default
|
||||
logging level and writes log entries in the following format:
|
||||
|
||||
'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
|
||||
file handler that writes to the file specified by the 'DEFAULT_LOG_PATH'
|
||||
environment variable.
|
||||
|
||||
Raises:
|
||||
-------
|
||||
KeyError:
|
||||
If the 'DEFAULT_LOG_PATH' environment variable is not set.
|
||||
|
||||
Example:
|
||||
--------
|
||||
logger = getDefaultLogger(__name__)
|
||||
logger.info("This is an info message.")
|
||||
|
||||
"""
|
||||
logger = logging.getLogger(name)
|
||||
if not logger.hasHandlers():
|
||||
file_handler = logging.FileHandler(os.environ['DEFAULT_LOG_PATH'], mode='a')
|
||||
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
|
Reference in New Issue
Block a user