14👍
10👍
Apart from the obvious choice of Sentry, for the sake of exercise, there is a nice blog article titled “Creating custom log handler that logs to database models in django” in the “James Lin Blog”, which briefly explains how to do this using a second database, with code samples.
The code is adapted from the standard Python RotatingFileHandler:
…The RotatingFileHandler allows you to specify which file to write to and rotate files, therefore my DBHandler should also allow you to specify which model to insert to and specify expiry in settings, and best of all, on a standalone app.
This could also be easily adapted for using a single db.
- [Django]-Adding new custom permissions in Django
- [Django]-Adding an attribute to the <input> tag for a django ModelForm field
- [Django]-Django – user permissions to certain views?
7👍
check django-db-logger
it takes less than a minute to integrate
- [Django]-Django: FloatField or DecimalField for Currency?
- [Django]-How can I get the URL (with protocol and domain) in Django (without request)?
- [Django]-How to create a Django queryset filter comparing two date fields in the same model
1👍
try django-requests. I’ve tried it and it basically just puts the request logs in a table called requests.
- [Django]-How do I get the current date and current time only respectively in Django?
- [Django]-Django ManyToMany filter()
- [Django]-Makemigrations doesn't detect changes in model
0👍
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.
- [Django]-Creating and saving foreign key objects using a SlugRelatedField
- [Django]-Django Rest Framework — no module named rest_framework
- [Django]-Django postgresql json field schema validation