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


Groups > comp.lang.python > #30115

Re: How to export a logging level?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.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 <prvs=608a82a30=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'configure': 0.04; 'root': 0.04; 'url:launchpad': 0.05; 'level,': 0.07; 'subject:How': 0.09; 'advice.': 0.09; 'logger': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'log.': 0.16; 'module).': 0.16; 'oqapy': 0.16; 'paqager': 0.16; 'simpler,': 0.16; 'subject:export': 0.16; 'url:oqapy': 0.16; 'url:paqager': 0.16; 'url:qarte': 0.16; 'v.v.': 0.16; '(in': 0.18; 'app': 0.19; 'module': 0.19; 'are:': 0.20; 'import': 0.21; 'skip:% 10': 0.22; 'cc:2**0': 0.23; 'work.': 0.23; 'task': 0.23; "i've": 0.23; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'logging': 0.27; 'module.': 0.27; 'lines': 0.28; 'url:mailman': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'related': 0.30; 'that.': 0.30; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; '-----': 0.32; 'url:listinfo': 0.32; 'thanks': 0.34; 'skip:l 30': 0.35; 'subject:?': 0.35; 'but': 0.36; 'url:org': 0.36; 'anything': 0.36; 'should': 0.36; 'level': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'application': 0.40; 'skip:" 10': 0.40; 'url:mail': 0.40; 'your': 0.60; 'received:194': 0.61; 'life': 0.66; 'ever.': 0.84; 'processed,': 0.84; 'worthless': 0.84
X-IronPort-AV E=Sophos;i="4.80,484,1344204000"; d="scan'208";a="752117"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Tue, 25 Sep 2012 19:47:20 +0200 (CEST)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To Vincent Vande Vyvre <vincent.vandevyvre@swing.be>
In-Reply-To <5061D8A4.40107@swing.be>
Subject Re: How to export a logging level?
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 7bit
X-Mailer Zimbra 7.2.0_GA_2669 (ZimbraWebClient - GC7 (Linux)/7.2.0_GA_2669)
Cc Python mail-list <python-list@python.org>
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.1362.1348595229.27098.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348595229 news.xs4all.nl 6900 [2001:888:2000:d::a6]:53277
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:30115

Show key headers only | View raw


----- Original Message -----
> In my application I import a module and I want to set the same
> logging
> level
> as the main app to this module.
> 
> I've tried this code
> 
> main.py
> 
> import logging
> logger = logging.getLogger(__name__)
> lvl = logging.DEBUG
> LOG_FORMAT = "%(asctime)-6s %(levelname)s: %(name)s - %(message)s"
> logging.basicConfig(format=LOG_FORMAT, datefmt='%H:%M:%S', level=lvl)
> 
> from mymodule.myfile import MyClass
> 
> ...
>     def load_myclass(self):
>         lvl = logger.getEffectiveLevel()
>         mc = MyClass(self, lvl)
> 
> 
> myfile.py
> 
> import logging
> logger = logging.getLogger(__name__)
> 
> class MyClass(object):
>     def __init__(self, main, lvl):
>         logger.setLevel(lvl)
> 
> If I set the level to logging.DEBUG I can see all infos from the main
> but anything
> from my module.
> 
> Thanks for your advice.
> --
> Vincent V.V.
> Oqapy <https://launchpad.net/oqapy> . Qarte
> <https://launchpad.net/qarte> . PaQager
> <https://launchpad.net/paqager>
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

Life is actually simpler, one rule to remember: you don't configure your loggers, ever. You let this worthless task to the user (in your case the main.py file or however import your module).

In myfile.py, the only logging related lines are:

import logging
logger = logging.getLogger(__name_)

Then you just log. How the logs  are processed, at which level, for which formatter, you don't care. The root logger will take care of that. Keep your main.py as it is and it should work.

JM

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


Thread

Re: How to export a logging level? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-09-25 19:47 +0200

csiph-web