Python 自定义日志记录模块

工具/版本

(1)安装环境:Windows7 64bit

(2)使用版本Python3.6

 

 

#!/usr/bin/env python
# encoding: utf-8
"""
+----------------------------------------------------------------------------+
 ╱◥██◣         ∧_∧    ∧_∧      ∧_∧     ∧_∧     ╱◥██◣
|田︱田田|      (^ .^)  (^ 、^)  (^ 0^)  (^ Д^)  |田︱田田|
╬╬╬╬╬╬╬╬╬╬-------∪-∪-------∪-∪--------∪-∪-------∪-∪---╬╬╬╬╬╬╬╬╬╬╬
+----------------------------------------------------------------------------+
License (C) Copyright 2017-2017,  Corporation Limited.
File Name         :    RecordLog.py
Auther            :    samenmoer
Software Version  :    Python3.6
Email Address     :    gpf192315@163.com
Creat Time        :    2018-09-23   10:33:04
优快云 blog         :    https://blog.youkuaiyun.com/samenmoer
Description       :
------------------------------------------------------------------------------
Modification History
Data          By           Version       Change Description
==============================================================================
${time}            |                |                 |
==============================================================================
¤╭⌒╮ ╭⌒╮¤╭⌒╮ ╭⌒╮¤╭⌒╮ ╭⌒╮¤╭⌒╮  ╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮╭⌒╮¤╭⌒╮
------------------------------------------------------------------------------
"""
import datetime
import os


# ==========================================================================
# * Founction Name    :   print_log
# * Parameter         :   message : 输入的打印信息
# * Return            :   None
# * Description       :   通过开关控制是否打印print信息
#                         记录log信息方便出错查看
# ==========================================================================
def print_log(message, Format=True, LogHead=""):
    FileName = "../BlackBox/Running.log"
    if Format is True:
        nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        message = "[%s]:%s %s\n" % (nowTime, LogHead, message)
    else:
        message = "%s%s\n" % (LogHead, message)

    print(message)
    f = open(FileName, "a", encoding='gb18030')
    f.write(message)

    return


# ==========================================================================
# * Founction Name    :   CheckLogPath
# * Parameter         :   LogPath     : Log存储路径
# * Return            :   LogPathData : Log路径确认结果
# * Description       :   查看是否存在log路径,不存在自动创建
# ==========================================================================
def CheckLogPath(LogPath):
    if not os.path.isdir(LogPath):
        os.mkdir(LogPath)
        LogPathData = "不存在Log路径:%s,已自动创建" % LogPath
    else:
        LogPathData = "存在Log路径:%s,可以记录Log" % LogPath

    return LogPathData


# ==========================================================================
# * Founction Name    :   RecordLogHead
# * Parameter         :   待打印的信息
# * Return            :   None
# * Description       :   打印程序开始运行log信息
# ==========================================================================
def RecordLogHead(StartTime, MainPath, LogPathData):
    StartTime = StartTime.strftime('%Y-%m-%d %H:%M:%S')
    print_log("********************************************", Format=None)
    print_log("     StartTime: %s" % StartTime, Format=None)
    print_log("********************************************", Format=None)
    print_log(LogPathData, LogHead=LogHead)
    FileName = os.path.basename(MainPath)
    FilePath = os.path.dirname(MainPath)
    print_log("运行程序路径 %s" % FilePath, LogHead=LogHead)
    print_log("运行程序文件 %s" % FileName, LogHead=LogHead)

    return


# ==========================================================================
# * Founction Name    :   GetRunTime
# * Parameter         :   StartTime   : 开始时间
# * Return            :   elapsed_int : 运行时间
# * Description       :   输入开始时间,计算运行时间,ms精度
# ==========================================================================
def GetRunTime(StartTime):
    end = datetime.datetime.now()
    elapse = (end - StartTime).total_seconds() * 1000
    print_log("Progress take time %s ms" % elapse, LogHead=LogHead)

    return elapse


# ==========================================================================
# * Founction Name    :   StrToBool
# * Parameter         :   String : 待转换的字符串
# * Return            :   Bool   :
# * Description       :   如果通过配置文件配置,将str的True转换成bool型
# ==========================================================================
def StrToBool(String):
    return True if String.lower() == "true" else False


# ==========================================================================
# * Founction Name    :   RecordInit
# * Parameter         :   MainPath : 主程序路径
# * Return            :   None
# * Description       :   日志记录初始化模块
# ==========================================================================
def RecordInit(MainPath):
    global LogHead, RunLogName, RecordEN

    StartTime = datetime.datetime.now()
    # 此处可通过读取配置文件获取打印选项参数
    LogHead = "[Record]"
    RecordEN = StrToBool("True")
    LogFileName = "RunLog.log"
    RunLogPath = "..\\BlackBox\\"

    RunLogName = RunLogPath + LogFileName
    LogPathData = CheckLogPath(RunLogPath)
    RecordLogHead(StartTime, MainPath, LogPathData)
    print_log("日志记录模块初始化完成", LogHead=LogHead)


if __name__ == '__main__':
    import sys

    PyPath = sys.argv[0]
    RecordInit(PyPath)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值