how to print in a specific log file using a logger without printing in the supervisord log file in a tornado application
Clash Royale CLAN TAG#URR8PPP
how to print in a specific log file using a logger without printing in the supervisord log file in a tornado application
I'm currently developing a Tornado application made from different indipendent modules.
The application run using supervisord, so right now, every time I use
logging.info()
the log is printed on the supervisor log and I'm ok with that.
The problem is that now the supervisor log file is full of very different stuff from different modules and very hard to read, so I now want that every module use a specific logger and every logger write on a different file.
So I created the logger:
def set_log_config(filename, when='h', interval=1, backupCount=0):
directory = os.path.dirname(os.path.abspath(filename))
create_folder(directory)
app_log = logging.getLogger("tornado.application.fiscal")
handler = logging.handlers.TimedRotatingFileHandler(filename, when=when, interval=interval, backupCount=backupCount)
formatter = logging.Formatter('[%(levelname)s %(asctime)s.%(msecs)d %(module)s:%(lineno)d] %(message)s', datefmt='%y%m%d %H:%M:%S')
handler.setFormatter(formatter)
app_log.addHandler(handler)
app_log.setLevel(logging.INFO)
return app_log
fiscal_logger = set_log_config(
'/home/dir/Trace/fiscal/fiscal_module_:%Y-%m-%d.log'.format(datetime.now(), when='midnight', interval=1, backupCount=21)
)
The logger works, it write on the specific file, but it also always write in the supervisor log file and I don't understand why.
So my question is: how can I write on the specific file when I use the fiscal_logger.info and on the supervisor file when I use logging.info?
1 Answer
1
First I explain why your logger also writes to the supervisor log file.
Writing to supervisor log file means there is a StreamHandler
in your current logger chain.
StreamHandler
logging.info
basically equals to logging.getLogger().info
which means it uses root
logger. Additionally, logging.xxx
will automatically add a StreamHandler
to root
logger if root
has no handler.
logging.info
logging.getLogger().info
root
logging.xxx
StreamHandler
root
root
And by default logs will be propagated along logger chain(For example, "tornado.application.fiscal"
's logger chain is root -> tornado -> application -> fiscal
). So fiscal_logger
's log is propagated to root
logger and processed by root
's StreamHandler
. That's why you see those logs in the supervisor log file.
"tornado.application.fiscal"
root -> tornado -> application -> fiscal
fiscal_logger
root
root
StreamHandler
To fix this problem, you have two options at least.
logging.xxxx
console_logger
fiscal_logger.propagate
False
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.