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

Discussion in 'Python' started by srimanthula.radhakrishna@gmail.com, Aug 30, 2012.

  1. Guest

    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?
    , Aug 30, 2012
    #1
    1. Advertising

  2. Guest

    Re: Python Logging: Specifying converter attribute of a log formatterin config file

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



    On Thursday, August 30, 2012 6:38:27 AM UTC-4, Radha Krishna Srimanthula wrote:
    > 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?
    , Aug 31, 2012
    #2
    1. Advertising

  3. Vinay Sajip Guest

    Re: Python Logging: Specifying converter attribute of a log formatterin config file

    On Thursday, August 30, 2012 11:38:27 AM UTC+1, Radha Krishna Srimanthula wrote:
    >
    > 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.
    Vinay Sajip, Sep 16, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gregor Horvath

    Logging: Formatter: name of the function

    Gregor Horvath, Dec 23, 2005, in forum: Python
    Replies:
    2
    Views:
    354
    Sylvain Defresne
    Dec 23, 2005
  2. Royan
    Replies:
    1
    Views:
    1,409
    Andrea Francia
    Apr 1, 2008
  3. A.M-SG

    Switching from XML formatter to Binary Formatter

    A.M-SG, Nov 21, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    313
    Steven Cheng[MSFT]
    Nov 22, 2005
  4. Charlie Martin

    Peculiarity of '@' in logging.Formatter

    Charlie Martin, Nov 21, 2011, in forum: Python
    Replies:
    3
    Views:
    159
    Vinay Sajip
    Nov 23, 2011
  5. Tiaburn Stedd

    Strange logging.Formatter behaviour

    Tiaburn Stedd, Nov 22, 2011, in forum: Python
    Replies:
    1
    Views:
    147
    Vinay Sajip
    Nov 22, 2011
Loading...

Share This Page