Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #88397
| Path | csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <prvs=52520aaf3=jeanmichel@sequans.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.008 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; '"""': 0.07; 'debug': 0.07; 'intermediate': 0.07; 'tom': 0.07; 'advance': 0.07; '*args,': 0.09; 'filename': 0.09; 'level:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'args,': 0.16; 'logger': 0.16; 'march,': 0.16; 'subject:Logging': 0.16; 'module': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'module,': 0.24; 'skip:l 30': 0.24; 'cheers,': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; 'help!': 0.26; 'logging': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'inspect': 0.31; 'prints': 0.31; 'file': 0.32; '-----': 0.33; 'except': 0.35; 'skip:s 30': 0.35; 'thanks': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'should': 0.36; 'email addr:python.org': 0.37; 'level': 0.37; 'performance': 0.37; 'thank': 0.38; 'work?': 0.38; 'does': 0.39; 'subject:': 0.39; 'called': 0.40; 'how': 0.40; 'simple,': 0.60; 'you.': 0.62; 'information': 0.63; 'received:194': 0.64; 'different': 0.65; 'email name:python-list': 0.65; 'to:addr:gmail.com': 0.65; 'notice:': 0.67; 'person,': 0.68; 'privileged.': 0.69; 'disclose': 0.74; '*really*': 0.84; '2015': 0.84; 'medium.': 0.91 |
| X-IronPort-AV | E=Sophos;i="5.11,502,1422918000"; d="scan'208";a="4158386" |
| Date | Tue, 31 Mar 2015 19:37:13 +0200 (CEST) |
| From | Jean-Michel Pichavant <jeanmichel@sequans.com> |
| To | Didymus <lynto28@gmail.com> |
| Cc | python-list@python.org |
| In-Reply-To | <b4fd634c-de04-4cd6-be71-32f56b355acc@googlegroups.com> |
| Subject | Re: Logging Custom Levels? |
| MIME-Version | 1.0 |
| X-Mailer | Zimbra 7.2.7_GA_2942 (ZimbraWebClient - GC37 (Linux)/7.2.7_GA_2942) |
| Content-Type | text/plain; charset="utf-8" |
| Content-Transfer-Encoding | base64 |
| 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.384.1427823434.10327.python-list@python.org> (permalink) |
| Lines | 39 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1427823434 news.xs4all.nl 2913 [2001:888:2000:d::a6]:57768 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:88397 |
Show key headers only | View raw
----- Original Message -----
> From: "Didymus" <lynto28@gmail.com>
> To: python-list@python.org
> Sent: Tuesday, 31 March, 2015 5:20:52 PM
> Subject: Logging Custom Levels?
>
> Hi,
>
> I've create a Python file called "log.py" and placed in the custom
> levels:
>
> # Performance Debug...
> logging.addLevelName(PDEBUG_NUM, "PDEBUG")
>
> def pdebug(self, message, *args, **kws):
> """ Performance Debug Message Level """
> self.log(PDEBUG_NUM, message, *args, **kws)
>
> logging.Logger.pdebug = pdebug
>
>
> This works except that the %(module) and %(lineno) does not print
> properly, it instead prints out as the "log.py" and the line that
> this is on. I think I figured out a way to get the module and line
> from the calling custom level:
>
> import inspect
> frame = inspect.currentframe()
> filename =
> os.path.splitext(os.path.basename(frame.f_back.f_code.co_filename))[0]
> linenumber = frame.f_back.f_lineno
>
>
> My question is how do I pass this into the "self.log" call properly?
> I've tried a few different things without any luck. Any ideas how I
> can pass this into the custom logging level and get it to work?
>
> Thanks in Advance for any help!
> Tom
A solution is pretty simple, do not use an intermediate log function pdebug.
import logging
PDEBUG_NUM=20
logging.addLevelName(PDEBUG_NUM, "PDEBUG")
logger = logging.getLogger('foo')
logging.basicConfig(level=logging.DEBUG, format='%(message)s %(lineno)d')
logger.log(PDEBUG_NUM, 'This will work :')
If you *really* want to go for the hackish way, forget about the inspect module, the following pdebug function should do the trick:
def pdebug(self, message, *args, **kws):
if self.isEnabledFor(PDEBUG_NUM):
self._log(PDEBUG_NUM, message, args, **kws)
Cheers,
JM
-- IMPORTANT NOTICE:
The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: Logging Custom Levels? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2015-03-31 19:37 +0200 Re: Logging Custom Levels? Didymus <lynto28@gmail.com> - 2015-04-01 04:51 -0700
csiph-web