日誌不管對於開發或者運維都是一項非常重要的東西,它可以用來排錯,解決故障,統計分析等。
本文介紹python中的日誌庫的用法。
匯入日誌庫:import logging
日誌庫的主要有以下方法:
獲取日誌名,比如
logging.getlogger(__name__)
定義handler,比如
logging.filehandler('/var/log/messages')
設定級別,比如
fh.setlevel(logging.debug)
定義格式,比如
formatter = logging.formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
列印日誌,比如
log.error("python logging test!!")
例項1
下面給出一段指令碼用來輸出日誌到/var/log/messages和螢幕。
import logging
log = logging.getlogger(__name__)
log.setlevel(logging.error)
formatter = logging.formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh = logging.filehandler('/var/log/messages')
fh.setlevel(logging.debug)
fh.setformatter(formatter)
log.addhandler(fh)
ch = logging.streamhandler()
ch.setlevel(logging.error)
ch.setformatter(formatter)
log.addhandler(ch)
log.error("python logging test!!")
執行下,你會發現/var/log/messages和螢幕上都會有類似「2015-06-28 07:41:41,527 - test - error - python logging test!!」。
例項2
如果只是記錄日誌到乙個檔案或螢幕,可以採用簡單的方法:logging.basicconfig()
import logging
logging.basicconfig(filename='example.log', level=logging.debug)
logging.error("this is a error test")
例項3
下面介紹需要記錄日誌到不同檔案的方法,這時就不能採用logging.basicconfig()了。
有兩個檔案,乙個是main.py,乙個是logger.py ,
輸出到不同的日誌檔案需要獲取不同的日誌物件:logging.getlogger()
cat main.py
#!/usr/bin/env python
import logging
from logger import logger
list = ['test1', 'test2']
msg1 = "this is a testing !"
msg2 = "this is the second testing !"
def set_logger(logger_name, log_file, level=logging.info):
#logging.basicconfig(filename=name, level=logging.debug, format='%(asctime)s %(name)s %(message)s')
l = logging.getlogger(logger_name)
formatter = logging.formatter('%(asctime)s : %(message)s')
filehandler = logging.filehandler(log_file, mode='w')
filehandler.setformatter(formatter)
#streamhandler = logging.streamhandler()
#streamhandler.setformatter(formatter)
l.setlevel(level)
l.addhandler(filehandler)
#l.addhandler(streamhandler)
for logger_name in list:
log_file = logger_name
set_logger(logger_name, log_file, level=logging.info)
logger(logger_name, msg1)
logger(logger_name, msg2)
cat logger.py
#!/usr/bin/env python
import logging
import os
import getpass
def log_flock(runner):
if runner is not none:
try:
fcntl.lockf(runner.output_lockfile, fcntl.lock_ex)
except oserror:
# already got closed?
pass
else:
pass
def log_unflock(runner):
if runner is not none:
try:
fcntl.lockf(runner.output_lockfile, fcntl.lock_un)
except oserror:
# already got closed?
pass
else:
pass
def logger(logger_name, msg, flag=none, runner=none):
log_flock(runner)
mypid = str(os.getpid())
user = getpass.getuser()
log = logging.getlogger(logger_name)
if flag == 'red':
log.error(msg)
else:
log.info(msg)
log_unflock(runner)
python的日誌記錄方法
python的日誌記錄方法較多,這裡我只簡單的介紹一下logging模組記錄日誌的方法,其他方法請自行查詢。logging模組支援5種等級的日誌記錄,包括debug information warning error和critical。下面我們利用例項簡單的了解一下這幾個等級的日誌記錄。首先在我們的...
python記錄日誌
coding utf 8 author martin date 2017 10 9 import logging import sys 獲取logger例項,如果引數為空則返回root logger 指定logger輸出格式 formatter logging.formatter asctime s...
詳解python程式中記錄日誌的方法
日誌可以用來記錄應用程式的狀態 錯誤和資訊訊息,也經常作為除錯程式的工具。它的重要性就不多說了,直接進入正題。python提供了乙個標準的日誌介面,就是logging模組。日誌級別有debug info warning error critical五種。首先來看logging簡單的使用方法。這一看到...