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


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

Re: How to export a logging level?

Started byJean-Michel Pichavant <jeanmichel@sequans.com>
First post2012-09-25 19:47 +0200
Last post2012-09-25 19:47 +0200
Articles 1 — 1 participant

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


Contents

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

#30115 — Re: How to export a logging level?

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2012-09-25 19:47 +0200
SubjectRe: How to export a logging level?
Message-ID<mailman.1362.1348595229.27098.python-list@python.org>
----- 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

[toc] | [standalone]


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


csiph-web