Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I log the package name in Python?

Tags:

python

logging

I'm using Pythons logging module. I want to log the full path of a message, like
"msg packagename.modulename.functionName lineNumber", but how can I get the package name of a message?

The logging configuration is:

LOGGING = {    
'formatters': {
    'simple': {
    'format': '[%(levelname)s] %(message)s [%(module)s %(funcName)s %(lineno)d]'
    },  
},
'handlers': {
'console': {
        'level':'INFO',
        'class':'logging.StreamHandler',
        'formatter':'simple',
    }
},
'loggers': {
    'develop': {
        'handlers': ['console'],
        'level': 'DEBUG',
        'propagate': True,
    },
}

}

and I get a logger like this:

logger = logging.getLogger('develop')
like image 674
remy Avatar asked Apr 18 '12 09:04

remy


1 Answers

I don't know how to get a "package name" like Java does by default, but to add the filename (which gives you just as much context), use %(pathname)s in your format string:

'format': '[%(levelname)s] %(message)s [%(pathname)s %(funcName)s %(lineno)d]'

See the documentation here: https://docs.python.org/2/library/logging.html#logrecord-attributes

like image 128
dongle man Avatar answered Sep 19 '22 02:09

dongle man