Jor*_*ard 6 python logging argparse python-3.x
我有一个命令行工具,可以生成大量日志。stdout我希望这些日志一完成就发送给他们。现在,程序完成所有内容(可能需要几分钟),然后将所有内容stdout一次性发送给所有人。这意味着目前用户不知道内部发生了什么,我想改变这一点。
到目前为止,我已经尝试将-u标志作为命令行参数传递,但这并没有改变任何东西。我有一种感觉,我需要以某种方式使用logging.handlers.BufferingHandlerwith flush(),但我一直无法通过修补来弄清楚。
def some_func(flag: bool):
if flag:
logging.info('flag is truthy')
return
logging.warning('flag is falsy')
Run Code Online (Sandbox Code Playgroud)
我希望上述函数在创建日志后立即向用户显示其中一个日志。注意:目前我使用logging.root.setLevel(logging.INFO).
谢谢!
san*_*_23 -2
StreamHandler在日志记录模块中使用
import logging
# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warning('warn message')
logger.error('error message')
logger.critical('critical message')
Run Code Online (Sandbox Code Playgroud)
这将转储控制台上的所有日志
$ python simple_logging_module.py
2005-03-19 15:10:26,618 - simple_example - DEBUG - debug message
2005-03-19 15:10:26,620 - simple_example - INFO - info message
2005-03-19 15:10:26,695 - simple_example - WARNING - warn message
2005-03-19 15:10:26,697 - simple_example - ERROR - error message
2005-03-19 15:10:26,773 - simple_example - CRITICAL - critical message
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6821 次 |
| 最近记录: |