Package: django-kn-defaults
kn-defaultsVision:This project shall containsA Logging helper module in the form of Middleware, Decorator, and a helper functionA Utility collection that can help with everyday tasksInstallation:You can install viapip install django-kn-defaultsComponents:Logging HelpersChecksCMS admin log signal handlerUsage:1. Logging UsageAddkn_defaults.loggingto INSTALLED_APPSHook the logging configurations. (example below)LOGGING setting Adjustments:First, please make sure you add these values to your env variablesDJANGO_PROJECT_NAME (str)DJANGO_PROJECT_ROOT (str)DJANGO_LOGSTASH_HOST (str)DJANGO_LOGSTASH_PORT (int)Below env variables are optionalDJANGO_LOGSTASH_ENV: defaults to DevDJANGO_LOGSTASH_EXTRA_PREFIX: Defaults to devDJANGO_LOGSTASH_SSL_ENABLE: defaults to FalseThen, You can integrate the kn BASE_LOGGING dict with your project LOGGING setting.fromkn_defaults.logging.defaultsimportBASE_LOGGINGBASE_LOGGING.update({# Your extra logging configurations goes here})LOGGING=BASE_LOGGINGIf you have a logging config already, you can merge it with BASE_LOGGING by hand.
Checkkn_defaults.logging.defaultsfor informationThe package have 3 logging components1. Middleware logging:To use the logging middlewareAdd'kn_defaults.logging.middlewares.KnLogging'to yourMIDDLEWAREMark your url names to be logged by the settingKN_LOGGING_URL_PATTERNSTheKN_LOGGING_URL_PATTERNSsetting is a list of the url names to be logged by  the middleware.
This list can also accept a namespace url with an*to denote "log all urls under that namespace".KN_LOGGING_URL_PATTERNS=['url_name','namespace:url_name','namespace2:*']The information being stored with the middleware loggingrequest_id : a unique if of the request to help traceback any logs associated with that specific requestmethod: GET/POST/ etc..path: the request.path (ie url) which originated the logipuser: the request.user if the user is authenticated, None otherwise.status_code: the response status codeoutbound_payload: The plain response the view sent backresponse_duration: How much time in seconds it took to generate a response back to the userpost_parameters: the POST information. This respectsDjango's sensitive parameters decorator2. Logging Helper function:Sample usage looks like thisfromkn_defaults.logging.defaultsimportloglog(level=10,msg='Message here')The helper logging is ready for use out of the box and it logs the local variables in the calling function next to
the log message.For level names here is a map.CRITICAL=50ERROR=40WARNING=30INFO=20DEBUG=103. function decorator Logging Helper:You can use it like thisfromkn_defaults.logging.defaultsimportlogging_decorator@logging_decorator(level=10,msg='')deffunction(arg_1=True,*args,**kwargs):passThis decorator logs the function *args & **kwargs and the function return valueSettings:KN_LOG_FILE_SIZEControl the log file size. Defaults to 5 MB.KN_HANDLER_CLASSControls the logging handler class, defaults to 'logging.handlers.RotatingFileHandler'KN_LOG_FILE_PATHControls where the log file would be stored. Defaults toos.path.join(os.getcwd(), 'log.log'))KN_LOG_BACKUP_COUNTControls the backup count for the default 'RotatingFileHandler'. Defaults to 32. ChecksThe package do some sanity checks regarding the existence of the logging handlers needed.It also checks that admin is not hooked to/admin/url.3. Helperscms_plugin_change_admin_loglogs django-cms plugins addition / update and delete to the regular admin log.
In case of a change action, it logs the changed fields and their values before and after.
It's automatically activated if 'cms' is in INSTALLED_APPS unless disabled by the settingDISABLE_CMS_PLUGIN_CHANGE_ADMIN_LOGCreating a releaseThe package version is controlled by kn_defaults.init.version.
and preparing the sdist is bypython setup.py sdist