In this post I will show you how to configure logging so your application servers can send info in nicely parseable json format to a centralized server for analysis.
So first things first. Lets begin by moving the logging out of settings.py and into it own config. Why because its easier to read and edit in yaml than in python/json and it shows up great in vim/emacs. So let's replace this:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse',
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
import yaml
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
LOGGING = yaml.load(open(SITE_ROOT + '/logging.yaml', 'r'))
version: 1
disable_existing_loggers: True
filters:
require_debug_false:
(): django.utils.log.RequireDebugFalse
handlers:
mail_admins:
level: ERROR
filters: [require_debug_false]
class: django.utils.log.AdminEmailHandler
loggers:
django.request:
handlers: [mail_admins]
propagate: True
level: ERROR
class CustomJsonFormatter(jsonlogger.JsonFormatter):
def parse(self):
return eval(self._fmt)
formatters:
(): log.CustomJsonFormatter
json:
format: "'created', 'module', 'funcName', 'lineno', 'levelname', 'message'"
handlers:
syslog:
level: DEBUG
class: logging.handlers.SysLogHandler
facility: local0
formatter: json
address: [loghost, 514]
loggers:
app_name:
handlers: [syslog]
propagate: True
level: DEBUG
$ModLoad imudp
$UDPServerRun 514
$template msg,"{'%msg%\n"
local0.* /var/log/local0;msg
sudo touch /var/log/local0
sudo chown syslog.adm /var/log/local0
sudo restart rsyslog
logger -p local0.info test test
Okay now its time to test. Logging messages from your app server should now be going to the central log host but you would be the luckiest person alive if that all worked without a hitch. If it doesn't be sure to check firewall rules, security groups, stuff like that.







Be the first to comment.