logging magic

Discussion in 'Python' started by nicholas.farrell@gmail.com, Jun 12, 2006.

  1. Guest

    Hi everyone.

    I've just been trying to add a bit more granularity to my logging code,
    as the jump from INFO (level 20) to DEBUG (level 10) is a bit too big.
    I was thinking of borrowing a few levels from java: fine (18), finer
    (16) and finest(14).

    This is what I've tried:

    log = logging.getLogger(appName)

    logging.FINE = 18
    logging.FINER = 16
    logging.FINEST = 14

    log.fine = lambda msg, self=log, level=logging.FINE: self.log(level,
    msg)
    log.finer = lambda msg, self=log, level=logging.FINER: self.log(level,
    msg)
    log.finest = lambda msg, self=log, level=logging.FINEST:
    self.log(level, msg)

    I could expand this to support kwargs, etc, but that's the least of my
    problems.

    The real issue is that by adding this extra method into the stack, the
    logging module incorrectly reports the module name/line number. I
    assume it's because logging.log(...) accesses the stack to find out
    where it was being invoked from.

    Does anyone know I can add such methods to my log object and still have
    it correctly report the context of the log message?

    Thanks to all in advance.
     
    , Jun 12, 2006
    #1
    1. Advertising

  2. Vinay Sajip Guest

    wrote:

    > Hi everyone.
    >
    > I've just been trying to add a bit more granularity to my logging code,
    > as the jump from INFO (level 20) to DEBUG (level 10) is a bit too big.
    > I was thinking of borrowing a few levels from java: fine (18), finer
    > (16) and finest(14).
    >
    > This is what I've tried:
    >
    > log = logging.getLogger(appName)
    >
    > logging.FINE = 18
    > logging.FINER = 16
    > logging.FINEST = 14
    >
    > log.fine = lambda msg, self=log, level=logging.FINE: self.log(level,
    > msg)
    > log.finer = lambda msg, self=log, level=logging.FINER: self.log(level,
    > msg)
    > log.finest = lambda msg, self=log, level=logging.FINEST:
    > self.log(level, msg)
    >
    > I could expand this to support kwargs, etc, but that's the least of my
    > problems.
    >
    > The real issue is that by adding this extra method into the stack, the
    > logging module incorrectly reports the module name/line number. I
    > assume it's because logging.log(...) accesses the stack to find out
    > where it was being invoked from.
    >
    > Does anyone know I can add such methods to my log object and still have
    > it correctly report the context of the log message?
    >
    > Thanks to all in advance.


    Try using addLevelName (this will ensure that your level names are
    correctly formatted in the log), and also try creating your own Logger
    subclass which has the custom methods you want.

    The logging system does walk up the stack, looking for a stack frame
    whose corresponding source file is not the logging package itself. What
    is the module name/file number which gets printed?

    Regards,

    Vinay Sajip
     
    Vinay Sajip, Jun 14, 2006
    #2
    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. Stefan Siegl
    Replies:
    0
    Views:
    969
    Stefan Siegl
    Aug 27, 2003
  2. janne
    Replies:
    0
    Views:
    9,542
    janne
    Sep 10, 2004
  3. Christoph Haas
    Replies:
    0
    Views:
    468
    Christoph Haas
    Jun 12, 2006
  4. Christoph Haas
    Replies:
    1
    Views:
    469
    Vinay Sajip
    Jun 14, 2006
  5. Giles Bowkett
    Replies:
    9
    Views:
    417
    Giles Bowkett
    Dec 17, 2007
Loading...

Share This Page