Store Django Log messages in a database?

Viewed 15615

Django (the python web framework) uses python's logging system to store logs.

Is there an easy way to store log messages in a database, and then allow admin users to look over them via the web? It's the sort of thing I could write myself, but no point re-inventing the wheel. I don't want to log exceptions, but info/debug/notice type messages that I have added to the code.

Ideally I'd like to be able to store metadata about the log message as it's done (like the remote IP address, user agent, wsgi process id, etc.), and then filter / browse based on that (i.e. show me all log messages from this IP address in the last 24 hours). Has anyone done this?

5 Answers

You can check a good solution that I posted here. You just need a string-connection to connect to your database. For example, if you use a MySQL, the connection-string should be:

# mysqlclient
'mysql+mysqldb://username:password@host:port/database'

or

# PyMySQL
'mysql+pymysql://username:password@host:port/database')

then you can use PhpMyAdmin as a "MySQL web administration tool" to look over the database via web browsers or DataGrip (my preference) to access any database remotely.


for using the handler in Django you just need to add the handler class to the LOGGING variable of setting.py as follow:

level = 'INFO' if DEBUG else 'WARNING'  # I prefer INFO in debugging mode and WARNING in production
handler = ['log_db_handler', ]  # In production I rarely check the server to see console logs
if DEBUG:
    handler.append('console')

LOGGING = {'version': 1,
           'disable_existing_loggers': False,

           'formatters': {'verbose': {'format': '{levelname} {message}',  # {asctime} {module} {process:d} {thread:d}
                                      'style': '{', }, },

           'handlers': {'log_db_handler': {'level': level,
                                           'class': 'db_logger.handlers.DBHandler',
                                           'formatter': 'verbose', },
                        'console': {'class': 'logging.StreamHandler', }},

           'loggers': {'db_log': {'handlers': handler,
                                  'level': level,
                                  'propagate': False, },
                       'django': {'handlers': handler,
                                  'level': level,
                                  'propagate': True, },
                       'django.request': {'handlers': handler,
                                          'level': level,
                                          'propagate': True, }}}

Pay attention that the 'db_logger.handlers.DBHandler' points to the handler class.

Related