Python简单日志处理类分享
简单的一个python日志处理类
#/usr/bin/python #coding=utf-8
importtime,types
classlogsys:
def__init__(self,project,logfilename='sys_log.txt'): self.project=project self.logfilename=logfilename
defget_log_time(self): returntime.strftime("%Y-%m-%d%X",time.localtime())
defwrite2file(self,*formart): s=self.formart_string(*formart) ifs: encoding='utf8' out=open(self.logfilename,'a+') out.write(s+"\n") out.close() else: pass
defformart_string(self,*formart): string='' encoding='utf8' forstrinformart: ifnottype(str)in[types.UnicodeType,types.StringTypes,types.StringType]: s=repr(str) else: s=str iftype(s)==type(u''): string+=s.encode(encoding)+"\t" else: string+=s+"\t" returnstring
defw(self,notice,*formart): self.write2file(self.get_log_time(),'['+notice+']',self.project,*formart)