Logging to zero or more destinations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • samwyse

    Logging to zero or more destinations

    In the Python 2.5 Library Reference, section 14.5.3 (Logging to
    multiple destinations), an example is given of logging to both a file
    and the console. This is done by using logging.basicCo nfig() to
    configure a log file, and then calling
    logging.getLogg er('').addHandl er(console) to add the console.

    However, in section 14.5.4 (Sending and receiving logging events
    across a network), a call is made to
    rootLogger.addH andler(socketHa ndler), and later it is stated that "On
    the client side, nothing is printed on the console".

    Finally, back in section 14.5.2 (Basic example), it's stated that "by
    default, the root logger is configured to only handle messages with a
    severity of WARNING or above. The message format is also a
    configuration default, as is the output destination of the messages -
    sys.stderr."

    The only way that I can see for all three statements to be consistent
    is that the root logger starts with an empty list of handlers, and
    doesn't instantiate a default handler until either
    logging.basicCo nfig() is called, or the first time that a message is
    logged. This would also seem to imply that there's no way to use an
    empty handler list (say, if you want to suppress all logging), because
    the root handler will instantiate a handler for you. Is this correct?

    P.S. I tried researching this further by myself, but the logging
    module doesn't come with source (apparently it's written in C?) and I
    don't have the time to find and download the source to my laptop.

    Thanks!
  • Rob Wolfe

    #2
    Re: Logging to zero or more destinations

    samwyse <samwyse@gmail. comwrites:
    In the Python 2.5 Library Reference, section 14.5.3 (Logging to
    multiple destinations), an example is given of logging to both a file
    and the console. This is done by using logging.basicCo nfig() to
    configure a log file, and then calling
    logging.getLogg er('').addHandl er(console) to add the console.
    >
    However, in section 14.5.4 (Sending and receiving logging events
    across a network), a call is made to
    rootLogger.addH andler(socketHa ndler), and later it is stated that "On
    the client side, nothing is printed on the console".
    >
    Finally, back in section 14.5.2 (Basic example), it's stated that "by
    default, the root logger is configured to only handle messages with a
    severity of WARNING or above. The message format is also a
    configuration default, as is the output destination of the messages -
    sys.stderr."
    >
    The only way that I can see for all three statements to be consistent
    is that the root logger starts with an empty list of handlers, and
    doesn't instantiate a default handler until either
    logging.basicCo nfig() is called,
    That is correct.
    or the first time that a message is
    logged.
    That is not correct. The list of handlers is empty until `basicConfig`
    or explicit `addHandler` is called.
    This would also seem to imply that there's no way to use an
    empty handler list (say, if you want to suppress all logging), because
    the root handler will instantiate a handler for you. Is this correct?
    No. Consider this:
    >>import logging
    >>logging.root. warning('error message')
    No handlers could be found for logger "root"
    >>logging.root. warning('error message')
    Note only one warning message.

    P.S. I tried researching this further by myself, but the logging
    module doesn't come with source (apparently it's written in C?) and I
    don't have the time to find and download the source to my laptop.
    Hmmm... that's strange. It is a pure Python package.

    $ ls /usr/lib/python2.5/logging/
    config.py config.pyc handlers.py handlers.pyc __init__.py __init__.pyc

    HTH,
    Rob

    Comment

    • Robert Kern

      #3
      Re: Logging to zero or more destinations

      samwyse wrote:
      In the Python 2.5 Library Reference, section 14.5.3 (Logging to
      multiple destinations), an example is given of logging to both a file
      and the console. This is done by using logging.basicCo nfig() to
      configure a log file, and then calling
      logging.getLogg er('').addHandl er(console) to add the console.
      >
      However, in section 14.5.4 (Sending and receiving logging events
      across a network), a call is made to
      rootLogger.addH andler(socketHa ndler), and later it is stated that "On
      the client side, nothing is printed on the console".
      >
      Finally, back in section 14.5.2 (Basic example), it's stated that "by
      default, the root logger is configured to only handle messages with a
      severity of WARNING or above. The message format is also a
      configuration default, as is the output destination of the messages -
      sys.stderr."
      >
      The only way that I can see for all three statements to be consistent
      is that the root logger starts with an empty list of handlers, and
      doesn't instantiate a default handler until either
      logging.basicCo nfig() is called, or the first time that a message is
      logged. This would also seem to imply that there's no way to use an
      empty handler list (say, if you want to suppress all logging), because
      the root handler will instantiate a handler for you. Is this correct?
      Sort of. Your analysis of what happens is entirely correct (see below). However,
      a way to suppress all logging is to have a single Handler where the emit()
      method does nothing. Or you can set the level above CRITICAL.

      $ python
      Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
      [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
      Type "help", "copyright" , "credits" or "license" for more information.
      >>import logging
      >>logging.warn( 'foo')
      WARNING:root:fo o
      >>logger = logging.getLogg er()
      >>logger.handle rs
      [<logging.Stream Handler instance at 0xb43788>]
      >>>
      $ python
      Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
      [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
      Type "help", "copyright" , "credits" or "license" for more information.
      >>import logging
      >>logger = logging.getLogg er()
      >>logger.handle rs
      []
      >>logging.basic Config()
      >>logger.handle rs
      [<logging.Stream Handler instance at 0xb43788>]
      >>logger.handle rs = []
      >>class NullHandler(log ging.Handler):
      .... def emit(self, record):
      .... pass
      ....
      >>logger.addHan dler(NullHandle r())
      >>logging.warn( 'foo')
      >>>
      P.S. I tried researching this further by myself, but the logging
      module doesn't come with source (apparently it's written in C?) and I
      don't have the time to find and download the source to my laptop.
      No it's all Python. Look in c:\Python25\Lib \logging\ .

      --
      Robert Kern

      "I have come to believe that the whole world is an enigma, a harmless enigma
      that is made terrible by our own mad attempt to interpret it as though it had
      an underlying truth."
      -- Umberto Eco

      Comment

      • Robert Kern

        #4
        Re: Logging to zero or more destinations

        Rob Wolfe wrote:
        samwyse <samwyse@gmail. comwrites:
        >The only way that I can see for all three statements to be consistent
        >is that the root logger starts with an empty list of handlers, and
        >doesn't instantiate a default handler until either
        >logging.basicC onfig() is called,
        >
        That is correct.
        >
        >or the first time that a message is
        >logged.
        >
        That is not correct. The list of handlers is empty until `basicConfig`
        or explicit `addHandler` is called.
        >
        >This would also seem to imply that there's no way to use an
        >empty handler list (say, if you want to suppress all logging), because
        >the root handler will instantiate a handler for you. Is this correct?
        >
        No. Consider this:
        >
        >>>import logging
        >>>logging.root .warning('error message')
        No handlers could be found for logger "root"
        >>>logging.root .warning('error message')
        >
        Note only one warning message.
        Ah, right. It is the module-level functions logging.warn(), etc. that invoke
        basicConfig() if no handler is present.

        --
        Robert Kern

        "I have come to believe that the whole world is an enigma, a harmless enigma
        that is made terrible by our own mad attempt to interpret it as though it had
        an underlying truth."
        -- Umberto Eco

        Comment

        • samwyse

          #5
          Re: Logging to zero or more destinations

          On Jul 8, 3:01 pm, Rob Wolfe <r...@smsnet.pl wrote:
          samwyse <samw...@gmail. comwrites:
          P.S.  I tried researching this further by myself, but the logging
          module doesn't come with source (apparently it's written in C?) and I
          don't have the time to find and download the source to my laptop.
          >
          Hmmm... that's strange. It is a pure Python package.
          >
          $ ls /usr/lib/python2.5/logging/
          config.py  config.pyc  handlers.py  handlers.pyc  __init__.py  __init__.pyc
          >
          HTH,
          Rob
          Oops, my bad. I was using IDLE and tried the "Open Module..." command
          on logging, not logging.somethi ng.

          Comment

          Working...