python logging module problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ritesh Raj Sarraf

    #1

    python logging module problem

    import os, sys, logging

    logger = logging.getLogg er("my_app")

    conerr = logging.StreamH andler(sys.stde rr)
    conerr.setLevel (logging.DEBUG)
    conerr_formatte r = logging.Formatt er('%(levelname )s %(message)s')
    conerr.setForma tter(conerr_for matter)

    console = logging.StreamH andler(sys.stdo ut)
    console.setLeve l(logging.INFO)
    console_formatt er = logging.Formatt er('%(message)s ')
    console.setForm atter(console_f ormatter)

    logger.addHandl er(conerr)
    logger.addHandl er(console)

    logger.info("Ri tesh Raj Sarraf.\n")
    logger.warning( "Ricky Raj Sarraf.\n")

    Hi,

    When I execute the above code, logger.info()'s messages don't get
    displayed. And logger.warning( )'s messages get displayed twice.

    C:\Eclipse\Work space\Python Fun>python log.py
    WARNING Ricky Raj Sarraf.

    Ricky Raj Sarraf.


    Is there something I am doing wrong ?
    I basically want to use Python's logging module for my entire program.
    I want is something like logger.message( ) which would contain normal
    program messages which shouldbe passed to stdout.

    I also want to implement a logger.verbose( ) handler which would execute
    when we enable verbose mode.

    Am I doing it the correct way ? Or am I using the wrong tool ? Should
    logging be used for it ?

    TIA,
    Ritesh

  • Ritesh Raj Sarraf

    #2
    Re: python logging module problem

    Ritesh Raj Sarraf wrote:
    import os, sys, logging
    >
    logger = logging.getLogg er("my_app")
    >

    I tried this code:

    import logging, sys

    # set up logging to file - see previous section for more details
    logging.basicCo nfig(level=logg ing.DEBUG,
    format='%(ascti me)s %(name)-12s %(levelname)-8s
    %(message)s',
    stream=sys.stde rr)

    # define a Handler which writes INFO messages or higher to the
    sys.stderr
    console = logging.StreamH andler(sys.stdo ut)
    console.setLeve l(logging.INFO)
    # set a format which is simpler for console use
    formatter = logging.Formatt er('%(message)s ')
    # tell the handler to use this format
    console.setForm atter(formatter )
    # add the handler to the root logger
    #logging.getLog ger('').addHand ler(console)
    logging.RootLog ger(console)#.a ddHandler(conso le)

    # Now, we can log to the root logger, or any other logger. First the
    root...
    logging.info('J ackdaws love my big sphinx of quartz.')
    logging.debug(' Ritesh raj Sarraf.\n')


    With this it seems to be working halfway.
    logging.debug() works perfect. But logging.info() is inheriting the
    settings of logging.debug() . For example it is using logging.debug() 's
    formatter while displaying. :-(

    Ritesh

    Comment

    • Vinay Sajip

      #3
      Re: python logging module problem

      Ritesh Raj Sarraf wrote:
      When I execute the above code, logger.info()'s messages don't get
      displayed. And logger.warning( )'s messages get displayed twice.
      >
      The warning messages are displayed twice because you have two handlers
      which both output to the console.

      The reason you don't get the info messages is that you haven't set a
      level on the logger, so the default of WARNING is used.

      It's usual to rely on logger levels and to set handler levels for
      additional refinement of what goes to a particular handler's
      destination.

      Regards,

      Vinay Sajip

      Comment

      • Ritesh Raj Sarraf

        #4
        Re: python logging module problem


        Vinay Sajip wrote:
        >
        It's usual to rely on logger levels and to set handler levels for
        additional refinement of what goes to a particular handler's
        destination.
        >
        The problem is that for StreamHandler, logging module logs to
        sys.stderr.
        I want to use the logging feature for most of the messages my program
        displays.

        So some messages would be going to sys.stdout and some to sys.stderr.

        So, I'm ended up creating two handlers, one for sys.stdout and the
        other for sys.stderr.
        I'd then make INFO level messages go to sys.stdout and DEBUG level
        messages go to sys.stderr.

        What do you suggest ??
        Is it good doing this way ?

        Ritesh

        Comment

        • Peter Otten

          #5
          Re: python logging module problem

          Ritesh Raj Sarraf wrote:
          Vinay Sajip wrote:
          >It's usual to rely on logger levels and to set handler levels for
          >additional refinement of what goes to a particular handler's
          >destination.
          The problem is that for StreamHandler, logging module logs to
          sys.stderr.
          I want to use the logging feature for most of the messages my program
          displays.
          >
          So some messages would be going to sys.stdout and some to sys.stderr.
          >
          So, I'm ended up creating two handlers, one for sys.stdout and the
          other for sys.stderr.
          I'd then make INFO level messages go to sys.stdout and DEBUG level
          messages go to sys.stderr.
          >
          What do you suggest ??
          Is it good doing this way ?
          You can achieve the desired behaviour by adding a custom Filter:

          import sys
          import logging

          logger = logging.getLogg er("my_app")
          logger.setLevel (logging.DEBUG)

          class LevelFilter(log ging.Filter):
          def __init__(self, level):
          self.level = level
          def filter(self, record):
          return self.level == record.levelno

          def make_handler(ou tstream, format, level):
          handler = logging.StreamH andler(outstrea m)
          formatter = logging.Formatt er(format)
          handler.setForm atter(formatter )
          handler.addFilt er(LevelFilter( level))
          return handler

          logger.addHandl er(make_handler (sys.stderr,
          'STDERR %(levelname)s %(message)s', logging.WARN))
          logger.addHandl er(make_handler (sys.stdout,
          'STDOUT %(levelname)s %(message)s', logging.INFO))

          logger.info("th e world is flat")
          logger.warning( "take care not to fall off its rim")

          Not sure whether this is a good idea. Another way might be to use distinct
          loggers.

          Peter

          Comment

          • Ritesh Raj Sarraf

            #6
            Re: python logging module problem


            Peter Otten wrote:
            You can achieve the desired behaviour by adding a custom Filter:
            >
            import sys
            import logging
            >
            logger = logging.getLogg er("my_app")
            logger.setLevel (logging.DEBUG)
            >
            class LevelFilter(log ging.Filter):
            def __init__(self, level):
            self.level = level
            def filter(self, record):
            return self.level == record.levelno
            >
            def make_handler(ou tstream, format, level):
            handler = logging.StreamH andler(outstrea m)
            formatter = logging.Formatt er(format)
            handler.setForm atter(formatter )
            handler.addFilt er(LevelFilter( level))
            return handler
            >
            logger.addHandl er(make_handler (sys.stderr,
            'STDERR %(levelname)s %(message)s', logging.WARN))
            logger.addHandl er(make_handler (sys.stdout,
            'STDOUT %(levelname)s %(message)s', logging.INFO))
            >
            logger.info("th e world is flat")
            logger.warning( "take care not to fall off its rim")
            >
            Not sure whether this is a good idea. Another way might be to use distinct
            loggers.
            >
            Peter
            Thanks. This looks similar to what I wanted. I'll try customizing it to
            my requirements and see if this helps.

            Thanks,
            Ritesh

            Comment

            Working...