【翻译】python日志logging模块

来源:互联网 发布:陕钢集团网络大学 编辑:程序博客网 时间:2024/06/14 09:37
python logging module
What if you wanted to have some debugging messages orimportant messages
to be stored somewhere so that you can check whether yourprogram has been
running as you would expect it? How do you “store somewhere”these messages?
This can be achieved using the logging module.
大概意思为:当你运行你的项目的时候,你是否想过在某个地方记录程序测试信息或者消息,这样就可以检测你的项目运行出错在哪里。你是如何处理这些信息的?这是一个可以处理logging模块。
Save as use_logging.py: 把下面的代码保存为user_logging.py
------------------------------------------------------
import os, platform, logging
ifplatform.platform().startswith('Windows'):#判断操作系统类型,获取当前用户的根目录

#print platform.platform(aliased = 0, terse=0)实参可以为空
#Linux-3.5.0-41-generic-i686-with-Ubuntu-12.04-precise

#print platform.platform(aliased = 0, terse=1)
#Linux-3.5.0-41-generic-i686-with-glibc2.7

  logging_file =os.path.join(os.getenv('HOMEDRIVE'), os.getenv('HOMEPATH'),'test.log')
else:
  logging_file =os.path.join(os.getenv('HOME'), 'test.log')
print("Logging to", logging_file)
#配置调试参数
logging.basicConfig(
  level=logging.DEBUG,
  format='%(asctime)s : %(levelname)s :%(message)s',
  filename = logging_file,
  filemode = 'w',
  )
logging.debug("Start of the program") #debug开始调试
logging.info("Doing something") #测试阶段
logging.warning("Dying now")#捕获一个警告
------------------------------------------------------
Output:
$ python3 use_logging.py
Logging to C:\Users\zhangzhipeng\test.log#Linux上则是:/home/zhangzhipeng/test.log
If we check the contents of test.log, it will look somethinglike this:
2012-10-26 16:52:41,339 : DEBUG : Start of the program
2012-10-26 16:52:41,339 : INFO : Doing something
2012-10-26 16:52:41,339 : WARNING : Dying now
How It Works:
We use three modules from the standard library - the os modulefor interacting
with the operating system, the platform module for informationabout the
platform i.e. the operating system and the logging module tolog information.
First, we check which operating system we are using bychecking the string re-
turned by platform.platform() (for more information, seeimport platform;
help(platform)). If it is Windows, we figure out the homedrive, the home
folder and the filename where we want to store theinformation. Putting these
three parts together, we get the full location of the file.For other platforms, we
need to know just the home folder of the user and we get thefull location of the
file.
We use the os.path.join() function to put these three parts ofthe location
together. The reason to use a special function rather thanjust adding the strings
together is because this function will ensure the fulllocation matches the format
expected by the operating system.
We configure the logging module to write all the messages in aparticular format
to the file we have specified.
Finally, we can put messages that are either meant fordebugging, information,
warning or even critical messages. Once the program has run,we can check this
file and we will know what happened in the program, eventhough no information
was displayed to the user running the program.
这个英文版的流程我就不翻译了,中间代码加了注释的。
这本书名叫A Byte of Python.
当然了,本例中的路径是绝对路径,可以根据实际情况写相对路径。

原创翻译

0 0
原创粉丝点击