Cool_Breeze 发表于 2021-7-19 14:56

Python3 日志学习笔记(logging)

# -*- coding : utf-8 -*-

import os
import sys
import logging
import threading
import subprocess

import xml.etree.ElementTree as ET


def Ping():

    log = logging.getLogger("root.Ping")
    ping = subprocess.Popen(['ping', '127.0.0.1', "-n", "2"], stdout = subprocess.PIPE, shell = True)
    log.info(f"{ping} 启动成功")
    return ping

def Dxdiag():
   
    log = logging.getLogger("root.Dxdiag")
    if os.path.exists("test.xml"):
      log.warning(f"删除test.xml")
      os.remove("test.xml")
    dxdiag = subprocess.Popen(['dxdiag', "/whql:off", '/64bit', "/x", "test.xml"])
    log.info(f"{dxdiag} 启动成功")
    return dxdiag

def TaskPing():

    log = logging.getLogger("root.TaskPing")
    ping = Ping()
    log.info(f"等待 {ping}")
    ping.wait()
    log.debug(ping.stdout.read().decode("gbk"))
    log.info(f"等待 {ping} 完成!")

def TaskDxdiag():
    log = logging.getLogger("root.TaskDxdiag")
    dxdiag = Dxdiag()
    log.info(f"等待 {dxdiag}")
    dxdiag.wait()
    if os.path.exists("test.xml"):
      root = ET.parse("test.xml").getroot()
      for n in root:
            log.debug(n.tag + ":" + n.text)
    log.info(f"等待 {dxdiag} 完成!")

if __name__ == "__main__":
   
    # 日志配置
    log = logging.getLogger("root")
    # 写入文件
    fileHandler = logging.FileHandler("subprocess.log", mode = "w", encoding = "gbk")
    fileHandler.setFormatter(logging.Formatter("%(filename)s - %(lineno)d - %(asctime)s - %(name)s - %(levelname)s - %(message)s"))
    fileHandler.setLevel(logging.DEBUG) # 过滤日志级别
    # 写入控制台
    consoleHandler = logging.StreamHandler()
    consoleHandler.setFormatter(logging.Formatter("%(asctime)s -%(levelname)s - %(message)s"))
    consoleHandler.setLevel(logging.INFO)
   
    log.addHandler(fileHandler)
    log.addHandler(consoleHandler)
    # 设置日志级别
    log.setLevel(logging.DEBUG)
    # 日志配置结束
   
    t1 = threading.Thread(target = TaskPing)
    t2 = threading.Thread(target = TaskDxdiag)
   
    t1.start()
    t2.start()
   
    t2.join()
    log.info("程序结束!")

控制台:
2021-07-19 14:53:35,123 -WARNING - 删除test.xml
2021-07-19 14:53:35,130 -INFO - <subprocess.Popen object at 0x00669790> 启动成功
2021-07-19 14:53:35,130 -INFO - <subprocess.Popen object at 0x00BAAE10> 启动成功
2021-07-19 14:53:35,131 -INFO - 等待 <subprocess.Popen object at 0x00669790>
2021-07-19 14:53:35,132 -INFO - 等待 <subprocess.Popen object at 0x00BAAE10>
2021-07-19 14:53:36,199 -INFO - 等待 <subprocess.Popen object at 0x00BAAE10> 完成!
2021-07-19 14:53:40,325 -INFO - 等待 <subprocess.Popen object at 0x00669790> 完成!
2021-07-19 14:53:40,326 -INFO - 程序结束!

文本:
Logger.py - 23 - 2021-07-19 14:53:35,123 - root.Dxdiag - WARNING - 删除test.xml
Logger.py - 26 - 2021-07-19 14:53:35,130 - root.Dxdiag - INFO - <subprocess.Popen object at 0x00669790> 启动成功
Logger.py - 16 - 2021-07-19 14:53:35,130 - root.Ping - INFO - <subprocess.Popen object at 0x00BAAE10> 启动成功
Logger.py - 41 - 2021-07-19 14:53:35,131 - root.TaskDxdiag - INFO - 等待 <subprocess.Popen object at 0x00669790>
Logger.py - 33 - 2021-07-19 14:53:35,132 - root.TaskPing - INFO - 等待 <subprocess.Popen object at 0x00BAAE10>
Logger.py - 35 - 2021-07-19 14:53:36,198 - root.TaskPing - DEBUG -

正在 Ping 127.0.0.1 具有 32 字节的数据:

来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128

来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128



127.0.0.1 的 Ping 统计信息:

    数据包: 已发送 = 2,已接收 = 2,丢失 = 0 (0% 丢失),

往返行程的估计时间(以毫秒为单位):

    最短 = 0ms,最长 = 0ms,平均 = 0ms


Logger.py - 36 - 2021-07-19 14:53:36,199 - root.TaskPing - INFO - 等待 <subprocess.Popen object at 0x00BAAE10> 完成!
Logger.py - 46 - 2021-07-19 14:53:40,324 - root.TaskDxdiag - DEBUG - Time:7/19/2021, 14:53:35
Logger.py - 46 - 2021-07-19 14:53:40,325 - root.TaskDxdiag - DEBUG - DxDiag64Bit:0
Logger.py - 47 - 2021-07-19 14:53:40,325 - root.TaskDxdiag - INFO - 等待 <subprocess.Popen object at 0x00669790> 完成!
Logger.py - 75 - 2021-07-19 14:53:40,326 - root - INFO - 程序结束!

无敌铁桶 发表于 2021-7-19 15:07

先来抢个沙发,哈哈哈哈

飘零星夜 发表于 2021-7-19 15:07

最近写笔记的有点多啊, 我要不要来一波

绝地飞鸿 发表于 2021-7-19 15:41

yingsummery 发表于 2021-7-19 16:46

学习了,谢谢楼主

Cool_Breeze 发表于 2021-7-19 17:17

飘零星夜 发表于 2021-7-19 15:07
最近写笔记的有点多啊, 我要不要来一波

下次使用,方便查找,。
页: [1]
查看完整版本: Python3 日志学习笔记(logging)