您的位置:首页 > 编程语言 > Python开发

【翻译】python日志logging模块

2017-03-20 14:41 183 查看
python logging module
What if you wanted to have some debugging messages or
important messages
to be stored somewhere so that you can check whether your
program 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
if
platform.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 something
like 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 module
for interacting
with the operating system, the platform module for information
about the
platform i.e. the operating system and the logging module to
log information.
First, we check which operating system we are using by
checking the string re-
turned by platform.platform() (for more information, see
import platform;
help(platform)). If it is Windows, we figure out the home
drive, the home
folder and the filename where we want to store the
information. 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 the
full location of the
file.
We use the os.path.join() function to put these three parts of
the location
together. The reason to use a special function rather than
just adding the strings
together is because this function will ensure the full
location matches the format
expected by the operating system.
We configure the logging module to write all the messages in a
particular format
to the file we have specified.
Finally, we can put messages that are either meant for
debugging, 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, even
though no information
was displayed to the user running the program.
这个英文版的流程我就不翻译了,中间代码加了注释的。
这本书名叫A Byte of Python.
当然了,本例中的路径是绝对路径,可以根据实际情况写相对路径。
原创翻译
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: