请教, log 如何命名分割业务?

2020-05-21 16:47:26 +08:00
 chaleaoch

level 啊, RotatingFileHandler, 时间戳啊, 这些都还不是问题. 在哪里打 log, 打多打少,这方面虽然还不是很成熟,但也不是问题的关键.

django 官网是这么说的.

By convention, the logger name is usually __name__, the name of the Python module that contains the logger. This allows you to filter and handle logging calls on a per-module basis. However, if you have some other way of organizing your logging messages, you can provide any dot-separated name to identify your logger:

# Get an instance of a specific named logger
logger = logging.getLogger('project.interesting.stuff')

我用起来不是很习惯, 当然目前项目的代码结构有问题,也是导致__name__的方式不好用的原因之一.

除了这种方式, 现在不是很清楚, 是否还有其他最佳实践可以分享给楼主, logger 要分多少个? 怎么分?

谢谢了

其实就是想看一下大家的这个东西.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '[%(process)d] [%(asctime)s] %(levelname)s [%(filename)s:%(lineno)s] %(message)s'
        }
    },
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'maxBytes': 1024*1024,
            'backupCount': 5,
            'filename': '/tmp/run.log',
            'formatter': 'verbose'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'django_docker': {
            'handlers': ['file', 'console'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['file'],
            'level': 'ERROR',
            'propagate': False,
        }
    },
}
806 次点击
所在节点    程序员
1 条回复
jones2000
2020-05-22 00:54:24 +08:00
日志这个东西怎么说呢, 应该是越详细越好,但是打的多了会降低性能,你要把日志成分多个等级的,如果调试日志,模块日志, 错误日志 等等要细分, 通过接口就可以动态控制打印哪些等级或哪些模块的日志。
我一般使用的日志格式: 日期 ,时间, 进程 id, 线程 id, 日志等级, 文件名,行号, 类名, 函数名 , 具体的日志输出信息。
日志信息文件可以是 1 个小时生成 1 个日志文件或 1 天生成 1 个日志文件,具体看日志量了。 后面就是对接日志收集+日志分析系统,开源的很多随便找一个就可以,然后定期清理日志文件比如保留最近 1 个月或 1 个星期

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/674067

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX