Logging is not happening when used it it another file in Python

Viewed 19

Problem Decsription: Logs are not written from one script file.

I have a project structure as below

Project
Logs
  commonLogging.py
  logging.conf
Script
  test.py

In test.py file the code exist as below

import logging.config
from Logs.commonLogging import *

projectRoot = Path(__file__).parent.parent
logDir = projectRoot.joinpath("Logs")
loggingConfigFile = logDir.joinpath("logging.conf")
logfile = logDir.joinpath("output.log")
logging.config.fileConfig(loggingConfigFile, defaults={'logfilename': logfile.as_posix() }, disable_existing_loggers=False)
    
def method1():
  method_name_logging(inspect.stack()[0][3])   ---> This logging is not coming in the log output file
  logging.info('Test23')

In commonLogging.py file the method 'method_name_logging' has been defined and it looks as below

import logging

def method_name_logging(method_name):
  logging.debug(f'Inside function: {method_name}')

The output log file looks as below

Log file output: INFO:2022-09-19 17:59:22,687:test:method1:10:Test123

The log output file should contain the below log

Inside function: method1

Not sure why this not getting printed in the log file.

log config file looks as below

[loggers]
keys=root,simpleExample

[handlers]
keys=consoleHandler,fileHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=INFO
handlers=consoleHandler,fileHandler

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler,fileHandler
qualname=simpleExample
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=('%(logfilename)s', 'a', 'utf8')

[formatter_simpleFormatter]
format= %(levelname)s:%(asctime)s:%(module)s:%(funcName)s:%(lineno)d:%(message)s
1 Answers

The problem is the 'Info' level logs were not getting printed. I updated the logging.conf file as below

[logger_root]
level=DEBUG
handlers=consoleHandler,fileHandler

Then it started working fine. But still, I could not figure out the relation between logger_root and handler_consoleHandler/handler_fileHandler

Related