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


Groups > comp.lang.python > #86848

Re: Logging custom level not print module properly??

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'subject:not': 0.03; '"""': 0.07; 'level,': 0.07; '*args,': 0.09; '__name__': 0.09; 'exec': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:module': 0.09; 'undefined': 0.09; 'unittest': 0.09; 'variables.': 0.09; 'python': 0.11; 'def': 0.12; 'suggest': 0.14; "'__main__':": 0.16; 'determines': 0.16; 'enough.': 0.16; 'handlers.': 0.16; 'logger': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Logging': 0.16; 'wrote:': 0.18; 'module': 0.19; 'skip:f 30': 0.19; '(the': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'skip:% 10': 0.24; 'skip:l 30': 0.24; 'file.': 0.24; "i've": 0.25; 'logging': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'thus': 0.29; "i'm": 0.30; "skip:' 10": 0.31; 'origin': 0.31; 'prints': 0.31; 'file': 0.32; 'class': 0.32; 'skip:c 30': 0.32; 'run': 0.32; 'level.': 0.33; 'skip:_ 10': 0.34; 'message.': 0.35; 'problem': 0.35; 'info': 0.35; 'common': 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'add': 0.35; 'module.': 0.36; 'picking': 0.36; "didn't": 0.36; 'method': 0.36; 'thanks': 0.36; 'hi,': 0.36; 'should': 0.36; 'too': 0.37; 'level': 0.37; 'performance': 0.37; 'checks': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'easy': 0.60; 'event.': 0.60; 'is.': 0.60; 'full': 0.61; 'matter': 0.61; 'name': 0.63; 'more': 0.64; 'levels': 0.65; 'below:': 0.68; 'containing': 0.69; 'below.': 0.71; 'inform': 0.78; 'well..': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Logging custom level not print module properly??
Date Tue, 03 Mar 2015 16:01:28 +0100
Organization None
References <6676f6f6-94bf-4ed9-9a09-46beb4317e6c@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd92a5.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.20.1425394911.21433.python-list@python.org> (permalink)
Lines 122
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1425394911 news.xs4all.nl 2902 [2001:888:2000:d::a6]:53935
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:86848

Show key headers only | View raw


Didymus wrote:

> Hi,
> 
> I have setup custom levels (with the help of the Python community) for
> logging. I set this up as a class in a module "log.py" below. The problem
> I'm seeing is that no matter the file the the logging is happening in it
> always prints the module as "log", I've rcreated the testcase below:
> 
> % python run.py
> [PWARNING log 22] Warning Message.
> [INFO run 8] Message.
> 
> The log.py:
> 
> """ Logging Module """
> import common
> import logging
> 
> # Custom
> PWARNING_NUM = 34
> 
> # Performance Warning...
> logging.addLevelName(PWARNING_NUM, "PWARNING")
> 
> def pwarning(self, message, *args, **kws):
>     """ Performance Warning Message Level """
>     # Yes, logger takes its '*args' as 'args'.
>     self.log(PWARNING_NUM, message, *args, **kws)
>         
> logging.Logger.pwarning = pwarning
> 
> class SetLogging():
> # Variables.
>     fileHandler = None
>     consoleHandler = None
>     
>     def __init__(self):
>         """ Set Verbosity Level, set which ever is True..."""
>         common.verbosity = 'INFO'
>         self.setLogHandlers()
>   
>     def setverbosity(self, verbosity_level):
>         """ Set what the logging level should be """
>         level = logging.getLevelName(verbosity_level)
>         common.rootLogger.setLevel(level)
>     
>     def setLogHandlers(self):
>         """ Set logging file and level. """
>         logFormatter =  logging.Formatter("[%(levelname)s %(module)s
>         %(lineno)d] %(message)s") common.rootLogger = logging.getLogger()
> # Add a File to log too...
>         fileHandler = logging.FileHandler(common.LOG_FILENAME)
>         fileHandler.setFormatter(logFormatter)
>         common.rootLogger.addHandler(fileHandler)
> # Put the message on the Console as well..
>         consoleHandler = logging.StreamHandler()
>         consoleHandler.setFormatter(logFormatter)
>         common.rootLogger.addHandler(consoleHandler)
>         self.setverbosity(common.verbosity)
> 
> # Main
> if __name__ == '__main__':
>     log = SetLogging()
>     log.setLogHandlers()
>     log.setverbosity('PMESSAGE')
> 
> 
> run.py:
> import common
> import log
> #
> common.LOG_FILENAME = '/tmp/runtest.log'         # Set the File the log
> will write too based on the name and time stamp.
> log = log.SetLogging()                                                    
>         # Set logging level and handlers.
> 
> common.rootLogger.pwarning('Warning Message.')
> common.rootLogger.info('Message.')
> 
> 
> common.py:
> # Logging Verbosity and File.
> #
> rootLogger = None                                                         
>     # Logger Handle, Undefined at the moment.
> LOG_FILENAME = '/tmp/python.log'                                          
>     # Log File full pathname.
> 
> 
> Not sure why the "def pwarning" isnt; picking up the module that the call
> is happening in correctly, but the standard INFO is. Thanks for any help
> in advanced.

The findCaller() method determines that your pwarning() is not part of the 
logging machinery and thus presents the file containing the pwarning() 
function as the origin of the logging event. 

I see no easy and clean way to inform findCaller() to go up one more level 
(the unittest package checks for a global flag for that purpose), but that 
may be because I didn't look hard enough.

If your usecase for custom levels is compelling I suggest that you 
override/replace findCaller() rather than go for the following hack...

exec compile('''
# Custom
PWARNING_NUM = 34

# Performance Warning...
addLevelName(PWARNING_NUM, "PWARNING")

def pwarning(self, message, *args, **kws):
    """ Performance Warning Message Level """
    # Yes, logger takes its '*args' as 'args'.
    self.log(PWARNING_NUM, message, *args, **kws)

Logger.pwarning = pwarning
''', logging.__file__.rstrip("co"), "exec") in vars(logging)

...which pretends that pwarning() is part of the logging/__init__.py module.

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


Thread

Logging custom level not print module properly?? Didymus <lynto28@gmail.com> - 2015-03-03 06:06 -0800
  Re: Logging custom level not print module properly?? Peter Otten <__peter__@web.de> - 2015-03-03 16:01 +0100
    Re: Logging custom level not print module properly?? Didymus <lynto28@gmail.com> - 2015-03-03 07:11 -0800
      Re: Logging custom level not print module properly?? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-03 08:30 -0700

csiph-web