Python 实现日志同时输出到屏幕和文件

 更新时间:2020年02月19日 15:27:20   作者:ForeverStrong  
这篇文章主要介绍了Python 实现日志同时输出到屏幕和文件,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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 - 打印全部的日志。详细的信息,通常只出现在诊断问题上。

INFO - 打印 INFO、WARNING、ERROR、CRITICAL 级别的日志。确认一切按预期运行。

WARNING - 打印 WARNING、ERROR、CRITICAL 级别的日志。表明一些问题在不久的将来,这个软件还能按预期工作。

ERROR - 打印 ERROR、CRITICAL 级别的日志。更严重的问题,软件没能执行一些功能。

CRITICAL : 打印 CRITICAL 级别。一个严重的错误,表明程序本身可能无法继续运行。

/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 实现日志同时输出到屏幕和文件就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python实现替换word中的关键文字(使用通配符)

    python实现替换word中的关键文字(使用通配符)

    今天小编就为大家分享一篇python实现替换word中的关键文字(使用通配符),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • 简单了解python单例模式的几种写法

    简单了解python单例模式的几种写法

    这篇文章主要介绍了简单了解python单例模式的几种写法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • python3 自动打印出最新版本执行的mysql2redis实例

    python3 自动打印出最新版本执行的mysql2redis实例

    这篇文章主要介绍了python3 自动打印出最新版本执行的mysql2redis实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • Python实现图形用户界面和游戏开发的方法和技巧

    Python实现图形用户界面和游戏开发的方法和技巧

    GUI图形用户界面编程,我们可以通过python提供的丰富的组件,快速的实现使用图形的界面和用户交互, GUI编程类似于“搭积⽊”,将⼀个个组件(Widget)放到窗⼝中,这篇文章主要给大家介绍了基于Python的GUI图形用户界面编程的相关资料,需要的朋友可以参考下
    2023-05-05
  • Anaconda环境改名的实现步骤

    Anaconda环境改名的实现步骤

    本文主要介绍了Anaconda环境改名的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • pytorch加载自己的数据集源码分享

    pytorch加载自己的数据集源码分享

    这篇文章主要介绍了pytorch加载自己的数据集源码分享,标准的数据集流程梳理分为数据准备以及加载数据库–>数据加载器的调用或者设计–>批量调用进行训练或者其他作用,需要的朋友可以参考下
    2022-08-08
  • Tensorflow 如何从checkpoint文件中加载变量名和变量值

    Tensorflow 如何从checkpoint文件中加载变量名和变量值

    这篇文章主要介绍了Tensorflow 如何从checkpoint文件中加载变量名和变量值的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • Python的条件控制 if 语句详解

    Python的条件控制 if 语句详解

    Python的 if 语句用来「控制代码」的执行,「判断条件成立」时执行一段代码,判断条件「不成立」时执行另一段代码,本文就给大家详细讲讲Python的条件控制 if 语句,需要的朋友可以参考下
    2023-08-08
  • 详解PyQt5 GUI 接收UDP数据并动态绘图的过程(多线程间信号传递)

    详解PyQt5 GUI 接收UDP数据并动态绘图的过程(多线程间信号传递)

    这篇文章主要介绍了PyQt5 GUI 接收UDP数据并动态绘图(多线程间信号传递),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • Python实现批量修改文件名实例

    Python实现批量修改文件名实例

    这篇文章主要介绍了Python实现批量修改文件名的方法,实例分析了两种实现批量修改文件名的技巧,涉及os.rename重命名方法、正则替换及字符串操作的相关技巧,需要的朋友可以参考下
    2015-07-07

最新评论