V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chaleaoch
V2EX  ›  程序员

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

  •  
  •   chaleaoch · 2020-05-21 16:47:26 +08:00 · 800 次点击
    这是一个创建于 1441 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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,
            }
        },
    }
    
    1 条回复    2020-05-22 00:54:24 +08:00
    jones2000
        1
    jones2000  
       2020-05-22 00:54:24 +08:00
    日志这个东西怎么说呢, 应该是越详细越好,但是打的多了会降低性能,你要把日志成分多个等级的,如果调试日志,模块日志, 错误日志 等等要细分, 通过接口就可以动态控制打印哪些等级或哪些模块的日志。
    我一般使用的日志格式: 日期 ,时间, 进程 id, 线程 id, 日志等级, 文件名,行号, 类名, 函数名 , 具体的日志输出信息。
    日志信息文件可以是 1 个小时生成 1 个日志文件或 1 天生成 1 个日志文件,具体看日志量了。 后面就是对接日志收集+日志分析系统,开源的很多随便找一个就可以,然后定期清理日志文件比如保留最近 1 个月或 1 个星期
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   775 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:51 · PVG 04:51 · LAX 13:51 · JFK 16:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.