日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

記錄Python腳本的運行日志的方法

系統(tǒng) 2321 0

一、logging模塊

Python中有一個模塊logging,可以直接記錄日志

            
#  日志級別
# CRITICAL 50
# ERROR  40
# WARNING 30
# INFO   20
# DEBUG  10
          

logging.basicConfig()函數(shù)中的具體參數(shù):

filename:?? 指定的文件名創(chuàng)建FiledHandler,這樣日志會被存儲在指定的文件中;
filemode:?? 文件打開方式,在指定了filename時使用這個參數(shù),默認(rèn)值為“w”還可指定為“a”;
format:????? 指定handler使用的日志顯示格式;
datefmt:??? 指定日期時間格式。,格式參考strftime時間格式化(下文)
level:??????? 設(shè)置rootlogger的日志級別
stream:???? 用指定的stream創(chuàng)建StreamHandler。可以指定輸出到sys.stderr,sys.stdout或者文件,默認(rèn)為sys.stderr。
????????????????? 若同時列出了filename和stream兩個參數(shù),則stream參數(shù)會被忽略。

?format參數(shù)中可能用到的格式化信息:

%(name)s

Logger的名字

%(levelno)s

數(shù)字形式的日志級別

%(levelname)s

文本形式的日志級別

%(pathname)s

調(diào)用日志輸出函數(shù)的模塊的完整路徑名,可能沒有

%(filename)s

調(diào)用日志輸出函數(shù)的模塊的文件名

%(module)s

調(diào)用日志輸出函數(shù)的模塊名

%(funcName)s

調(diào)用日志輸出函數(shù)的函數(shù)名

%(lineno)d

調(diào)用日志輸出函數(shù)的語句所在的代碼行

%(created)f

當(dāng)前時間,用UNIX標(biāo)準(zhǔn)的表示時間的浮 點數(shù)表示

%(relativeCreated)d

輸出日志信息時的,自Logger創(chuàng)建以 來的毫秒數(shù)

%(asctime)s

字符串形式的當(dāng)前時間。默認(rèn)格式是 “2003-07-08 16:49:45,896”。逗號后面的是毫秒

%(thread)d

線程ID。可能沒有

%(threadName)s

線程名。可能沒有

%(process)d

進程ID。可能沒有

%(message)s

用戶輸出的消息

二、logging模塊測試

1、打印日志到標(biāo)準(zhǔn)輸出中

            
import logging
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
          

輸出結(jié)果

C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe D:/pyworkpeace/tupian.py 'https://www.tianyancha.com/login'
WARNING:root:warning message

Process finished with exit code 0

可以看出默認(rèn)情況下Python的logging模塊將日志打印到了標(biāo)準(zhǔn)輸出中,且只顯示了大于等于WARNING級別的日志。默認(rèn)的日志的格式為:

日志級別:Logger名稱:用戶輸出消息

2、將日志文件輸入到文件中

            
import os
logging.basicConfig(filename=os.path.join(os.getcwd(),'log.txt'),level=logging.DEBUG)
logging.debug('this is a message')


          

運行這三行代碼后會在安裝Python的目錄中出現(xiàn)一個log.txt文件,文件內(nèi)容

DEBUG:root:this is a message
DEBUG:root:debug message

3、自定義格式,輸出日志文件

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

import logging


def console_out(logFilename):
  ''''' Output log to file and console '''
  # Define a Handler and set a format which output to file
  logging.basicConfig(
    level=logging.DEBUG, # 定義輸出到文件的log級別,大于此級別的都被輸出
    format='%(asctime)s %(filename)s : %(levelname)s %(message)s', # 定義輸出log的格式
    datefmt='%Y-%m-%d %A %H:%M:%S', # 時間
    filename=logFilename, # log文件名
    filemode='w') # 寫入模式“w”或“a”
  # Define a Handler and set a format which output to console
  console = logging.StreamHandler() # 定義console handler
  console.setLevel(logging.INFO) # 定義該handler級別
  formatter = logging.Formatter('%(asctime)s %(filename)s : %(levelname)s %(message)s') # 定義該handler格式
  console.setFormatter(formatter)
  # Create an instance
  logging.getLogger().addHandler(console) # 實例化添加handler

  # Print information       # 輸出日志級別
  logging.debug('logger debug message')
  logging.info('logger info message')
  logging.warning('logger warning message')
  logging.error('logger error message')
  logging.critical('logger critical message')


if __name__ == "__main__":
  console_out('logging.log')


          

輸出結(jié)果:

此時也會自動生成一個日志文件,日志文件和運行文件在同一個文件夾中,文件名logging.log

2017-10-23 Monday 11:37:59 hgghf : DEBUG logger debug message
2017-10-23 Monday 11:37:59 hgghf : INFO logger info message
2017-10-23 Monday 11:37:59 hgghf : WARNING logger warning message
2017-10-23 Monday 11:37:59 hgghf : ERROR logger error message
2017-10-23 Monday 11:37:59 hgghf : CRITICAL logger critical message

修改輸出路徑:

            
filename='/tmp/test1.log', # log文件名
          

當(dāng)將腳本中這行代碼換一下,那么我們輸出日志的路徑地址就換成了D:\tmp

下面的方式同樣可以達到上述結(jié)果

?4、自定義輸出位置

            
import logging 
logging.basicConfig(level=logging.DEBUG, 
          format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', 
          datefmt='%a, %d %b %Y %H:%M:%S', 
          filename='/tmp/test.log', 
          filemode='w') 
 
logging.debug('debug message') 
logging.info('info message') 
logging.warning('warning message') 
logging.error('error message') 
logging.critical('critical message') 


          

由于運行腳本放在D:\pyworkpeace\下,輸出文件在D盤tmp文件夾下test.log,內(nèi)容如下:

Mon, 23 Oct 2017 15:00:05 tupian.py[line:11] DEBUG debug message
Mon, 23 Oct 2017 15:00:05 tupian.py[line:12] INFO info message
Mon, 23 Oct 2017 15:00:05 tupian.py[line:13] WARNING warning message
Mon, 23 Oct 2017 15:00:05 tupian.py[line:14] ERROR error message
Mon, 23 Oct 2017 15:00:05 tupian.py[line:15] CRITICAL critical message

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 罗江县| 静海县| 鹤岗市| 江北区| 错那县| 奈曼旗| 托克逊县| 白山市| 宽城| 石渠县| 铁岭县| 聂拉木县| 商水县| 邮箱| 锦屏县| 济阳县| 鸡泽县| 蒙阴县| 和田县| 井陉县| 辉南县| 固阳县| 石狮市| 兴宁市| 贺州市| 渭南市| 阜康市| 若羌县| 崇礼县| 白河县| 石阡县| 汶川县| 江门市| 商河县| 阿巴嘎旗| 庆城县| 江川县| 淅川县| 长泰县| 青龙| 淮滨县|