0

I am using RotatingFilehandler to keep 5 files as backup. but it is not logging the messages from all the modules except the main module.

basicconfig is working but it creates only one file.

log_check.py :

import ll
import logChk

import logging
from logging.handlers import RotatingFileHandler

LOG_FILENAME = 'check.log'
logger = logging.getLogger(__name__)
formatter =logging.Formatter("[%(asctime)s] %(levelname)s [ %(name)s.%(funcName)s:%(lineno)d] - %(message)s")
logger.setLevel(logging.DEBUG)
handler = RotatingFileHandler(LOG_FILENAME, maxBytes=2000, backupCount=5)
handler.setFormatter(formatter)
logger.addHandler(handler)


logger = logging.getLogger(__name__)

def foo():
    logger.warning("logging from d on call foo()")

if __name__ == "__main__":

    logger.info('Starting logger for...')

    logger.warning('logging from c')
    foo()
    logChk.doo()
    ll.fun()

ll.py :

import logging
import log_conf
logger=logging.getLogger(__name__)

def fun():
    logger.info("In Fun")

logChk.py :

import logging

logger = logging.getLogger(__name__)

def doo():
    logger.info('Starting logger for... in dooo')
3
  • it is not logging the messages from all the modules except the main module. I do not see any reference to a main module in your code snippets here at time of writing. Can you add it or adjust your module names appropriately? Commented Sep 15, 2019 at 19:24
  • you tagged this question with python2.7 and python3.x - which are you using, you didnt specify? Commented Sep 15, 2019 at 19:27
  • Check out the docs and the reference on disable_existing_loggers as well to see if this is what's getting you: docs.python.org/3.7/howto/logging.html#configuring-logging Commented Sep 15, 2019 at 19:28

1 Answer 1

2

The loggers are organised hierarchically by name. When a log message is logged, it will traverse the loggers looking for handlers, starting with the logger you logged the message at, moving up the hierarchy. The hierarchy follows the names, so since you are naming your loggers by module names, a message logged to the module "x.y.z", will first try finding handlers attached to "x.y.z", then "x.y", "x" and finally the root logger, "".

So to log all messages (as you normally want to do), you need to add a handler to the root logger instead of the module logger.

LOG_FILENAME = 'check.log'
rootlogger = logging.getLogger() # No name here will return you the root logger
formatter =logging.Formatter("[%(asctime)s] %(levelname)s [ % (name)s.%(funcName)s:%(lineno)d] - %(message)s")
rootlogger.setLevel(logging.DEBUG)
handler = RotatingFileHandler(LOG_FILENAME, maxBytes=2000, backupCount=5)
handler.setFormatter(formatter)
rootlogger.addHandler(handler)

To manage your logging configuration, I would recommend you not set handlers and formatters manually, but instead use the logging.config module.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.