Skip to content

Instantly share code, notes, and snippets.

@andreipak
Forked from st4lk/root_logger_settings.py
Last active August 29, 2015 14:08
Show Gist options
  • Save andreipak/703932624a508c5e5243 to your computer and use it in GitHub Desktop.
Save andreipak/703932624a508c5e5243 to your computer and use it in GitHub Desktop.
"""
Settings for root logger.
Log messages will be printed to console and also to log file (rotated, with
specified size). All log messages from used libraries will be also handled.
Three approaches for defining logging settings are used:
1. using logging classes directly (py25+, py30+)
2. using fileConfig (py26+, py30+)
3. using dictConfig (py27+, py32+)
Choose any variant as you like, but keep in mind python versions, that
will work with selected approach.
First method works on most python versions.
Description can be found here:
http://www.lexev.org/2013/python-logging-every-day/
http://www.lexev.org/en/2013/python-logging-every-day/
Avaliable logging.Formatter format args can be found here:
http://docs.python.org/2/library/logging.html#logrecord-attributes
"""
###############################################
#### LOGGING CLASS SETTINGS (py25+, py30+) ####
###############################################
#### also will work with py23, py24 without 'encoding' arg
import logging
import logging.handlers
f = logging.Formatter(fmt='%(levelname)s:%(name)s: %(message)s '
'(%(asctime)s; %(filename)s:%(lineno)d)',
datefmt="%Y-%m-%d %H:%M:%S")
handlers = [
logging.handlers.RotatingFileHandler('rotated.log', encoding='utf8',
maxBytes=100000, backupCount=1),
logging.StreamHandler()
]
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
for h in handlers:
h.setFormatter(f)
h.setLevel(logging.DEBUG)
root_logger.addHandler(h)
##############################
#### END LOGGING SETTINGS ####
##############################
####################################################
#### LOGGING FILECONFIG SETTINGS (py26+, py30+) ####
####################################################
# logging.conf contents:
"""
[loggers]
keys=root
[handlers]
keys=consoleHandler,rotateFileHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler,rotateFileHandler
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[handler_rotateFileHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=('rotated.log', 'a', 100000, 1, 'utf8')
[formatter_simpleFormatter]
format=%(levelname)s:%(name)s: %(message)s (%(asctime)s; %(filename)s:%(lineno)d)
datefmt=%Y-%m-%d %H:%M:%S
"""
import logging
import logging.config
logging.config.fileConfig('logging.conf', disable_existing_loggers=False)
#########################################
#### END LOGGING FILECONFIG SETTINGS ####
#########################################
####################################################
#### LOGGING DICTCONFIG SETTINGS (py27+, py32+) ####
####################################################
import logging
import logging.config
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(levelname)s:%(name)s: %(message)s '
'(%(asctime)s; %(filename)s:%(lineno)d)',
'datefmt': "%Y-%m-%d %H:%M:%S",
}
},
'handlers': {
'console': {
'level': 'DEBUG',
'formatter': 'standard',
'class': 'logging.StreamHandler',
},
'rotate_file': {
'level': 'DEBUG',
'formatter': 'standard',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'rotated.log',
'encoding': 'utf8',
'maxBytes': 100000,
'backupCount': 1,
}
},
'loggers': {
'': {
'handlers': ['console', 'rotate_file'],
'level': 'DEBUG',
},
}
}
logging.config.dictConfig(LOGGING)
#########################################
#### END LOGGING DICTCONFIG SETTINGS ####
#########################################
@andreipak
Copy link
Author

python code snippets

using config parser

    logging_config = config.get(config_section, 'log-config')
    if os.path.exists(logging_config):
        logging.config.fileConfig(fname=logging_config,
                disable_existing_loggers=config.getboolean(config_section, 'log-disable-existing-loggers'))

using env vars

    logging_config = os.environ.get('LOGGING_CONFIG')
    if logging_config and os.path.exists(logging_config):
         disable_existing_loggers = str(os.environ.get('DISABLE_EXISTING_LOGGERS')).lower() in ('1', 'true')
         logging.config.fileConfig(fname=logging_config, disable_existing_loggers=disable_existing_loggers)

code snippet from .ini file

[DEFAULT]
# actual log file is: log-dir + config-section + '.log'
log-dir:    /var/log/mysvc
log-max:    20000000
log-cnt:    10

log-config: false
log-disable-existing-loggers:   false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment