欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 實(shí)現(xiàn)日志同時(shí)輸出到屏幕和文件

 更新時(shí)間:2020年02月19日 15:27:20   作者:ForeverStrong  
這篇文章主要介紹了Python 實(shí)現(xiàn)日志同時(shí)輸出到屏幕和文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

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.')

默認(rèn)的 level 是 logging.WARNING,低于這個(gè)級(jí)別的就不輸出了。如果需要顯示低于 logging.WARNING 級(jí)別的內(nèi)容,可以引入 logging.NOTSET 級(jí)別來顯示。

DEBUG - 打印全部的日志。詳細(xì)的信息,通常只出現(xiàn)在診斷問題上。

INFO - 打印 INFO、WARNING、ERROR、CRITICAL 級(jí)別的日志。確認(rèn)一切按預(yù)期運(yùn)行。

WARNING - 打印 WARNING、ERROR、CRITICAL 級(jí)別的日志。表明一些問題在不久的將來,這個(gè)軟件還能按預(yù)期工作。

ERROR - 打印 ERROR、CRITICAL 級(jí)別的日志。更嚴(yán)重的問題,軟件沒能執(zhí)行一些功能。

CRITICAL : 打印 CRITICAL 級(jí)別。一個(gè)嚴(yán)重的錯(cuò)誤,表明程序本身可能無法繼續(xù)運(yùn)行。

/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. 日志同時(shí)輸出到屏幕和文件

#!/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 實(shí)現(xiàn)日志同時(shí)輸出到屏幕和文件就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Pandas中的loc與iloc區(qū)別與用法小結(jié)

    Pandas中的loc與iloc區(qū)別與用法小結(jié)

    loc函數(shù):通過行索引 “Index” 中的具體值來取行數(shù)據(jù)(如取"Index"為"A"的行)而iloc函數(shù):通過行號(hào)來取行數(shù)據(jù)(如取第二行的數(shù)據(jù)),這篇文章介紹Pandas中的loc與iloc區(qū)別與用法,感興趣的朋友一起看看吧
    2024-01-01
  • Python使用gRPC實(shí)現(xiàn)數(shù)據(jù)分析能力的共享

    Python使用gRPC實(shí)現(xiàn)數(shù)據(jù)分析能力的共享

    gRPC是一個(gè)高性能、開源、通用的遠(yuǎn)程過程調(diào)用(RPC)框架,由Google推出,本文主要介紹了Python如何使用gRPC實(shí)現(xiàn)數(shù)據(jù)分析能力的共享,感興趣的可以了解下
    2024-02-02
  • Python實(shí)現(xiàn)字符串與數(shù)組相互轉(zhuǎn)換功能示例

    Python實(shí)現(xiàn)字符串與數(shù)組相互轉(zhuǎn)換功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)字符串與數(shù)組相互轉(zhuǎn)換功能,結(jié)合具體實(shí)例形式分析了Python字符串與數(shù)組相關(guān)轉(zhuǎn)換功能的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下
    2017-09-09
  • Python實(shí)現(xiàn)的插入排序算法原理與用法實(shí)例分析

    Python實(shí)現(xiàn)的插入排序算法原理與用法實(shí)例分析

    這篇文章主要介紹了Python實(shí)現(xiàn)的插入排序算法原理與用法,簡(jiǎn)單描述了插入排序的原理,并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)插入排序的相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • python使用PyCharm進(jìn)行遠(yuǎn)程開發(fā)和調(diào)試

    python使用PyCharm進(jìn)行遠(yuǎn)程開發(fā)和調(diào)試

    這篇文章主要介紹了python使用PyCharm進(jìn)行遠(yuǎn)程開發(fā)和調(diào)試,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • pycharm新建Vue項(xiàng)目的方法步驟(圖文)

    pycharm新建Vue項(xiàng)目的方法步驟(圖文)

    這篇文章主要介紹了pycharm新建Vue項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • 詳解超星腳本出現(xiàn)亂碼問題的解決方法(Python)

    詳解超星腳本出現(xiàn)亂碼問題的解決方法(Python)

    超星助手是一款為孩子們提供學(xué)習(xí)的軟件,支持用戶們后臺(tái)運(yùn)行多開等,還可以簽到,查題等多功能,下面這篇文章主要給大家介紹了關(guān)于超星腳本出現(xiàn)亂碼問題的解決方法,需要的朋友可以參考下
    2022-05-05
  • python?snap7讀寫PLC的操作方法

    python?snap7讀寫PLC的操作方法

    這篇文章主要介紹了python?snap7讀寫PLC的操作方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-02-02
  • 使用python實(shí)現(xiàn)將視頻中的音頻分離出來

    使用python實(shí)現(xiàn)將視頻中的音頻分離出來

    這篇文章主要介紹了使用python實(shí)現(xiàn)將視頻中的音頻分離出來,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • python numpy 一維數(shù)組轉(zhuǎn)變?yōu)槎嗑S數(shù)組的實(shí)例

    python numpy 一維數(shù)組轉(zhuǎn)變?yōu)槎嗑S數(shù)組的實(shí)例

    今天小編就為大家分享一篇python numpy 一維數(shù)組轉(zhuǎn)變?yōu)槎嗑S數(shù)組的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07

最新評(píng)論