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


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

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

Started byDaniel Dehennin <daniel.dehennin@ac-dijon.fr>
First post2012-10-23 13:04 +0200
Last post2012-10-23 13:04 +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

  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

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

FromDaniel Dehennin <daniel.dehennin@ac-dijon.fr>
Date2012-10-23 13:04 +0200
SubjectRe: Making `logging.basicConfig` log to *both* `sys.stderr` and `sys.stdout`?
Message-ID<mailman.2659.1350990282.27098.python-list@python.org>

[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

[toc] | [standalone]


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


csiph-web