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


Groups > comp.lang.python > #31925

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

From Daniel Dehennin <daniel.dehennin@ac-dijon.fr>
Subject Re: Making `logging.basicConfig` log to *both* `sys.stderr` and `sys.stdout`?
References <34939e5a-19ba-4cc5-b10b-36f8b2e42830@h9g2000vbr.googlegroups.com>
Date 2012-10-23 13:04 +0200
Newsgroups comp.lang.python
Message-ID <mailman.2659.1350990282.27098.python-list@python.org> (permalink)

Show all headers | View raw


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

Hello,

>> Unfortunately this setup makes `logging.basicConfig` pretty useless.
>> However, I believe that this is something that more people could
>> benefit from. I also believe, that it just "makes sense" to send
>> warnings (and above) to `stderr`, the rest to `stdout`.

[...]

> Python 2.x is closed to feature changes, and Python 2.7 and Python 3.2
> already support flexible configuration using dictConfig() - see
> 
> http://docs.python.org/library/logging.config.html#logging.config.dictConfig
> 
> Also, Python 3.3 will support passing a list of handlers to
> basicConfig(): see
> 
> http://plumberjack.blogspot.com/2011/04/added-functionality-for-basicconfig=
> -in.html
> 
> which will allow you to do what you want quite easily.

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?

Here is my configuration:

#+begin_src python
def init_logging(options):
    # TODO: color stderr messages by level if sys.stderr.isatty()
    config = { 'version' : 1,
               'formatters' : { 'stdout' : { 'format' : '%(message)s',
                                             'datefmt' : '', },
                                'stderr' : { 'format' : '%(message)s',
                                             'datefmt' : '', },
                },
               'handlers' : { 'stdout' : { 'class' : 'logging.StreamHandler',
                                           'stream' : 'ext://sys.stdout',
                                           'level' : 'INFO',
                                           '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

Regards.
-- 
Daniel Dehennin
EOLE

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Making `logging.basicConfig` log to *both* `sys.stderr` and `sys.stdout`? Daniel Dehennin <daniel.dehennin@ac-dijon.fr> - 2012-10-23 13:04 +0200

csiph-web