Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django Practice - Use Python's builtin Logging #1

Open
plynte opened this issue Dec 21, 2017 · 0 comments
Open

Django Practice - Use Python's builtin Logging #1

plynte opened this issue Dec 21, 2017 · 0 comments

Comments

@plynte
Copy link
Owner

plynte commented Dec 21, 2017

Django Practice - Use Python's builtin Logging

settings.py

LOG_LEVEL = 'DEBUG' if DEBUG else 'WARNING'
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'main': {
            'datefmt': '%Y-%m-%d %H:%M:%S',
            'format': '%(asctime)s [%(module)s %(levelname)s] %(message)s',
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {

    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
        },
        'mail_admins': {
            'level': 'DEBUG',
            'class': 'django.utils.log.AdminEmailHandler',
            'include_html': True,
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'main'
        },
        'default': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'formatter': 'main',
            'filename': os.path.join(BASE_DIR, 'logs', 'output.log')
        },
        'myhandler': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'formatter': 'main',
            'filename': os.path.join(BASE_DIR, 'logs', 'azurecloudapi.log')
        },
    },
    'loggers': {
        # loggers 类型 为"django" 这将处理所有类型的日志
        'django': {
            'handlers': ['null'],
            'propagate': False,
            'level': LOG_LEVEL,
        },
        'django.request': {
            'handlers': ['console', 'default'],
            'level': LOG_LEVEL,
            'propagate': False,
        },
        'django.server': {
            'handlers': ['console', 'default'],
            'level': LOG_LEVEL,
            'propagate': False,
        },
        'mylogger': {
            'handlers': ['console', 'myhandler'],
            'level': LOG_LEVEL,
        },
    }
}
  • formatters
    定义日志输出的具体格式
  • handlers
    定义如何处理日志,比如发送到文件,console等
  • filters
    定义一个日志记录是否发送到handler
  • loggers
    记录日志接口,供代码使用

How to use logging

import logging

# logger = logging.getLogger(str('mylogger'))
logger = logging.getLogger(__name__)  # 用__name__通用,自动检测

def test():
	logger.debug()
	logger.info()
	logger.warning()
	logger.error()
	logger.critical()
	#...
@plynte plynte closed this as completed Dec 21, 2017
@plynte plynte reopened this Dec 21, 2017
@plynte plynte closed this as completed Dec 21, 2017
@plynte plynte reopened this Dec 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant