python項目_log日志的使用


log日志的使用(在settings.py文件中添加)

# 日志配置
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(lineno)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(module)s %(lineno)d %(message)s'
        },
    },
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'file': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            # 日志位置,日志文件名,日志保存目錄必須手動創建
            'filename': os.path.join(os.path.dirname(BASE_DIR), "logs/luffy.log"),
            # 日志文件的最大值,這里我們設置300M
            'maxBytes': 300 * 1024 * 1024,
            # 日志文件的數量,設置最大日志數量為10
            'backupCount': 10,
            # 日志格式:詳細格式
            'formatter': 'verbose'
        },
    },
    # 日志對象
    'loggers': {
        'django': {
            'handlers': ['console', 'file'],
            'propagate': True, # 是否讓日志信息繼續冒泡給其他的日志處理系統
        },
    }
}

2.自己需要手動添加logs文件

'filename': os.path.join(os.path.dirname(BASE_DIR), "logs/luffy.log"),   ##在指定路徑下添加logs文件

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM