Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32025 > unrolled thread

[SOLVED] Re: Making `logging.basicConfig` log to *both* `sys.stderr` and `sys.stdout`?

Started byDaniel Dehennin <daniel.dehennin@ac-dijon.fr>
First post2012-10-24 11:32 +0200
Last post2012-10-24 11:32 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [SOLVED] Re: Making `logging.basicConfig` log to *both* `sys.stderr` and `sys.stdout`? Daniel Dehennin <daniel.dehennin@ac-dijon.fr> - 2012-10-24 11:32 +0200

#32025 — [SOLVED] Re: Making `logging.basicConfig` log to *both* `sys.stderr` and `sys.stdout`?

FromDaniel Dehennin <daniel.dehennin@ac-dijon.fr>
Date2012-10-24 11:32 +0200
Subject[SOLVED] Re: Making `logging.basicConfig` log to *both* `sys.stderr` and `sys.stdout`?
Message-ID<mailman.2745.1351071158.27098.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Daniel Dehennin <Daniel.Dehennin@ac-dijon.fr> writes:

> Hello,

Hi


[...]

> I tried to setup the same for my scripts with
> "logging.config.dictConfig()" without much success, I want to limit
> output to stdout to INFO, everything bellow INFO should be sent to
> stderr instead.
>
> Any hints?

I finally find a solution for my script, I added a filter.

Regards.

#+begin_src python
class UniqLevelFilter(logging.Filter):
    def __init__(self, level):
        self._level = level

    def filter(self, rec):
        return rec.levelno == self._level

def init_logging(options):
    # TODO: color stderr messages by level if sys.stderr.isatty()
    # http://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output
    # http://plumberjack.blogspot.fr/2010/12/colorizing-logging-output-in-terminals.html
    config = { 'version' : 1,
               'filters' : { 'stdout' : { '()' : '__main__.UniqLevelFilter',
                                          'level' : logging.INFO, },
                },
               'formatters' : { 'stdout' : { 'format' : '%(message)s',
                                             'datefmt' : '', },
                                'stderr' : { 'format' : '%(message)s',
                                             'datefmt' : '', },
                },
               'handlers' : { 'stdout' : { 'class' : 'logging.StreamHandler',
                                           'stream' : 'ext://sys.stdout',
                                           'level' : 'INFO',
                                           'filters' : ['stdout'],
                                           'formatter' : 'stdout', },
                              'stderr' : { 'class' : 'logging.StreamHandler', 
                                           'stream' : 'ext://sys.stderr',
                                           'level' : 'WARNING', 
                                           'formatter' : 'stderr', }
                },
               'root' : { 'level' : options.log_level.upper(),
                          'handlers' : ['stdout', 'stderr'],
                },
    }

    logging.config.dictConfig( config )
    return logging.getLogger()

#+end_src

-- 
Daniel Dehennin
EOLE

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web