Python仅在一个日志文件上禁用日志记录

93ze6v8z  于 8个月前  发布在  Python
关注(0)|答案(1)|浏览(78)

我在屏幕上和两个单独的日志文件上配置了日志记录,一个是INFO,一个只是ERROR。
通过设置INFO DISABLE = 1,我希望能够禁用INFO日志文件上的日志记录,同时保持ERROR文件和屏幕上的日志记录。我的文件:
logging.conf

[loggers]
keys=root

[handlers]
keys=screen, file1, file2

[formatters]
keys=simple,complex

[formatter_simple]
format=%(asctime)s - %(filename)s  %(levelname)-5s %(message)s
datefmt=[%d-%m-%Y %H:%M:%S]

[formatter_complex]
format=%(asctime)s - [%(filename)s:%(lineno)d in function '%(funcName)s']  %(levelname)-5s %(message)s
datefmt=[%d-%m-%Y %H:%M:%S]

[logger_root]
handlers = screen,file1,file2
level = NOTSET

[handler_file1]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=15
formatter=simple
level=INFO
args=('info.log',)

[handler_screen]
class=StreamHandler
formatter=simple
level=INFO
args=(sys.stdout,)

[handler_file2]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=15
formatter=complex
level=ERROR
args=('error.log',)

字符串
bot.py

import logging

##### LOGGING
if LOG_INFO_DISABLE == 1:
    logging.disable(logging.ERROR)

logger = logging.getLogger(__name__)

...
...

if __name__ == '__main__':
    import logging.config
    logging.config.fileConfig('logging.conf')
    ...


现在它也为我禁用了屏幕记录。

puruo6ea

puruo6ea1#

我将handler_file1的级别更改为“”,这意味着它将只记录严重性为“”或更高级别的消息。INFO消息将不会记录到文件中,但它们仍将记录到控制台中。

[handler_file1]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=15
formatter=simple
level=WARNING  # Change this line to set the desired level
args=('info.log',)

字符串

相关问题