DANCE WITH JAVA

开发出高质量的系统

常用链接

统计

积分与排名

好友之家

最新评论

python log

#!/usr/bin/python
"""Publog.py
CLASSES Logger
if you want to use this logger .you must set two value
    1:set the output level
    2:set the outs--output files's path /default is console
"""
__version__= '$Version 1.0$'
import sys
import os
import time
#log level define begin
DEBUG=0
INFO=1
WARN=2
ERROR=3
FATAL=4
NOLOG=5
#log level define end
outs=['con']
level=NOLOG
#min is 10k
maxsize=10000
class Logger:
    def __init__(self,head='common log'):
        self.head=head
        self.output=outs
        if maxsize <10000:
            self.maxsize=10000
        else:
            self.maxsize=maxsize
        if level >5 | level<0:
            self.level=NOLOG
        else:
            self.level=level
    #debug
    def debug(self,str=''):
        if DEBUG>=self.level:
            self.writelog(str)
        else:
            return
    #info
    def info(self,str=''):
        if INFO>=self.level:
            self.writelog(str)
        else:
            return
    #warn
    def warn(self,str=''):
        if WARN>=self.level:
            self.writelog(str)
        else:
            return
    #error
    def error(self,str=''):
        if ERROR>=self.level:
            self.writelog(str)
        else:
            return
    #fatal
    def fatal(self,str=''):
        if FATAL>=self.level:
            self.writelog(str)
        else:
            return
    #output
    def writelog(self,str=''):
        prtstr=self.head+" : "+str+"  ("+time.ctime() +")"
        for path in self.output:
            if path=='con':
                print prtstr
            else:
                try:
                    if os.path.exists(path):
                        if os.stat(path)[6] >self.maxsize:
                            newname=self.getname(path)
                            if os.path.exists(newname):
                                os.remove(newname)
                            os.rename(path,newname)
                    logfile=open(path,"a+")                           
                    try:
                        logfile.write(prtstr+os.linesep)
                        logfile.close()
                    finally:
                        if not logfile.closed:
                            logfile.close()
                except IOError:
                    pass
    def getname(self,path):
        dirname,filename=os.path.split(path)       
        # filename=str(time.localtime()[0:3])+filename
        year,month,date,hour,min=time.localtime()[0:5]
        filename=str(year)+"-"+str(month)+"-"+str(date)+"-"+str(hour)+"-"+str(min)+"-"+filename
        newname=os.path.join(dirname,filename)
        return newname
def test():
    global level,outs
    level=INFO
    outs=['con','D:/edi/log.txt']
    maxsize=20000
    logger=Logger("==test head")
    logger.debug("debug")
    logger.info("info")
    logger.error("error")
if __name__== '__main__':
    test()
       

posted on 2006-09-20 22:19 dreamstone 阅读(275) 评论(0)  编辑  收藏 所属分类: 片段


只有注册用户登录后才能发表评论。


网站导航: