Last active
February 15, 2019 19:25
-
-
Save danizen/bc86c2af9e6413a24985ea227f447a4a to your computer and use it in GitHub Desktop.
Towards simple settings for logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Example settings wodge for standard configuration | |
LOGGING_CONFIG = 'nlm.occs.logging.configure_logging' | |
# The above compiles to our current "standard" wodge, except: | |
# - all "local" apps become installed apps | |
# - the level for "local" apps becomes automatically either 'DEBUG' if settings.DEBUG else 'INFO' | |
LOGGING = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'full': { | |
'format': '[%(asctime)s] %(levelname)s %(name)s %(message)s', | |
}, | |
}, | |
'filters': { | |
'require_debug_true_or_tty': { | |
'()': 'nlm.occs.logging.DebugTrueOrTTYFilter', | |
}, | |
'require_debug_false': { | |
'()': 'django.utils.log.RequireDebugFalse', | |
}, | |
'skip_nessus_scan_requests': { | |
'()': 'nlm.occs.logging.SkipNessusScanFilter', | |
} | |
}, | |
'handlers': { | |
'file': { | |
'formatter': 'full', | |
'level': 'DEBUG', | |
'class': 'logging.FileHandler', | |
'encoding': 'UTF-8', | |
'filename': os.path.join(os.environ.get('LOG_DIR', 'logs'), 'occsdjango.log'), | |
}, | |
'console': { | |
'formatter': 'full', | |
'level': 'DEBUG', | |
'class': 'logging.StreamHandler', | |
'filters': ['require_debug_true_or_tty'], | |
}, | |
'mail_admins': { | |
'level': 'ERROR', | |
'filters': ['require_debug_false', 'skip_nessus_scan_requests'], | |
'class': 'django.utils.log.AdminEmailHandler', | |
'include_html': True, | |
}, | |
}, | |
'loggers': { | |
'nlm.occs': { | |
'handlers': ['file'], | |
'level': 'INFO', | |
'propagate': True, | |
}, | |
'django': { | |
'handlers': ['file', 'console'], | |
'level': 'INFO', | |
'propagate': True, | |
}, | |
# NOTE: All local apps are added by LOGGING_CONFIG above, and their level is set based | |
# on settings.DEBUG to 'DEBUG' or 'INFO' automatically. | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## nlm.occs.logging.configure_logging only accepts the following keys in LOGGING | |
# logdir | |
# filename | |
# full_format | |
# debug_sql | |
# mail_admins | |
# extra_loggers | |
# | |
# So, the following will raise ImproperlyConfigured with a helpful messsage | |
# | |
LOGGING_CONFIG = 'nlm.occs.logging.configure_logging' | |
LOGGING = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'full': { | |
'format': '[%(asctime)s] %(levelname)s %(name)s %(message)s', | |
}, | |
}, | |
'filters': { | |
'require_debug_true_or_tty': { | |
'()': 'infrastructure.log.DebugTrueOrTTYFilter', | |
}, | |
'require_debug_false': { | |
'()': 'django.utils.log.RequireDebugFalse', | |
}, | |
'skip_nessus_scan_requests': { | |
'()': 'infrastructure.log.SkipNessusScanFilter', | |
} | |
}, | |
'handlers': { | |
'file': { | |
'formatter': 'full', | |
'level': 'DEBUG', | |
'class': 'logging.FileHandler', | |
'encoding': 'UTF-8', | |
'filename': os.path.join(LOG_DIR, LOG_FILENAME), | |
}, | |
'console': { | |
'formatter': 'full', | |
'level': 'DEBUG', | |
'class': 'logging.StreamHandler', | |
'filters': ['require_debug_true_or_tty'], | |
}, | |
'mail_admins': { | |
'level': 'ERROR', | |
'filters': ['require_debug_false', 'skip_nessus_scan_requests'], | |
'class': 'django.utils.log.AdminEmailHandler', | |
'include_html': True, | |
}, | |
}, | |
'loggers': { | |
'nlm.occs': { | |
'handlers': ['file' ], | |
'level': 'INFO', | |
'propagate': True, | |
}, | |
'django': { | |
'handlers': ['file', 'console'], | |
'level': 'INFO', | |
'propagate': True, | |
}, | |
'cfmedicine': { | |
'handlers': ['file', 'console'], | |
'level': 'INFO', | |
'propagate': True, | |
} | |
}, | |
} | |
## |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Example settings wodge to explain what things do: | |
LOGGING_CONFIG = 'nlm.occs.logging.configure_logging' | |
LOGGING = { | |
'full_format': '[%(asctime)s] %(levelname)s %(name)s %(message)s', # Override the format used for file and stream above | |
'logdir': ...., # self-explanatory, overrides DJANGO_LOG_DIR | |
'filename': ...., # changes log-name from standard "occsdjango.log' | |
'mail_admins': True, # Changes the wodge to what is below | |
} | |
# This translates a bit differently: | |
LOGGING = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'full': { | |
'format': full_format, # Argument passed through LOGGING = | |
}, | |
}, | |
'filters': { | |
'require_debug_true_or_tty': { | |
'()': 'nlm.occs.logging.DebugTrueOrTTYFilter', | |
}, | |
'require_debug_false': { | |
'()': 'django.utils.log.RequireDebugFalse', | |
}, | |
'skip_nessus_scan_requests': { | |
'()': 'nlm.occs.logging.SkipNessusScanFilter', | |
} | |
}, | |
'handlers': { | |
'file': { | |
'formatter': 'full', | |
'level': 'DEBUG', | |
'class': 'logging.FileHandler', | |
'encoding': 'UTF-8', | |
'filename': os.path.join(logdir, filename), | |
}, | |
'console': { | |
'formatter': 'full', | |
'level': 'DEBUG', | |
'class': 'logging.StreamHandler', | |
'filters': ['require_debug_true_or_tty'], | |
}, | |
'mail_admins': { | |
'level': 'ERROR', | |
'filters': ['require_debug_false', 'skip_nessus_scan_requests'], | |
'class': 'django.utils.log.AdminEmailHandler', | |
'include_html': True, | |
}, | |
}, | |
'loggers': { | |
'nlm.occs': { | |
'handlers': ['file', 'mail_admins'], # Because of argument mail_admins True | |
'level': 'INFO', | |
'propagate': True, | |
}, | |
'django': { | |
'handlers': ['file', 'console', 'mail_admins'], # Because of argument mail_admins True | |
'level': 'INFO', | |
'propagate': True, | |
}, | |
# NOTE: All local apps are added by LOGGING_CONFIG above | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## extra_loggers may be passed through, and debug_sql is also supported | |
LOGGING_CONFIG = 'nlm.occs.logging.configure_logging' | |
LOGGING = { | |
'debug_sql': True, | |
# These are added to the configuration | |
'extra_loggers': { | |
'django_cas_ng': { | |
'handlers': ['console'], | |
'level': 'DEBUG', | |
'propagate': True, | |
}, | |
'cas': { | |
'handlers': ['console'], | |
'level': 'DEBUG', | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# Here, we still have nlm.occs.logging.configure_logging, but rather than attempt to discover locally installed apps, | |
# we instead assume that all local logging is done under 'nlm.occs.app', and provide a helper. So, the job of the code | |
# is simpler | |
LOGGING_CONFIG = 'nlm.occs.logging.configure_logging' | |
# Translation does not include local apps, because users are expected to get a logger like this: | |
from nlm.occs.logging import getLogger | |
logger = getLogger(__name__) | |
# The implementation is simple | |
def getLoggir(name=None): | |
return logging.getLogger('nlm.occs.app.%s' % name if name else 'nlm.occs.app') | |
# The user can just log from 'nlm.occs.app', and it works, but if he/she wants to know where it came from, that works too. | |
# And so the logging wodge is translated without trying to find local apps. The code to build this is not so hard, right: | |
try: | |
config = LoggingConfig(**logging_settings) | |
except: | |
raise ImproperlyConfigured(...) | |
# This *is* more complicated because of config.debug_sql, etc. | |
loggers = { | |
'nlm.occs': { | |
'handlers': ['file'], | |
'level': 'INFO', | |
'propagate': True, | |
}, | |
'django': { | |
'handlers': ['file', 'console'], | |
'level': 'INFO', | |
'propagate': True, | |
} | |
} | |
# This part isn't complicated | |
loggers.update(logging_config.extra_loggers) | |
realdict = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'full': { | |
'format': config.full_format, | |
}, | |
}, | |
'filters': { | |
'require_debug_true_or_tty': { | |
'()': 'nlm.occs.logging.DebugTrueOrTTYFilter', | |
}, | |
'require_debug_false': { | |
'()': 'django.utils.log.RequireDebugFalse', | |
}, | |
'skip_nessus_scan_requests': { | |
'()': 'nlm.occs.logging.SkipNessusScanFilter', | |
} | |
}, | |
'handlers': { | |
'file': { | |
'formatter': 'full', | |
'level': 'DEBUG', | |
'class': 'logging.FileHandler', | |
'encoding': 'UTF-8', | |
'filename': config.log_file_path, | |
}, | |
'console': { | |
'formatter': 'full', | |
'level': 'DEBUG', | |
'class': 'logging.StreamHandler', | |
'filters': ['require_debug_true_or_tty'], | |
}, | |
'mail_admins': { | |
'level': 'ERROR', | |
'filters': ['require_debug_false', 'skip_nessus_scan_requests'], | |
'class': 'django.utils.log.AdminEmailHandler', | |
'include_html': True, | |
}, | |
}, | |
'loggers': loggers, | |
} | |
logging.dictConfig(realdict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment