Python 實現(xiàn)日志同時輸出到屏幕和文件
1. 日志輸出到屏幕
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import logging
logging.basicConfig(level=logging.NOTSET, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.debug('This is a debug message.')
logging.info('This is an info message.')
logging.warning('This is a warning message.')
logging.error('This is an error message.')
logging.critical('This is a critical message.')
默認的 level 是 logging.WARNING,低于這個級別的就不輸出了。如果需要顯示低于 logging.WARNING 級別的內容,可以引入 logging.NOTSET 級別來顯示。
DEBUG - 打印全部的日志。詳細的信息,通常只出現(xiàn)在診斷問題上。
INFO - 打印 INFO、WARNING、ERROR、CRITICAL 級別的日志。確認一切按預期運行。
WARNING - 打印 WARNING、ERROR、CRITICAL 級別的日志。表明一些問題在不久的將來,這個軟件還能按預期工作。
ERROR - 打印 ERROR、CRITICAL 級別的日志。更嚴重的問題,軟件沒能執(zhí)行一些功能。
CRITICAL : 打印 CRITICAL 級別。一個嚴重的錯誤,表明程序本身可能無法繼續(xù)運行。
/usr/bin/python2.7 /home/strong/git_workspace/MonoGRNet/test.py 2019-06-26 16:00:45,990 - root - DEBUG - This is a debug message. 2019-06-26 16:00:45,990 - root - INFO - This is an info message. 2019-06-26 16:00:45,990 - root - WARNING - This is a warning message. 2019-06-26 16:00:45,990 - root - ERROR - This is an error message. 2019-06-26 16:00:45,990 - root - CRITICAL - This is a critical message. Process finished with exit code 0
2. 日志輸出到文件
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import logging
import os.path
import time
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
time_line = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
print(os.getcwd())
log_path = os.path.dirname(os.getcwd()) + '/'
logfile = log_path + time_line + '.log'
handler = logging.FileHandler(logfile, mode='w')
handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.debug('This is a debug message.')
logger.info('This is an info message.')
logger.warning('This is a warning message.')
logger.error('This is an error message.')
logger.critical('This is a critical message.')
/usr/bin/python2.7 /home/strong/git_workspace/MonoGRNet/test.py /home/strong/git_workspace/MonoGRNet Process finished with exit code 0
201906261627.log
2019-06-26 16:27:26,899 - test.py[line:30] - INFO: This is an info message. 2019-06-26 16:27:26,899 - test.py[line:31] - WARNING: This is a warning message. 2019-06-26 16:27:26,899 - test.py[line:32] - ERROR: This is an error message. 2019-06-26 16:27:26,899 - test.py[line:33] - CRITICAL: This is a critical message.
3. 日志同時輸出到屏幕和文件
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import logging
import os.path
import time
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.DEBUG)
time_line = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
print(os.getcwd())
log_path = os.path.dirname(os.getcwd()) + '/'
logfile = log_path + time_line + '.log'
handler = logging.FileHandler(logfile, mode='w')
handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
handler.setFormatter(formatter)
console = logging.StreamHandler()
console.setLevel(logging.WARNING)
logger.addHandler(handler)
logger.addHandler(console)
logger.debug('This is a debug message.')
logger.info('This is an info message.')
logger.warning('This is a warning message.')
logger.error('This is an error message.')
logger.critical('This is a critical message.')
/usr/bin/python2.7 /home/strong/git_workspace/MonoGRNet/test.py /home/strong/git_workspace/MonoGRNet This is a warning message. This is an error message. This is a critical message. Process finished with exit code 0
201906261636.log
2019-06-26 16:36:38,385 - test.py[line:34] - INFO: This is an info message. 2019-06-26 16:36:38,385 - test.py[line:35] - WARNING: This is a warning message. 2019-06-26 16:36:38,385 - test.py[line:36] - ERROR: This is an error message. 2019-06-26 16:36:38,385 - test.py[line:37] - CRITICAL: This is a critical message.
以上這篇Python 實現(xiàn)日志同時輸出到屏幕和文件就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python實現(xiàn)替換word中的關鍵文字(使用通配符)
今天小編就為大家分享一篇python實現(xiàn)替換word中的關鍵文字(使用通配符),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
python3 自動打印出最新版本執(zhí)行的mysql2redis實例
這篇文章主要介紹了python3 自動打印出最新版本執(zhí)行的mysql2redis實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python實現(xiàn)圖形用戶界面和游戲開發(fā)的方法和技巧
GUI圖形用戶界面編程,我們可以通過python提供的豐富的組件,快速的實現(xiàn)使用圖形的界面和用戶交互, GUI編程類似于“搭積?”,將?個個組件(Widget)放到窗?中,這篇文章主要給大家介紹了基于Python的GUI圖形用戶界面編程的相關資料,需要的朋友可以參考下2023-05-05
Tensorflow 如何從checkpoint文件中加載變量名和變量值
這篇文章主要介紹了Tensorflow 如何從checkpoint文件中加載變量名和變量值的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
詳解PyQt5 GUI 接收UDP數(shù)據(jù)并動態(tài)繪圖的過程(多線程間信號傳遞)
這篇文章主要介紹了PyQt5 GUI 接收UDP數(shù)據(jù)并動態(tài)繪圖(多線程間信號傳遞),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09

