How to set width of combined fields in Python logging -


in python logging, there way set width of 2 combined fields?

i'm looking combine filenames , line numbers produce output similar to:

2011-10-14 13:47:51 main.py:12       debug - initializing cluster 2011-10-14 13:47:51 cluster.py:364   debug - starting cluster 2011-10-14 13:47:51 cluster.py:98    info  - starting simulation 2011-10-14 13:47:51 simulation.py:79 debug - computing parameters 

how modify formatting string below, achieve this?

logging.formatter('%(asctime)s %(filename)s:%(lineno)s %(levelname)5s - %(msg)s  

update:

as @jonrsharpe points out, no out-of-the-box way this, have customize formatting.

solution: @alecxe recommends custom filter, , here complete example:

import logging import logging.config  class filenamelinenofilter(logging.filter):     def filter(self, record):         record.filename_lineno = '{}:{}'.format(record.filename, record.lineno)         return true  log_settings = {     'version': 1,     'filters': {         'filename_lineno_filter': {             '()': filenamelinenofilter,         },     },     'formatters': {         'standard': {             'format': '%(asctime)s %(filename_lineno)-18s %(levelname)5s - %(msg)s',         },     },     'handlers': {         'default': {             'level': 'debug',             'class': 'logging.streamhandler',             'filters': ['filename_lineno_filter'],             'formatter': 'standard',             },     },     'loggers': {         '': {              'handlers': ['default'],             'level': 'debug',         },     } }  logging.config.dictconfig(log_settings) logger = logging.getlogger() logger.debug('debug output goes here.') logger.info('informational goes here.') 

you can make combined field of custom filter:

import logging  class myfilter(logging.filter):     def filter(self, record):         record.filename_lineno = "%s:%d" % (record.filename, record.lineno)         return true 

then, can reference %(filename_lineno)s placeholder in formatting string.


Comments

Popular posts from this blog

android - net_scheduler holding wakelock -

sql - MySQL : Getting Entries from a many-to-many table -

java - Retrieving data from database using jsp (Hibernate + Spring + Maven) -