1. 程式人生 > >Python模塊-logging模塊(一)

Python模塊-logging模塊(一)

level fmt 文件的 所在 mut com 警告 正常 port

logging模塊用來寫日誌文件

有5個級別,debug(),info(),warning(),error()和critical(),級別最高的為critical()

debug()為調試模式,info()為正常情況下的信息,warning()為警告,error()為錯誤,critical()為嚴重問題

普通的打印

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.debug(‘The debug‘)
logging.info(‘The info‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

5個級別中只打印了警告級別及之後的語句

寫入日誌文件

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(filename=‘log_test.log‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

level=loggin.INFO,把日誌級別設置為INFO,只有INFO或比INFO級別更高的日誌才會被寫到日誌文件裏

寫入日誌文件的內容

技術分享圖片

只從info級別開始寫入內容,debug級別比info低,沒有寫入

日誌格式化

%(name)s 日誌寫入者的名稱

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(name)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

%(levelno)s 數字形式的日誌級別,%(levelname)s 文本形式的日誌級別

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(levelno)s,%(levelname)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

%(lineno)d 調用日誌輸出函數的語句所在的代碼行

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(lineno)d‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

%(pathname)s 調用日誌輸出函數的模塊的完整路徑名

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(pathname)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

%(filename)s 調用日誌輸出函數的模塊的文件名

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(filename)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

%(module)s 調用日誌輸出函數的模塊名

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(module)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

就是文件名,沒有後綴

%(funcName)s 調用日誌輸出函數的函數名

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(funcName)s‘,level=logging.INFO)

def log_test():
    logging.debug(‘This python is running‘)
    logging.info(‘The infomation of python‘)
    logging.warning(‘Request outtime‘)
    logging.error(‘The python run error‘)
    logging.critical(‘The server down‘)

log_test()

運行結果

技術分享圖片

%(created)f 當前時間,用UNIX標準的表示時間的浮點數表示

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(created)f‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

%(relativeCreated)d 輸出日誌信息自創建以來的毫秒數

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging
import time

logging.basicConfig(format=‘%(relativeCreated)d‘,level=logging.INFO)

logging.debug(‘This python is running‘)
time.sleep(2)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

結果是不定的

%(asctime)s 字符串形式的當前時間,默認格式為“2018-02-15 17:59:31,546”,逗號後面的是毫秒

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(asctime)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

%(thread)d 當前線程號,%(threadName)s 當前線程名

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(thread)d,%(threadName)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

%(process)d 進程號

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(process)d‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

%(message)s 用戶輸出的消息

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(format=‘%(message)s‘,level=logging.INFO)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

格式化的日誌寫入

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import logging

logging.basicConfig(filename=‘log_test.log‘,level=logging.INFO,format=‘%(message)s‘,datefmt=‘%m‘)

logging.debug(‘This python is running‘)
logging.info(‘The infomation of python‘)
logging.warning(‘Request outtime‘)
logging.error(‘The python run error‘)
logging.critical(‘The server down‘)

運行結果

技術分享圖片

Python模塊-logging模塊(一)