V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
chaleaoch
V2EX  ›  程序员

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

  •  
  •   chaleaoch · May 21, 2020 · 1181 views
    This topic created in 2180 days ago, the information mentioned may be changed or developed.

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