简体   繁体   中英

Python logging:

There is a python tornado program run on Linux. Recently, I found a bug(may be not). Some import operation record will be recored to file named 'dagger.log'.

Because of logging.handler.RotatingFileHandler , when the log file is nearly maxBytes in length ,the system will successively create new files with the same pathname as the base file, but with extensions ".1", ".2" etc. appended to it. For example, with a backupCount of 8 and a base file name of "app.log", you would get "dagger.log","dagger.log.1", "dagger.log.2", ... through to "dagger.log.8".

But I found that log file on Linux, picture

dagger.log.[1-8] and dagger.log were created at 13:11 Jan 4th , dagger.log.[1-8] was last modified at nearly 13:31 Jan 4th. It was abnormal. Because the file dagger.log didn't reach the maximum size, dagger.log.[1-8] should not be created. what is more, every log file was different. they don't have the same record at all.

It confused me for a long time, I can't fix it. Can someone can help me? Forgive me for my awful English. I Don't know Can you understand what my problem is ?

Are you running multiple processes (with either tornado.process.fork_processes(8) or tornado.httpserver.HTTPServer.start(8) ? Logging with multiple processes can be tricky. The forked processes may start out sharing the same log file, then when it comes time to rotate each process will rotate independently, creating 8 new files.

The solution is to not configure logging until after you have started your child processes, and to give each process its own log file name (or, as grepe suggested, just log to stdout/stderr and let some external process deal with files and rotation). I recommend using an external process manager and load balancer for this, but if you still want to fork the child processes from within your Tornado app it would look something like this (starting from the "advanced multi-process" variant of HTTPServer initialization):

sockets = tornado.netutil.bind_sockets(8888)
task_id = tornado.process.fork_processes(0)
configure_logging('dagger%d.log' % task_id)
server = HTTPServer(app)
server.add_sockets(sockets)
IOLoop.current().start()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM