Python Logging: Specifying converter attribute of a log formatter inconfig file

  • Thread starter srimanthula.radhakrishna
  • Start date
S

srimanthula.radhakrishna

I'd like to have all timestamps in my log file to be UTC timestamp. When specified through code, this is done as follows:

myHandler = logging.FileHandler('mylogfile.log', 'a')
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(name)-15s:%(lineno)4s: %(message)-80s')
formatter.converter = time.gmtime

myLogger = logging.getLogger('MyApp')
myLogger.addHandler(myHandler)


I'd like to move away from the above 'in-code' configuration to a config file based mechanism.

Here's the config file section for the formatter:

[handler_MyLogHandler]
args=("mylogfile.log", "a",)
class=FileHandler
level=DEBUG
formatter=simpleFormatter

Now, how do I specify the converter attribute (time.gmtime) in the above section?
 
B

bernhard.haslhofer

I have the same problem and couldn't find a solution. It seems that converters can only be set programmatically?
 
V

Vinay Sajip

Now, how do I specify the converter attribute (time.gmtime) in the above section?

Sadly, there is no way of doing this using the configuration file, other than having e.g. a

class UTCFormatter(logging.Formatter):
converter = time.gmtime

and then using a UTCFormatter in the configuration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top