![](/img/trans.png)
[英]Python logging configuration file with bandersnatch on python 3.5
[英]Python logging configuration file
我在嘗試登錄我的 python 項目時似乎遇到了一些問題。
我只是試圖模仿以下配置:
但是,我不想在代碼中執行此操作,而是希望將其放在配置文件中。
下面是我的配置文件:
[loggers]
keys=root
[logger_root]
handlers=screen,file
[formatters]
keys=simple,complex
[formatter_simple]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
[formatter_complex]
format=%(asctime)s - %(name)s - %(levelname)s - %(module)s : %(lineno)d - %(message)s
[handlers]
keys=file,screen
[handler_file]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=5
formatter=complex
level=DEBUG
args=('logs/testSuite.log',)
[handler_screen]
class=StreamHandler
formatter=simple
level=INFO
args=(sys.stdout,)
問題是我的屏幕輸出看起來像:
2010-12-14 11:39:04,066 - 根 - 警告 - 3
2010-12-14 11:39:04,066 - 根 - 錯誤 - 4
2010-12-14 11:39:04,066 - 根 - 關鍵 - 5
我的文件是輸出,但看起來和上面一樣(雖然包含了額外的信息)。 然而,調試和信息級別不會輸出到任何一個。
我在 Python 2.7
這是我顯示失敗的簡單示例:
import os
import sys
import logging
import logging.config
sys.path.append(os.path.realpath("shared/"))
sys.path.append(os.path.realpath("tests/"))
class Main(object):
@staticmethod
def main():
logging.config.fileConfig("logging.conf")
logging.debug("1")
logging.info("2")
logging.warn("3")
logging.error("4")
logging.critical("5")
if __name__ == "__main__":
Main.main()
看起來您已經為處理程序設置了級別,但沒有為記錄器設置級別。 記錄器的級別會在每條消息到達其處理程序之前對其進行過濾,默認值為WARNING
及以上(如您所見)。 將根記錄器的級別設置為NOTSET
,並將其設置為DEBUG
(或您希望記錄的最低級別)應該可以解決您的問題。
將以下行添加到根記錄器解決了我的問題:
level=NOTSET
只需在[logger_root]
添加日志級別。 它起作用了。
[logger_root]
level=DEBUG
handlers=screen,file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import logging.handlers
from logging.config import dictConfig
logger = logging.getLogger(__name__)
DEFAULT_LOGGING = {
'version': 1,
'disable_existing_loggers': False,
}
def configure_logging(logfile_path):
"""
Initialize logging defaults for Project.
:param logfile_path: logfile used to the logfile
:type logfile_path: string
This function does:
- Assign INFO and DEBUG level to logger file handler and console handler
"""
dictConfig(DEFAULT_LOGGING)
default_formatter = logging.Formatter(
"[%(asctime)s] [%(levelname)s] [%(name)s] [%(funcName)s():%(lineno)s] [PID:%(process)d TID:%(thread)d] %(message)s",
"%d/%m/%Y %H:%M:%S")
file_handler = logging.handlers.RotatingFileHandler(logfile_path, maxBytes=10485760,backupCount=300, encoding='utf-8')
file_handler.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(default_formatter)
console_handler.setFormatter(default_formatter)
logging.root.setLevel(logging.DEBUG)
logging.root.addHandler(file_handler)
logging.root.addHandler(console_handler)
[31/10/2015 22:00:33] [DEBUG] [yourmodulename] [yourfunction_name():9] [PID:61314 TID:140735248744448] this is logger infomation from hello module
我認為您應該將 disable_existing_loggers 添加到 false。
寫入終端和文件的簡單方法如下:
import logging.config
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("log_file.log"),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
然后在您的代碼中使用它,如下所示:
logger.info('message')
logger.error('message')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.