首页IT科技simple的比较级和最高级怎么写(Simple usage of Python’s logging module)

simple的比较级和最高级怎么写(Simple usage of Python’s logging module)

时间2025-05-03 02:03:05分类IT科技浏览3654
导读:Richard Jones Log:...

Richard Jones Log:

Simple usage of Pythons logging module

Several people have posted about Pythons new logging module. Most are confused by its complexity and inaccessible documentation (great reference, wheres the introduction?) Well, Ive used it once now, and heres the simplest I could make it:

import logging logger = logging.getLogger(myapp) hdlr = logging.FileHandler(var/myapp.log) formatter = logging.Formatter(%(asctime)s %(levelname)s %(message)s) hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.INFO)

And then to use it:

logger.info(a log message)

... of course, for most people, the ideal usage would be:

import logging logger = logging.open(var/myapp.log)

Gee, thatd be nice. Maybe Ill submit that as a patch, in one of my spare moments.

Comment by Stuart Bishop

on Wed, 10 Mar 2004

I think the preferred usage should be:

>>> logging.config.fileConfig(log.ini)

>>> logger = logging.getLogger(myapp)

>>> logger.info(oops I did it again)

Or for most applications, rather than libraries, just use the root logger:

>>> logging.info(played with your heart, right till the end)

This works file right now, except that unfortunatly the current format of log.ini is full of dead chickens and error prone. I think the goal is good, but the implementation falls down at this point (if you are going to have python code that is evald in your config files, you might as well save everyone the bother and just use real Python modules). I think this is where interested people should spend their Copious Free Time™.

Comment by Richard

on Wed, 10 Mar 2004

But I dont want to involve that horribly complicated config file stuff. I just want an object thatll log stuff to a file with different levels etc.

Doesnt the root logger log to stdout? And my first chunk of code is the only way to override that behaviour AFAICT.

Comment by

john on Wed, 10 Mar 2004

Is the preferred way to close the log

logger.removeHandler(hdlr)

hdlr.close()

? closing the handle seems to be problematic.

Thanks.

Comment by Jp Calderone

on Wed, 10 Mar 2004

Twisteds logging package seems to be a little friendlier for setup:

from twisted.python import log

log.startLogging(file(app.log, w))

log.msg("Tra la la, la la, la la")

twisted.python.log is capable of all the same things the logging package is. I would really have liked it if the logging package hadnt been so over-engineered. Then I could probably have convinced the right people that twisted.python.log wasnt necessary anymore. Alas...

Comment by Vinay Sajip

on Thu, 18 Mar 2004

I fully sympathise with Richards view that the common use case of logging to a file should be made easier. I will try to come up with something easier. However, a logger is not analogous to a file - rather, it is an event sink which can be routed to many different listeners, one of which might be a file. Hence comparisons with Twisteds logging do not seem accurate to me (based on a quick reading of Twisteds logging API in epydoc format). From what I can see, in Twisteds view, a logger appears analogous to a file or stream. If Im wrong, please point me in the right direction.

Response to Stuart Bishops comment about the config file format, which Richard says is "horribly complicated":

You could just use the Tkinter-based GUI application to set up a configuration for your application, and call fileConfig() to make the configuration effective. Why should you care what "cruft" is in the .ini file? You need never look at it. Some of the apparent cruft is there only to support the GUI configurator. Of course if someone can suggest a better config file format (while still sticking with ConfigParser), all suggestions will be gratefully accepted. Im not suggesting that whats there now is optimal. If you have Copious Free Time to look at alternative configuration implementations, Id be happy to hear suggestions.

Response to johns comment:

Please file a bug report on SourceForge. Closing handlers used to be problematic, but I am not aware of specific problems with current Python CVS or standalone version 0.4.9.2 (http://www.red-dove.com/python_logging.html#download).

Response to Jp Calderones comment:

Twisteds logging package may well be easier for certain specific setups. But can you substantiate that "twisted.python.log is capable of all the same things the logging package is"? For example, emailing logs, logging to HTTP, TCP, UDP, SOAP servers, logging to Unix syslog and NTs event log? Perhaps the logging package is over-engineered for your needs, but thats not to say its a commonly held view - not everyones needs are the same.

Comment by W. Borgert

on Thu, 18 Mar 2004

On GNU/Linux or UNIX you want to use the syslog:

import logging, logging.handlers

logger = logging.getLogger("a_name")

hdlr = logging.handlers.SysLogHandler(

facility=logging.handlers.SysLogHandler.LOG_DAEMON)

formatter = logging.Formatter(

%(filename)s: %(levelname)s: %(message)s)

hdlr.setFormatter(formatter)

logger.addHandler(hdlr)

Together with an advanced syslog daemon, such as syslog-ng,

this works very well.

Comment by

Brian Mahoney on Thu, 18 Mar 2004

I have stand-alone program execution that logs to a file via the logger FileHandler, but if I start the program on a thread, the launch command arg includes a socket number and the logger replaces FileHandler with SocketHandler. Then all my threaded programs can log via socket to the launching program.

Comment by Jim Jewett

on Thu, 01 Apr 2004

I think this boils down to "logging works great, once you are up to speed. It does not scale *down* very well."

easylog is an attempt to do this. Since it treats the underlying logging module as a black box, it should benefit from future improvements. (But since it deals with a black box, the internals are uglier than I would like.)

I am definately looking for feedback before I remove the alpha tag.

Comment by Vinay Sajip

on Tue, 11 Jan 2005

The new basicConfig() (in Python 2.4 and also from the standalone distribution v 0.4.9.5) addresses these concerns. This should be made clear in an update to the posting. You can now use basicConfig to set up a file handler, root logger level and formatter in one call. See the Python 2.4 docs online for more details:

http://docs.python.org/lib/minimal-example.html

Comment by

shawn on Thu, 26 May 2005

Im very happy with this simple way to use logging. Im currently trying to get my head around (and maintain) a zope installation with a bunch of pretty complex products. I just needed some simple way to output debugging info to a separate file. Ive found the existing documentation and examples to be a bit complex for someone who is just learning zope and python. This makes it very easy. Thanks.

Comment by Sebastien Celles

on Sat, 19 Apr 2008

Hello,

it would be nice to found a sample that explain how to have both a log in a file and on the console (stdout or stderr).

For now Im using

log = logging.getLogger("MyApp")

hdlr = logging.FileHandler(log.csv)

FORMAT=%(asctime)s\t%(levelname)s\t%(message)s

formatter = logging.Formatter(FORMAT)

logging.basicConfig(format=FORMAT) # log sur console

hdlr.setFormatter(formatter)

log.addHandler(hdlr)

log.setLevel(logging.DEBUG) #set verbosity to show all messages of severity >= DEBUG

So as you can see Im using basicConfig for doing this but thats not the best way...

I guess that I should use someting like

hdlrConsole = logging.StreamHandler(/dev/stdout)

hdlrConsole.setFormatter(formatter)

Best regards

声明:本站所有文章           ,如无特殊说明或标注                 ,均为本站原创发布            。任何个人或组织      ,在未征得本站同意时           ,禁止复制            、盗用                 、采集     、发布本站内容到任何网站      、书籍等各类媒体平台                 。如若本站内容侵犯了原著者的合法权益                 ,可联系我们进行处理     。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
网速测试准不准确(在线网速测试网速比较快,用起来比较慢的原因?) a logging team boss(A Logging System for Python)