Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

config - Managing loggers with Python logging

I'm writing a server app which should be able to log at different levels both on the console and a log file.

The problem is, if logging.basicConfig() is set it will log to the console but it must be set in the main thread.

It can also be set with logging.basicConfig(filename='logger.log') to write to a file.

Setting a handle either for console logging (logging.StreamHandler()) or file logging (logging.FileHandler()) complements the logging.baseconfig() option set.

The problem is, that the settings are not independent. What I mean is, the loglevel of logging.baseConfig() must include the Handler level, or it wont be logged.

So if I set the baseConfig to log to file, and a StreamHandler to log to console, the file loglevel must be lower than the console level. (Also, the basicConfig option logs all other logs.)

I tried to create two Handles, one for the console and one for the log file, they work, but whatever log type is specified by basicConfig() will still be displayed duplicating messages.

Is there a way to disable the output of basicConfig() ? Or any other way to implement these options ?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't say in your question exactly what levels you want on your console and file logging. However, you don't need to call basicConfig(), as it's only a convenience function. You can do e.g. (code just typed in, not tested):

import logging

logger = logging.getLogger(__name__)
configured = False

def configure_logging():
    global configured
    if not configured:
        logger.setLevel(logging.DEBUG) # or whatever
        console = logging.StreamHandler()
        file = logging.FileHandler('/path/to/file')
        #set a level on the handlers if you want;
        #if you do, they will only output events that are >= that level
        logger.addHandler(console)
        logger.addHandler(file)
        configured = True

Events are passed to the logger first, and if an event is to be processed (due to comparing the level of the logger and the event) then the event is passed to each handler of the logger and all its ancestors' handlers as well. If a level is set on a handler the event may be dropped by that handler, otherwise it will output the event.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...