py日志输出

下面我们看一个更标准的程序:

>>> 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都写,并且向终端打印日志

 

 

 

 

================================

python print 输出到文件中   

2009-04-23 14:49:57 |  分类: Linux学习 | 字号   订阅

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>

 1 #  -*- codin g: gb2312 -*-
 2 import  logging
 3
 4 logging.basicConfig(level = logging.INFO ,
 5                     for mat = ' %(asctime)s %(name)-12s %(levelname)-8s %(message)s ' ,
 6                     datefmt = ' %m-%d %H:%M ' ,
 7                     filename = ' ./AutoUpdate.log ' ,
 8                     filemode = ' w ' )
 9
10 console  =  logging.StreamHandler()
11 console.setLevel(logging.INFO)
12 formatter  =  logging.Formatter( ' %(name)-12s: %(levelname)-8s %(message)s ' )
13 console.setFormatter(formatter)
14 logging.getLogger( '' ).addHandler(console)
15
16
17 #
18 #  console = logging.StreamHandler()
19 #  console = setLevel(logging.DEBUG )
20 #  formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
21 #  console.setFormatter(formatter)
22 #  logging.getLogger('').addHandler(console)
23
24
25 def  getLogging(name):
26      return  logging.getLogger(name)


<test.py>

import  log

testlog 
=  log.getLogging( ' test ' )
testlog.error( 
" ~~~~~~~~~~~~~~test1. " )
testlog.info( 
" ~~~~~~~~~~~~~~test2. "  )
testlog.exception( 
" ~~~~~~~~~~~~~~~~~~~~~~~~~test3. " )

你可能感兴趣的:(日志)