Package: django-automated-logging
Django Database Based Automated LoggingNotice:Most of this will be moved into a wiki.What is Django-Automated-Logging (DAL)?TL;DR: DAL is a package toautomaticallytrack changes in your project, ranging
from simple logging messages, to model changes or requests done by users.You can decide what you want to do and how.
DAL allows fine-grained customization and filtering with various methods.IntroductionDjango Fully Automated Logging -finallysolved and done properly.How to install?pip install django-automated-loggingorpoetry add django-automated-loggingWhat is the purpose?The goal of DAL is to provide an easy, accessible and DRY way to log the inner working of you applications.
Ultimately giving you the chance to easily see what is happening without excessive manual print/logging statements.The application uses minimal requirements and is performant.How does it work?The application facilitates the built-in logging mechanic
by providing a custom handler, that just needs to be added to theLOGGINGconfiguration.DAL uses native Django signals to know what is happening behind the scenes without injecting custom code.Minimal SetupYou can also configure DAL to only log to a file and not to a database.
You just need to enable DAL and not include the custom logging handler.Detailed InformationFeaturesEasy SetupExtensibleFeature-RichCompletely AutomatedBuilt-In Database LoggerNo custom code needs to be inserted into your codebaseCan capture logging messages unrelated to the package itselfOnly does what it needs to do, no extra bells and whistles.SetupInitial Configuration is via your projectssettings.pyINSTALLED_APPSappend:'automated_logging'MIDDLEWAREappend:'automated_logging.middleware.AutomatedLoggingMiddleware'LOGGINGsectionhandlersadd:'db':{'level':'INFO','class':'automated_logging.handlers.DatabaseHandler',}LOGGINGsectionloggersadd: (only required if database logging desired)'automated_logging':{'level':'INFO','handlers':['db'],'propagate':True,},'django':{'level':'INFO','handlers':['db'],'propagate':True,},execute:python manage.py migrate automated_loggingLOGGINGconfiguration details are just recommendations.ConfigurationFurther configuration can be done via the variableAUTOMATED_LOGGING. The defaults are:AUTOMATED_LOGGING={"globals":{"exclude":{"applications":["plain:contenttypes","plain:admin","plain:basehttp","glob:session*","plain:migrations",]}},"model":{"detailed_message":True,"exclude":{"applications":[],"fields":[],"models":[],"unknown":False},"loglevel":20,"mask":[],"max_age":None,"performance":False,"snapshot":False,"user_mirror":False,},"modules":["request","unspecified","model"],"request":{"data":{"content_types":["application/json"],"enabled":[],"ignore":[],"mask":["password"],"query":False,},"exclude":{"applications":[],"methods":["GET"],"status":[200],"unknown":False,},"ip":True,"loglevel":20,"max_age":None,},"unspecified":{"exclude":{"applications":[],"files":[],"unknown":False},"loglevel":20,"max_age":None,},}You can always inspect the current default configuration by doing:frompprintimportpprintfromautomated_logging.settingsimportdefaultfromautomated_logging.helpersimportnamedtuple2dictpprint(namedtuple2dict(default))Recommendation:include theglobalsapplication defaults as those modules can be particularly verbose or be duplicates.There arethreedifferent independent modules availablerequest(for request logging),unspecified(for general logging messages), andmodels(for model changes).
They can be enabled and disabled by including them in themodulesconfiguration.Theloglevelsetting indicates the severity for the logging messages sent from the module.INFO (20)orDEBUG (10)is the right call for most cases.New in 6.x.x:Saving can be batched via thebatchsetting for the handler.New in 6.x.x:Saving can be threaded bythread: Truefor the handler settings.This is highly experimentalNew in 6.x.x:every field inexcludecan be either be aglob(prefixing the string withgl:), aregex(prefixing the string withre:) or plain (prefixing the string withpl:). The default isglob.DecoratorsYou can explicitly exclude or include views/models, by using the new decorators.fromautomated_logging.decoratorsimportinclude_view,include_model,exclude_view,exclude_model@include_view(methods=None)@exclude_view(methods=[])defview(request):pass@include_model(operations=None,fields=None)@exclude_model(operations=[],fields=[])classExampleModel:passincludealwaystakes precedence overexclude, if you use multipleincludeorexcludeinstead of overwriting they willupdate/extendthe previous definition.operationscan be eithercreate,modify,delete.fieldsis a list model specific fields to be included/excluded.methodsis a list methods to be included/excluded.Class-Based ConfigurationClass-Based Configuration is done over a specific meta classLoggingIgnore. Decorators take precedence over class-based configuration, but class-based configuration takes precedence over AUTOMATED_LOGGING configuration.classExampleModel:classLoggingIgnore:complete=Falsefields=[]operations=[]as described aboveoperationsandfieldswork the same way.complete = Truemeans that a model is excluded no matter what.ChangelogVersion 6.0.0Added:batchsettings to the handlerAdded:decoratorsAdded:class-based configurationAdded:request and response bodies can now be savedAdded:regex, glob matching for settingsUpdated:settingsUpdated:modelsUpdated:to current django version (2.2, 3.0, 3.1)Updated:DAL no longer stores internal information directly, but now has a custom _meta object injected.Updated:project now uses black for formattingUpdated:internals were completely rewritten for greater maintainability and speed.Fixed:https://github.com/indietyp/django-automated-logging/issues/1Fixed:https://github.com/indietyp/django-automated-logging/issues/2Moved:max_ageis now part of thesettings.pyconfiguration.Version 5.0.0Added:maxagehandler setting to automatically remove database entries after a certain amount of time.Added:query string in requests can now be enabled/disabled (are now disabled by default)Fixed:Value and URI could be longer than 255 characters. DAL would throw an exception. This is fixed.RoadmapVersion 6.1.xarchive optionsdecorators greater flexibilitywiki -> documentationmake django-ipware optional via extrasand more!Version 7.x.ximplementation of a git like versioning interfaceVersion 8.x.xtemporary world domination