1. 程式人生 > >Python 自定義日誌記錄模組

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     :    
[email protected]
Creat Time : 2018-09-23 10:33:04 CSDN blog : https://blog.csdn.net/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)