Setting up loggers from a config file

Viewed 36

I have a bunch of python scripts for my project and I want to implement logging configurations on them. So I have 2 loggers (log1, log2) all three have different levels, formatters and file handlers. All the loggers configurations are stored in a Logger.conf file to be implemented to. The purpose of having multiple loggers is to assign a logger to a certain file, some files are more important than others so depending on this hierarchy a file could be assigned log1, or log2. script1.py's logger will be log1 whilst script2.py and script3.py will be assigned to log2. The configurations of log1 and log2 are presented down below. How can I structure the Logger.conf and achieve the expected logger configurations.

Configurations:

  • log1:

    • SetLevel: DEBUG
    • Filehanler:
      • Setlevel: DEBUG
      • Formatter: %(asctimes) - %(name)s - %(levelname)s - %(message)s
      • output: logging.log
    • StreamHandler:
      • Setlevel: WARNING
      • Formatter: %(name)s - %(levelname)s - %(message)s
      • output: console
  • log2:

    • SetLevel: INFO
    • Filehanler:
      • Setlevel: INFO
      • Formatter: %(name)s : %(message)s
      • output: logging.log
    • SMTPHandler:
      • Setlevel: CRITICAL
      • Formatter: %(levelname)s - %(message)s
      • output emails: ['hello123@gmail.com','billyyes@gmail.com']

Directory Tree:

Project Folder/
├─Logger.conf
├─Logging.log
├─script1.py
├─script2.py
└─script3.py

code within script1.py, script2.py, script3.py

logger.DEBUG("hi this is a debug msg")
logger.INFO("hi this is an info msg")
logger.WARNING("hi this is a warning msg")
logger.ERROR("hi this is an error msg")
logger.CRITICAL("hi this is a critical msg")

When the code is ran on all scripts strating from script1 and ending at script3.py the expected outputs are as follows:

Expected Output:

console:

log1 - WARNING - hi this is a warning msg
log1 - ERROR -hi this is an error msg
log1 - CRITICAL -hi this is a critical msg

logging.log:

JULY-02-2022 04:30:31 - log1 - DEBUG - hi this is a debug msg
JULY-02-2022 04:30:31 - log1 - INFO - hi this is an info msg
JULY-02-2022 04:30:31 - log1 - WARNING - hi this is a warning msg
JULY-02-2022 04:30:31 - log1 - ERROR - hi this is an error msg
JULY-02-2022 04:30:31 - log1 - CRITICAL - hi this is a critical msg
log2 : hi this is an info msg
log2 : hi this is a warning msg
log2 : hi this is an error msg
log2 : hi this is a critical msg

email:

CRITICAL - hi this is a critical msg
0 Answers
Related