除日誌文件外,使Python記錄器還將所有消息輸出到stdout

本文翻譯自:Making Python loggers output all messages to stdout in addition to log file

Is there a way to make Python logging using the logging module automatically output things to stdout in addition to the log file where they are supposed to go? 是否有辦法使用logging模塊自動將事物輸出到stdout 以及它們應該去的日誌文件? For example, I'd like all calls to logger.warning , logger.critical , logger.error to go to their intended places but in addition always be copied to stdout . 例如,我希望所有調用logger.warninglogger.criticallogger.error去他們想要的地方,但另外總是被複制到stdout This is to avoid duplicating messages like: 這是爲了避免重複消息,如:

mylogger.critical("something failed")
print "something failed"

#1樓

參考:https://stackoom.com/question/wzFF/除日誌文件外-使Python記錄器還將所有消息輸出到stdout


#2樓

All logging output is handled by the handlers; 所有日誌記錄輸出都由處理程序處理; just add a logging.StreamHandler() to the root logger. 只需將logging.StreamHandler()添加到根記錄器。

Here's an example configuring a stream handler (using stdout instead of the default stderr ) and adding it to the root logger: 這是配置流處理程序(使用stdout而不是默認的stderr )並將其添加到根記錄器的示例:

import logging
import sys

root = logging.getLogger()
root.setLevel(logging.DEBUG)

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)

#3樓

It's possible using multiple handlers. 可以使用多個處理程序。

import logging
import auxiliary_module

# create logger with 'spam_application'
log = logging.getLogger('spam_application')
log.setLevel(logging.DEBUG)

# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
log.addHandler(fh)

# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
ch.setFormatter(formatter)
log.addHandler(ch)

log.info('creating an instance of auxiliary_module.Auxiliary')
a = auxiliary_module.Auxiliary()
log.info('created an instance of auxiliary_module.Auxiliary')

log.info('calling auxiliary_module.Auxiliary.do_something')
a.do_something()
log.info('finished auxiliary_module.Auxiliary.do_something')

log.info('calling auxiliary_module.some_function()')
auxiliary_module.some_function()
log.info('done with auxiliary_module.some_function()')

# remember to close the handlers
for handler in log.handlers:
    handler.close()
    log.removeFilter(handler)

Please see: https://docs.python.org/2/howto/logging-cookbook.html 請參閱: https//docs.python.org/2/howto/logging-cookbook.html


#4樓

The simplest way to log to stdout: 記錄到stdout的最簡單方法:

import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

#5樓

The simplest way to log to file and to stderr: 記錄到文件和stderr的最簡單方法:

import logging

logging.basicConfig(filename="logfile.txt")
stderrLogger=logging.StreamHandler()
stderrLogger.setFormatter(logging.Formatter(logging.BASIC_FORMAT))
logging.getLogger().addHandler(stderrLogger)

#6樓

You could create two handlers for file and stdout and then create one logger with handlers argument to basicConfig . 您可以爲file和stdout創建兩個處理程序,然後創建一個帶有basicConfig handlers參數的記錄器。 It could be useful if you have the same log_level and format output for both handlers: 如果兩個處理程序具有相同的log_level和format輸出,那麼它可能很有用:

import logging
import sys

file_handler = logging.FileHandler(filename='tmp.log')
stdout_handler = logging.StreamHandler(sys.stdout)
handlers = [file_handler, stdout_handler]

logging.basicConfig(
    level=logging.DEBUG, 
    format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
    handlers=handlers
)

logger = logging.getLogger('LOGGER_NAME')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章