本文共 3555 字,大约阅读时间需要 11 分钟。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | log4py.py日志重构类 import datetime import sys import traceback import codecs import types import logging import os import time #log编码全部按utf8处理 loglevels = { 'stdout' :[ 'info' , 'debug' , 'warn' , 'error' , 'fatal' ], 'file' :[ 'info' , 'debug' , 'warn' , 'error' , 'fatal' ] } #print os.getcwd()+'/logs/logs.txt' logfile = os.getcwd() + '/logs/logs.' + time.strftime( '%Y-%m-%d' ,time.localtime(time.time())) + '.txt' class log4py(): def __init__( self ,modulename = "gloabal" ): self .filename = logfile #self.flag = set(loglevel['stdout']+loglevel['file']) self .loglevel = loglevels self .modulename = modulename self .fcname = None class function(): def __init__( self ,fcname,parent): parent.debug( 'enter ' ,fcname) self .fcname = fcname self .parent = parent def __del__( self ): self .parent.debug( 'exit ' , self .fcname) def dbgfc( self ,fcname): '''''set debug function name''' f = None if 'debug' in self .flag: f = self .function(fcname, self ) return f def _gettime( self ): return datetime.datetime.now().isoformat() def outstd( self , * fmt): s = self .fmtstr( * fmt) print s def outfile( self , * fmt): s = self .fmtstr( * fmt) #print 'before outfile '+s if s: #print 'outfile '+s encoding = 'utf8' out = open (logfile, 'a+' ) #, encoding out.write(s) out.write( '\n' ) out.close() def fmtstr( self , * fmt): str = '' encoding = 'utf8' #缺省utf8编码 for i in fmt: if not type (i) in [types.UnicodeType, types.StringTypes, types.StringType]: s = repr (i) else : s = i if type (s) = = type (u''): str + = s.encode(encoding) else : str + = s str + = '.' #str += '/n' #print 'fmtstr:'+str return str def debug( self , * fmt): if 'debug' in self .loglevel[ 'stdout' ]: self .outstd( self ._gettime(), '[DEBUG]' , self .modulename, * fmt) if 'debug' in self .loglevel[ 'file' ]: #print 'debug file ...' self .outfile( self ._gettime(), '[DEBUG]' , self .modulename, * fmt) def warn( self , * fmt): if 'warn' in self .loglevel[ 'stdout' ]: self .outstd( self ._gettime(), '[WARN]' , self .modulename, * fmt) if 'warn' in self .loglevel[ 'file' ]: self .outfile( self ._gettime(), '[WARN]' , self .modulename, * fmt) def info( self , * fmt): if 'info' in self .loglevel[ 'stdout' ]: self .outstd( self ._gettime(), '[INFO]' , self .modulename, * fmt) if 'info' in self .loglevel[ 'file' ]: self .outfile( self ._gettime(), '[INFO]' , self .modulename, * fmt) def error( self , * fmt): #print '/033[0;30;41m', if 'error' in self .loglevel[ 'stdout' ]: self .outstd( self ._gettime(), '[ERROR]' , self .modulename, * fmt) if 'error' in self .loglevel[ 'file' ]: self .outfile( self ._gettime(), '[ERROR]' , self .modulename, * fmt) #print '/033[0m' def fatal( self , * fmt): if 'fatal' in self .loglevel[ 'stdout' ]: self .outstd( self ._gettime(), '[FATAL' , self .modulename, * fmt) if 'fatal' in self .loglevel[ 'file' ]: self .outfile( self ._gettime(), '[FATAL' , self .modulename, * fmt) #unit test if __name__ = = '__main__' : log = log4py() log.outstd( 'INFO' , 'stdout' , 'test' ) log.outfile( 'INFO' , 'stdout' , 'test' ) log.debug( 'debug information 调试' ) log.error( 'errorrrrrrrrrrrrrrr' ) log.debug( 'hello' ) |
用法:
1 2 3 | from log4py import log4py log = log4py( '所在的python文件' ) |
转载地址:http://pxqyo.baihongyu.com/