I'm rewriting a small daemon from bash to Python-3.x to have more powerful language. I'm really new to Python language.
So, I'm trying to use Python's logging module to log messages in my script. I would like to log all message levels except for debug level through Syslog using SysLogHandlerand logging debug messages to a file if only --debug option is enabled.
I'm using Python-3.6 on Gentoo Gnu/linux. This is for a daemon which auto syncs and auto pretends world update for gentoo's portage package manager. I setup already the logging through Syslog using SysLogHandler and all messages expect for debug ones are shown. I setup as well the logging through the file using WatchedFileHandler, but I haven't found a way to filter only debug messages. Neither I found a way to enable debug only if --debug option is enabled.
My code:
import logging, logging.handlers debug_log = "debug.log" debug = "no" def Create_logger(): """Setup the logging environment""" logging.addLevelName(logging.CRITICAL, '[Crit ]') logging.addLevelName(logging.ERROR, '[Error]') logging.addLevelName(logging.WARNING, '[Warn ]') logging.addLevelName(logging.INFO, '[Info ]') logging.addLevelName(logging.DEBUG, '[Debug]') logger = logging.getLogger(name) # Debug part file_handler = logging.handlers.WatchedFileHandler(debug_log) file_handler.setLevel(logging.DEBUG) file_formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s') file_handler.setFormatter(file_formatter) logger.addHandler(file_handler) # Syslog part syslog_handler = logging.handlers.SysLogHandler(address='/dev/log',facility='daemon') syslog_handler.setLevel(logging.INFO) syslog_formatter = logging.Formatter('%(name)s %(levelname)s %(message)s') syslog_handler.setFormatter(syslog_formatter) logger.addHandler(syslog_handler) return logger log=Create_logger() log.error('This is error') log.setLevel(logging.CRITICAL) log.error('This is an second error') log.critical('This is critical !!!') log.setLevel(logging.INFO) log.info('Hello world :p') log.debug('this is an debug message') log.setLevel(logging.DEBUG) log.debug(f'This is an other debug message and debug log is locate to {debug_log}') What I get from /var/log/messages (syslog):
Aug 9 23:43:23 Gentoo syuppod[26195]: [Error] This is error Aug 9 23:43:23 Gentoo syuppod[26195]: [Crit ] This is critical !!! Aug 9 23:43:23 Gentoo syuppod[26195]: [Info ] Hello world :p What I get from debug.log:
2019-08-09 23:43:23,052 syuppod [Error] This is error 2019-08-09 23:43:23,052 syuppod [Crit ] This is critical !!! 2019-08-09 23:43:23,052 syuppod [Info ] Hello world :p 2019-08-09 23:43:23,052 syuppod [Debug] This is an other debug message and debug log is locate to debug.log So, it's Ok for Syslog logging, but not for debug.log file and I tried if debug == "yes" statement before declaring all debug part in function Create_logger(), but it's not working.
I also found this question, but its answer covers only a half of what I'm trying to do. I'm still trying to find a solution in the available documentation.
Could you please advise me?