Django: pass variable to enter settings file

I am trying to add a variable to my log line via settings.py file.

This is the code in the settings (part of the registration):

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
                        'level': 'CRITICAL',
                         'class': 'django.utils.log.AdminEmailHandler'
                        },

        'customhandler':{
                        'level':'DEBUG',
                        'class':'logging.RotatingFileHandler',
                        'formatter':'custom_format',
                        'filename':LOG_LOCATION
                        },
                 },

     'loggers': {
         'django.request': {
                        'handlers': ['mail_admins'],
                         'level': 'CRITICAL',
                        'propagate': True,
                            },
         'Logger_Custom1': {
                        'handlers':['customhandler'],
                        'level':'DEBUG',
                        'propagate':True
                           },
                 },

    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
                     },
         'simple': {
            'format': '%(levelname)s %(message)s'
                     },
         'custom_format':{
            'format':'[%(asctime)s %(levelname)s T:%(threadName)s F:%(funcName)s ] %(message)s '
                         },
                 }
}

The above code works fine, but now I would like each log message to have a variable at the end. Sort of:

MyVariable = "Somelines" 
[%(asctime)s %(levelname)s T:%(threadName)s F:%(funcName)s ] %(message)s 'MyVariable

That way, my log will have this variable content at the end of each log line. I know that we can do this inside a function like this: logging.warning('% before you %','Look','Leap')But it will require that we include this line everywhere separately. In addition, when we need to add or change this variable name, we will need to change this line everywhere in every file.

, settings.py, .

+5
2

. , , .

, , :

testvar = "MyVariable"

, :

'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s ' + testvar

, . . , , .

+3

, , , . . , - . " " , , .

'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s and look, custom stuff here!!!'

..

custom = 'and look, custom stuff here!!!'
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s ' + custom
+1

All Articles