Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85791 > unrolled thread
| Started by | Didymus <lynto28@gmail.com> |
|---|---|
| First post | 2015-02-18 05:49 -0800 |
| Last post | 2015-02-20 05:29 -0800 |
| Articles | 5 — 2 participants |
Back to article view | Back to comp.lang.python
Logging with Custom Levels not working Didymus <lynto28@gmail.com> - 2015-02-18 05:49 -0800
Re: Logging with Custom Levels not working Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-18 13:15 -0700
Re: Logging with Custom Levels not working Didymus <lynto28@gmail.com> - 2015-02-19 10:16 -0800
Re: Logging with Custom Levels not working Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-20 02:16 -0700
Re: Logging with Custom Levels not working Didymus <lynto28@gmail.com> - 2015-02-20 05:29 -0800
| From | Didymus <lynto28@gmail.com> |
|---|---|
| Date | 2015-02-18 05:49 -0800 |
| Subject | Logging with Custom Levels not working |
| Message-ID | <24fc6e1c-1db3-41e0-84df-18b0ca5a8c69@googlegroups.com> |
Greetings,
I've been setting up a custom logging for a project here and got some great examples of code from stackoverflow. The only problem I'm having is that no matter what I set the log level too all the messages print out...
Verbosity Level set to: 55
[PVERBOSE] Performance Verbose Level
[PDEBUG] Performance Debug Level
[PMESSAGE] Performance Message Level
[PWARNING] Performance Warning Level
[PERROR] Performance Error Level
This should only print the PERROR level, but everything in the custom levels is being printed out. Not sure what I'm doing wrong here. Any help is greatly appreciated. We are use Python 2.7.5 .Example Code below:
Thanks
Tom
#!/usr/bin/python -tt
import logging
#
# Performance Custom Message Levels 'PERROR' : 55, 'PWARNING' : 54, 'PMESSAGE': 53, 'PDEBUG' : 52, 'PVERBOSE' : 51
#
PERROR_NUM = 55
PWARNING_NUM = 54
PMESSAGE_NUM = 53
PDEBUG_NUM = 52
PVERBOSE_NUM = 51
# Performance Error...
logging.addLevelName(PERROR_NUM, "PERROR")
def perror(self, message, *args, **kws):
""" Performance Error Message Level """
# Yes, logger takes its '*args' as 'args'.
self._log(PERROR_NUM, message, args, **kws)
logging.Logger.perror = perror
# 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
# Performance Messages...
logging.addLevelName(PMESSAGE_NUM, "PMESSAGE")
def pmessage(self, message, *args, **kws):
""" Performance Info/Message Level """
# Yes, logger takes its '*args' as 'args'.
self._log(PMESSAGE_NUM, message, args, **kws)
logging.Logger.pmessage = pmessage
# Performance Debug...
logging.addLevelName(PDEBUG_NUM, "PDEBUG")
def pdebug(self, message, *args, **kws):
""" Performance Debug Message Level """
# Yes, logger takes its '*args' as 'args'.
self._log(PDEBUG_NUM, message, args, **kws)
logging.Logger.pdebug = pdebug
# Performance Verbose...
logging.addLevelName(PVERBOSE_NUM, "PVERBOSE")
def pverbose(self, message, *args, **kws):
""" Performance Verbose Message Level """
# Yes, logger takes its '*args' as 'args'.
self._log(PVERBOSE_NUM, message, args, **kws)
logging.Logger.pverbose = pverbose
#
# Setup Logging.
#
def SetLogging(verbosity):
""" Set logging file and level. """
global rootLogger
logFormatter = logging.Formatter("[%(levelname)s] %(message)s") # Output formatting for message (i.e. date/time, thread, line #, message level, message).
rootLogger = logging.getLogger()
# Add a File to log too...
fileHandler = logging.FileHandler('/tmp/log.txt')
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
# Put the message on the Console as well..
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
# Set what the logging level should be:
level = logging.getLevelName(verbosity) # This turns the level into a number, which setLevel uses or logging.INFO for "verbosity" variable.
rootLogger.setLevel(level) # This can be DEBUG, INFO, WARN, ERROR or CRITICAL to control verbosity. Custom Levels: PVERBOSE (51), PDEBUG (52), PWARNING (53) or PERROR (54)
print 'Verbosity Level set to: %s' % level
#
#
#
verbosity = 'PERROR'
SetLogging(verbosity)
rootLogger.pverbose('Performance Verbose Level')
rootLogger.pdebug('Performance Debug Level')
rootLogger.pmessage('Performance Message Level')
rootLogger.pwarning('Performance Warning Level')
rootLogger.perror('Performance Error Level')
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-02-18 13:15 -0700 |
| Message-ID | <mailman.18834.1424290566.18130.python-list@python.org> |
| In reply to | #85791 |
On Wed, Feb 18, 2015 at 6:49 AM, Didymus <lynto28@gmail.com> wrote: > def perror(self, message, *args, **kws): > """ Performance Error Message Level """ > # Yes, logger takes its '*args' as 'args'. > self._log(PERROR_NUM, message, args, **kws) > > logging.Logger.perror = perror I think you need to call self.log, not self._log. The _log method appears to assume that the level check has already been performed. You really shouldn't be calling it directly anyway, as the leading _ is an indication that the method is not part of the public API.
[toc] | [prev] | [next] | [standalone]
| From | Didymus <lynto28@gmail.com> |
|---|---|
| Date | 2015-02-19 10:16 -0800 |
| Message-ID | <674667d9-a720-4d3f-979b-3ea2c2267e8b@googlegroups.com> |
| In reply to | #85818 |
On Wednesday, February 18, 2015 at 3:16:40 PM UTC-5, Ian wrote:
> > def perror(self, message, *args, **kws):
> > """ Performance Error Message Level """
> > # Yes, logger takes its '*args' as 'args'.
> > self._log(PERROR_NUM, message, args, **kws)
> >
> > logging.Logger.perror = perror
>
> I think you need to call self.log, not self._log. The _log method
> appears to assume that the level check has already been performed. You
> really shouldn't be calling it directly anyway, as the leading _ is an
> indication that the method is not part of the public API.
Yes, I had tried that and get:
Logged from file log5.py, line 21
Traceback (most recent call last):
File "/usr/lib64/python2.7/logging/__init__.py", line 851, in emit
msg = self.format(record)
File "/usr/lib64/python2.7/logging/__init__.py", line 724, in format
return fmt.format(record)
File "/usr/lib64/python2.7/logging/__init__.py", line 464, in format
record.message = record.getMessage()
File "/usr/lib64/python2.7/logging/__init__.py", line 328, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
I also added:
ll = rootLogger.getEffectiveLevel()
which send back the correct level, but it's still printing out everything...
-Tom
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-02-20 02:16 -0700 |
| Message-ID | <mailman.18916.1424424329.18130.python-list@python.org> |
| In reply to | #85916 |
On Thu, Feb 19, 2015 at 11:16 AM, Didymus <lynto28@gmail.com> wrote: > On Wednesday, February 18, 2015 at 3:16:40 PM UTC-5, Ian wrote: >> > def perror(self, message, *args, **kws): >> > """ Performance Error Message Level """ >> > # Yes, logger takes its '*args' as 'args'. >> > self._log(PERROR_NUM, message, args, **kws) >> > >> > logging.Logger.perror = perror >> >> I think you need to call self.log, not self._log. The _log method >> appears to assume that the level check has already been performed. You >> really shouldn't be calling it directly anyway, as the leading _ is an >> indication that the method is not part of the public API. > > Yes, I had tried that and get: > > Logged from file log5.py, line 21 > Traceback (most recent call last): > File "/usr/lib64/python2.7/logging/__init__.py", line 851, in emit > msg = self.format(record) > File "/usr/lib64/python2.7/logging/__init__.py", line 724, in format > return fmt.format(record) > File "/usr/lib64/python2.7/logging/__init__.py", line 464, in format > record.message = record.getMessage() > File "/usr/lib64/python2.7/logging/__init__.py", line 328, in getMessage > msg = msg % self.args > TypeError: not all arguments converted during string formatting log and _log don't have the same function signature. Specifically, log takes *args and _log just takes args (as noted in the comment on the preceding line).
[toc] | [prev] | [next] | [standalone]
| From | Didymus <lynto28@gmail.com> |
|---|---|
| Date | 2015-02-20 05:29 -0800 |
| Message-ID | <f799a552-e33f-405a-a691-1f0ac2366440@googlegroups.com> |
| In reply to | #85960 |
On Friday, February 20, 2015 at 4:25:50 AM UTC-5, Ian wrote:
> > On Wednesday, February 18, 2015 at 3:16:40 PM UTC-5, Ian wrote:
> >> > def perror(self, message, *args, **kws):
> >> > """ Performance Error Message Level """
> >> > # Yes, logger takes its '*args' as 'args'.
> >> > self._log(PERROR_NUM, message, args, **kws)
> >> >
> >> > logging.Logger.perror = perror
> >>
> >> I think you need to call self.log, not self._log. The _log method
> >> appears to assume that the level check has already been performed. You
> >> really shouldn't be calling it directly anyway, as the leading _ is an
> >> indication that the method is not part of the public API.
> >
> > Yes, I had tried that and get:
> >
> > Logged from file log5.py, line 21
> > Traceback (most recent call last):
> > File "/usr/lib64/python2.7/logging/__init__.py", line 851, in emit
> > msg = self.format(record)
> > File "/usr/lib64/python2.7/logging/__init__.py", line 724, in format
> > return fmt.format(record)
> > File "/usr/lib64/python2.7/logging/__init__.py", line 464, in format
> > record.message = record.getMessage()
> > File "/usr/lib64/python2.7/logging/__init__.py", line 328, in getMessage
> > msg = msg % self.args
> > TypeError: not all arguments converted during string formatting
>
> log and _log don't have the same function signature. Specifically, log
> takes *args and _log just takes args (as noted in the comment on the
> preceding line).
Hi,
I found the problem. I wasn't setting the level on the Handlers. Need to have:
fileHandler.setLevel(level)
consoleHandler.setLevel(level)
Once that was added to the SetLogging() function, it was printing out the correct levels. The rootLogger.setLevel(level) wasn't getting propagated to the Handlers...
Thanks for all the help!
Tom
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web