반응형
logging.config.dictConfig의 완전한 예는 어디에 있습니까?
dictConfig 를 사용하고 싶지만 문서는 사용 합니다. 와 함께 사용 된 사전의 사용 가능한 예는 어디에서 사용할 수 있습니까 dictConfig
?
여기 어때요!
LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
},
},
'handlers': {
'default': {
'level': 'INFO',
'formatter': 'standard',
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stdout', # Default is stderr
},
},
'loggers': {
'': { # root logger
'handlers': ['default'],
'level': 'INFO',
'propagate': False
},
'my.packg': {
'handlers': ['default'],
'level': 'WARNING',
'propagate': False
},
'__main__': { # if __name__ == '__main__'
'handlers': ['default'],
'level': 'DEBUG',
'propagate': False
},
}
}
용법 :
logging.config.dictConfig(LOGGING_CONFIG) # just once
log = logging.getLogger(__name__) # once in each module
log.debug("Logging is configured.")
받아 들여지는 대답은 좋습니다! 하지만 덜 복잡할까요? 로깅 모듈은 매우 강력한 기능과 문서는 많은 것이 있습니다. 그러나 처음에는 포맷 할 필요가 없습니다. 원하는 것을 제공 할 때 추가 할 수 있습니다.
예를 들면 :
import logging.config
DEFAULT_LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'loggers': {
'': {
'level': 'INFO',
},
'another.module': {
'level': 'DEBUG',
},
}
}
logging.config.dictConfig(DEFAULT_LOGGING)
logging.info('Hello, log')
아래 에서 장고 v1.11.15 기본 구성을 찾았 습니다.
DEFAULT_LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse',
},
'require_debug_true': {
'()': 'django.utils.log.RequireDebugTrue',
},
},
'formatters': {
'django.server': {
'()': 'django.utils.log.ServerFormatter',
'format': '[%(server_time)s] %(message)s',
}
},
'handlers': {
'console': {
'level': 'INFO',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
},
'django.server': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'django.server',
},
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django': {
'handlers': ['console', 'mail_admins'],
'level': 'INFO',
},
'django.server': {
'handlers': ['django.server'],
'level': 'INFO',
'propagate': False,
},
}
}
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import logging.handlers
from logging.config import dictConfig
logger = logging.getLogger(__name__)
DEFAULT_LOGGING = {
'version': 1,
'disable_existing_loggers': False,
}
def configure_logging(logfile_path):
"""
Initialize logging defaults for Project.
:param logfile_path: logfile used to the logfile
:type logfile_path: string
This function does:
- Assign INFO and DEBUG level to logger file handler and console handler
"""
dictConfig(DEFAULT_LOGGING)
default_formatter = logging.Formatter(
"[%(asctime)s] [%(levelname)s] [%(name)s] [%(funcName)s():%(lineno)s] [PID:%(process)d TID:%(thread)d] %(message)s",
"%d/%m/%Y %H:%M:%S")
file_handler = logging.handlers.RotatingFileHandler(logfile_path, maxBytes=10485760,backupCount=300, encoding='utf-8')
file_handler.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(default_formatter)
console_handler.setFormatter(default_formatter)
logging.root.setLevel(logging.DEBUG)
logging.root.addHandler(file_handler)
logging.root.addHandler(console_handler)
[31/10/2015 22:00:33] [DEBUG] [yourmodulename] [yourfunction_name():9] [PID:61314 TID:140735248744448] this is logger infomation from hello module
참고 URL : https://stackoverflow.com/questions/7507825/where-is-a-complete-example-of-logging-config-dictconfig
반응형
'IT' 카테고리의 다른 글
HTML 태그 "div"의 약어는 무엇입니까? (0) | 2020.08.12 |
---|---|
서블릿의 doGet 및 doPost (0) | 2020.08.12 |
특정 좌표에 DIV를 배치하는 방법은 무엇입니까? (0) | 2020.08.12 |
Angular.js가 프로그래밍 방식으로 양식 필드를 더티로 설정 (0) | 2020.08.12 |
임의의 순서로 행 반환 (0) | 2020.08.12 |