18.6. Logging Config File
logging.config.fileConfig(fname, defaults=None, disable_existing_loggers=True, encoding=None)
https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig
18.6.1. DictConfig
logging.config.dictConfig(config)
https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig
https://docs.python.org/3/library/logging.config.html#dictionary-schema-details
Format
- Descriptionfilename
- Specifies that a FileHandler be created, using the specified filename, rather than a StreamHandlerfilemode
- If filename is specified, open the file in this mode. Defaults to 'a'format
- Use the specified format string for the handlerdatefmt
- Use the specified date/time format, as accepted by time.strftime()style
- If format is specified, use this style for the format string. One of '%', '{' or '$' for printf-style, str.format() or string.Template respectively. Defaults to '%'level
- Set the root logger level to the specified levelstream
- Use the specified stream to initialize the StreamHandler. Note that this argument is incompatible with filename - if both are present, a ValueError is raisedhandlers
- If specified, this should be an iterable of already created handlers to add to the root logger. Any handlers which don't already have a formatter set will be assigned the default formatter created in this function. Note that this argument is incompatible with filename or stream - if both are present, a ValueError is raised
{
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '{asctime} [{levelname}] {name}: {message}',
'style': '{',
},
},
'handlers': {
'default': {
'level': 'INFO',
'formatter': 'standard',
'class': 'logging.StreamHandler',
},
},
'loggers': {
'': {
'handlers': ['default'],
'level': 'INFO',
'propagate': True
},
'django.request': {
'handlers': ['default'],
'level': 'WARN',
'propagate': False
},
}
}
18.6.2. Ini
[loggers]
keys=root,simpleExample
[handlers]
keys=consoleHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
18.6.3. Yaml
version: 1
formatters:
simple:
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
loggers:
simpleExample:
level: DEBUG
handlers: [console]
propagate: no
root:
level: DEBUG
handlers: [console]
18.6.4. Use Case - 0x01
File myapp/log.py
import logging
class LogFormatter(logging.Formatter):
COLORS = {
'CRITICAL': '\033[91;1m',
'ERROR': '\033[91m',
'INFO': '\033[36m',
'WARNING': '\033[33m',
'DEBUG': '\033[32m',
'RESET': '\033[0m'
}
def format(self, record):
message = super().format(record)
reset = self.COLORS['RESET']
color = self.COLORS[record.levelname]
return f"{color}{message}{reset}"
File myapp/settings.py
:
import os
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'colored': {
'()': 'myapp.mog.LogFormatter',
'format': "<{name}> [{levelname}] {message}",
'style': '{',
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'colored',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': os.getenv('MYAPP_LOG_LEVEL', default='DEBUG'),
},
'django.db.backends': {
'handlers': ['console'],
'level': os.getenv('MYAPP_LOG_LEVEL', default='DEBUG'),
},
'django.server': {
'handlers': ['console'],
'level': os.getenv('MYAPP_LOG_LEVEL', default='DEBUG'),
},
'django.request': {
'handlers': ['console'],
'level': os.getenv('MYAPP_LOG_LEVEL', default='DEBUG'),
},
'django.utils.security': {
'handlers': ['console'],
'level': os.getenv('MYAPP_LOG_LEVEL', default='DEBUG'),
},
'django.utils.autoreload': {
'handlers': ['console'],
'level': 'ERROR',
},
},
}