码迷,mamicode.com
首页 > 编程语言 > 详细

【python】日志模块

时间:2016-04-20 13:28:35      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

# -*- coding: utf-8 -*-
"""
Created on Thu Jul 09 09:36:59 2015

@author: dapenghuang
"""

from config import config_general
import time
import sys
import os

def getFileDir(layer=1):
    file_full = os.path.abspath(sys.argv[0])
    floor=file_full.split(os.sep)
    fileDir=‘‘
    for i in range(len(floor)-layer):
        fileDir+=floor[i]+os.sep
    return fileDir[:-1]
    
algorithmDir=getFileDir(1)#获得父目录

logOpen = config_general.logOpen#log是否打开
logPrint = config_general.logPrint#log是否打印控制台

logFile = ./log/log.txt

class Log:
    ‘‘‘
    为过程提供日志支持
    ‘‘‘
    def __init__(self,filename= logFile):
        ‘‘‘
        以追加模式打开
        ‘‘‘
        self._f = open(filename,a)
    def __del__(self):
        self._f.close()
    def writeLog(self,logType,className,funcName,logtxt):
        if logOpen == True:
            log = u%s【%s】【%s.%s】%s % (time.ctime(),logType,className,funcName,logtxt)
            log += \n
            if logPrint == True:
                print(log)
            self._f.write(log)


""" 
#example
#debug
class A:
    def __init__(self):
        pass
    def rec(self):
        log =Log()
        className =  self.__class__.__name__
        funcName =  sys._getframe().f_code.co_name
        log.writeLog(‘Info‘,className,funcName,‘test‘)
        
a=A()
a.rec()
#enddebug
"""

 

【python】日志模块

标签:

原文地址:http://www.cnblogs.com/colipso/p/5411839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!