下面我们看一个更标准的程序:
>>> import logging
>>> logger=logging.getLogger()
>>> handler=logging.FileHandler("Log_test.txt")
>>> logger.addHandler(handler)
>>> logger.setLevel(logging.NOTSET)
>>> logger.error("This is an error message")
>>> logger.info("This is an info message")
>>> logger.critical("This is a critical message")
日志文件中会出现三行内容:
This is an error message
This is an info message
This is a critical message
======================================
#!/usr/bin/python
import logging
fname1 = 'test1.txt'
fname2 = 'test2.txt'
def iniLog():
logger = logging.getLogger() #起个logger
filehandler = logging.FileHandler(fname1) #handler有多种,filehandler,streamhandler==
streamhandler = logging.StreamHandler()
fmt = logging.Formatter('%(asctime)s, %(funcName)s, %(message)s') #格式化日志
logger.setLevel(logging.DEBUG) #设置级别
logger.addHandler(filehandler) #挂上handler
logger.addHandler(streamhandler)
return logger
#This function does nothing if the root logger already has handlers configured.这个是文档上的原话,哈哈
logging.basicConfig(filename=fname2, level=logging.INFO)
logging.info("do 1") #这个调用和下面那个结果可是不一样的,这个只写file2,并且不会在终端打印日志
logger = iniLog()
logger.info("do 2") #这个往两个file都写,并且向终端打印日志
================================
c="a string to print to file"
f=open('out.txt','w')
print >>f,c
f.close()
============================
import logging
import urllib, os
import sys
LOGPATH = os.path.abspath(os.path.dirname(sys.argv[0]))+'\com_server.py.log'
def logger_constructor():
logger = logging.getLogger()
hdlr = logging.FileHandler(LOGPATH)
formatter = logging.Formatter(u'[%(asctime)s]%(levelname)-8s"%(message)s"','%Y-%m-%d %a %H:%M:%S')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.NOTSET)
return logger
global_logger = logger_constructor()
def trace_log(msg):
global_logger.debug(msg)
def DEBUGMSG(s):
s = s.replace('\r','').replace('\n','')
if (s):
print "%s: %s" % (time.ctime(), s)
trace_log(s)
DEBUGMSG('kkkk')
============================
发现python里面扩展了日志打印功能 。感觉比java自带的还好,和log4j很类似。
下面总结 下其用法。先说一种不用 配置文件 的:
<log.py>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
14
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
15
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
16
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
17
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
18
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
19
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
20
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
21
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
22
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
23
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
24
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
25
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
26
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<test.py>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)