python - 在 python 中使用logging.config.dictConfig配置日志记录时出错

标签 python logging

首先,我正在使用python。尝试通过配置json文件配置日志记录并使用logging.config.dictConfig()。我之前使用过它,效果很好。但这次我删除了一些内容,但有些内容却不起作用。 我的配置文件将如下所示:

{
    "version": 1,
    "disable_existing_loggers": false,
    "formatters": {
        "simple": {
            "format": "%(name)-6s | %(levelname)-8s | %(message)s"
        },
        "info_format": {
            "format": "%(name)-6s | %(levelname)-6s | %(message)s"
        },
        "error_format": {
            "format": "%(filename)-8s | %(module)-12s | %(funcName)s | %(lineno)d : %(message)s"
        },
        "debug_format": {
             "format": "%(filename)-8s | %(module)-12s | %(funcName)s | %(lineno)d : %(message)s"
        }
    },
    "handlers": {
        "console": {
             "class": "logging.StreamHandler",
             "level": "DEBUG",
             "formatter": "simple",
             "stream": "ext://sys.stdout"
        },
        "info_handler": {
            "level": "INFO",
            "formatter": "info_format",
            "encoding": "utf8",
            "stream": "ext://sys.stdout"
        },
        "error_handler": {
            "level": "ERROR",
            "formatter": "error_format",
            "encoding": "utf8",
            "stream": "ext://sys.stderr"
        }
    },
    "loggers": {
        "info": {
             "level": "INFO",
             "handlers": ["console"],
             "propagate": "no"
         },
         "error": {
             "level": "ERROR",
             "handlers": ["console"],
             "propagate": "no"
         },
         "tornado.access": {
             "level": "INFO",
             "handlers": ["console"],
             "propagate": "no"
         },
         "tornado.general": {
             "level": "INFO",
             "handlers": ["console"],
             "propagate": "no"
         },
         "tornado.application": {
             "level": "ERROR",
             "handlers": ["console"],
             "propagate": "no"
         }
    }

}

这是我配置日志记录的方法:

configParser = ConfigParser.ConfigParser()
__ProjectPath = os.getcwd()
config_path = __ProjectPath + "/configs/logconfig.json"
with open(config_path, 'rt') as f:
    configs = json.loads(f.read())
    logging.config.dictConfig(configs)

这是错误消息:

Traceback (most recent call last):
  File "/home/jonah/code/leancloud-demo/wsgi.py", line 13, in <module>
    from app import app
  File "/home/jonah/code/leancloud-demo/app.py", line 14, in <module>
    import log
  File "/home/jonah/code/leancloud-demo/log.py", line 51, in <module>
    logging.config.dictConfig(configs)
  File "/usr/lib/python2.7/logging/config.py", line 794, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib/python2.7/logging/config.py", line 576, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler u'error_handler': 'NoneType' object has no attribute 'split'

Process finished with exit code 1

我用谷歌了一段时间,就是找不到答案。抱歉我的英语不好。

最佳答案

阅读https://docs.python.org/2/library/logging.config.html#dictionary-schema-details

// class (mandatory). This is the fully qualified name of the handler class

"info_handler": {
    "class": "logging.StreamHandler",
    "level": "INFO",
    "formatter": "info_format",
    "stream": "ext://sys.stdout"
},
"error_handler": {
    "class": "logging.StreamHandler",
    "level": "ERROR",
    "formatter": "error_format",
    "stream": "ext://sys.stderr"
}

关于python - 在 python 中使用logging.config.dictConfig配置日志记录时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32626100/

相关文章:

python - 创建 .csv 文件的远程 python 脚本(权限被拒绝)

python - plotly :更改森伯斯特图中元素的顺序

configuration - 如何通过配置文件在 Jetty 中配置登录?

java - 我可以在导出 RCPas 产品时导出 log4j.properties 吗?

linux - 捕获标准错误和标准输出的好方法

python - 如何解决 kivy.graphics DLL 问题?

python - 为 Sqlite3 Python 更新多个值的优雅方式

python - 如何按前缀字母顺序和后缀数字顺序对 Python 字符串进行排序?

java - 为什么 java.util.logging.Logger 打印到 stderr?

python - 自定义管理命令的 Django 日志记录