python日志
短信预约 -IT技能 免费直播动态提醒
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')
用法:
from log4py import log4py
log=log4py('所在的python文件')
log.debug("aaaaa")
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341