Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

ignore module import log in python

------------ [REASON] ------------
# The problem is that calling getLogger without arguments returns the root logger
# so when you set the level to logging.DEBUG you are also setting the level for
# other modules that use that logger.

------------ [PREVIOUSLY] ------------
## so instead of this
import logging
log_format = "%(levelname)s|%(asctime)s|%(message)s"
logging.basicConfig(filename='monitor_model.log',
					level=logging.DEBUG, format=log_format,
					datefmt="%Y-%m-%d %H:%M:%S")


------------ [NEW] ------------
## do this
#						       __________ it will get the module name
# logger config               ↓
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# log format
log_format = logging.Formatter("%(levelname)s|%(name)s|%(asctime)s|%(message)s")
# 												↑_____ add module name in log
# file handler
file_handler = logging.FileHandler('monitor_model.log')
file_handler.setFormatter(log_format)
logger.addHandler(file_handler)

# now you can use logger (eg)
logger.info(f'this is some message')
 
PREVIOUS NEXT
Tagged: #ignore #module #import #log #python
ADD COMMENT
Topic
Name
7+8 =