0

I'm trying to setup an environment where I can log to multiple files without adding too much complexity. The current setup is outlined below. What's convenient about this is that I can just call logger = logging.getLogger(__name__) anywhere after initiating set_logger() and it just works "globally" without any extra code.

Now let's say I would like to have a separate file that only logs security related messages, without adding too much extra complexity? E.g. something like logger2 = logging.getLogger(__name__, file='security.log') that is callable from within the module would be ideal. Is that somehow possible? All Stack Overflow answers I found basically suggested having to construct a new logger in each module instead.

# settings.py
LOG_LEVEL = logging.INFO
LOG_FORMAT = '[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s():%(lineno)d] – %(message)s'
LOG_FILE = 'orchestrator.log'
LOG_PATH = os.path.join(os.getcwd(), 'logs', LOG_FILE)
LOG_BACKUPS = 21

def set_logger(debug: bool = None):
    _LOG_LEVEL = logging.DEBUG if debug else LOG_LEVEL
    logging.basicConfig(
        format=LOG_FORMAT,
        datefmt='%Y/%m/%d %H:%M:%S',
        level=_LOG_LEVEL,
        handlers=[
            logging.StreamHandler(),
            logging.handlers.TimedRotatingFileHandler(
                LOG_PATH,
                encoding='utf-8',
                when="d",
                interval=1,
                backupCount=LOG_BACKUPS
            )
        ]
    )
#main.py
import settings
settings.set_logger()
#module.py
import logging
logger = logging.getLogger(__name__)
schlumpfpirat
  • 195
  • 2
  • 12

0 Answers0