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


Groups > comp.lang.python > #32025

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <daniel.dehennin@ac-dijon.fr>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.018
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'output': 0.04; "'',": 0.07; 'scripts': 0.09; 'python': 0.09; 'script,': 0.09; 'stderr': 0.09; 'stdout': 0.09; 'subject:both': 0.09; 'todo:': 0.09; 'def': 0.10; "'()'": 0.16; 'subject:log': 0.16; 'config': 0.17; 'skip:u 30': 0.17; 'subject:] ': 0.19; 'tried': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; 'instead.': 0.27; 'received:172.30': 0.29; 'writes:': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "skip:' 10": 0.30; 'daniel': 0.30; "skip:' 20": 0.32; 'info': 0.32; 'to:addr:python-list': 0.33; 'received:fr': 0.36; 'should': 0.36; 'subject:[': 0.37; 'level': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.39; 'hello,': 0.39; 'url:12': 0.40; 'success,': 0.62; 'url:blogspot': 0.64; 'limit': 0.65; 'finally': 0.66; 'regards.': 0.66; 'color': 0.69; "'class'": 0.84; 'subject: *': 0.84; 'subject:Making': 0.84; 'subject:skip:l 10': 0.84; 'info,': 0.91; 'url:fr': 0.95
From Daniel Dehennin <daniel.dehennin@ac-dijon.fr>
To python-list@python.org
Subject [SOLVED] Re: Making `logging.basicConfig` log to *both* `sys.stderr` and `sys.stdout`?
References <34939e5a-19ba-4cc5-b10b-36f8b2e42830@h9g2000vbr.googlegroups.com> <87objt5u16.fsf@daniel.eole.lan>
Organisation Eole
Date Wed, 24 Oct 2012 11:32:24 +0200
In-Reply-To <87objt5u16.fsf@daniel.eole.lan> (Daniel Dehennin's message of "Tue, 23 Oct 2012 13:04:21 +0200")
User-Agent Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.2.50 (gnu/linux)
MIME-Version 1.0
Content-Type multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2745.1351071158.27098.python-list@python.org> (permalink)
Lines 84
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351071158 news.xs4all.nl 6858 [2001:888:2000:d::a6]:37556
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32025

Show key headers only | View raw


[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

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


Thread

[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

csiph-web