yongjieShi

Python中log的简单粗暴的设置和使用

前沿

这个博文只会讲自己曾经使用的一种case,方便拿过来直接用,能都达到的目标是

  1. 将重要的信息输出到屏幕上的同时,
  2. 能够记录在日志之中,方便查看程序运行过程的输出啥的
    下面的程序可以直接拿过来用

设置

import logging
# 关键是声明下面的init函数
def initLogging(logFilename):
    logging.basicConfig(level=logging.DEBUG, format=\'%(asctime)s-%(levelname)s-%(message)s\', datefmt=\'%y-%m-%d- %H:%M\', filename=logFilename, filemode=\'w\')
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    formatter = logging.Formatter(\'%(asctime)s-%(levelname)s-%(message)s\')
    console.setFormatter(formatter)
    logging.getLogger(\'\').addHandler(console)

使用

首先调用initLogging函数,传入要写入的log的名称

initLogging(\'./output4.log\')
# 使用的时候直接logging.info(要打印的字符串)
# 比如
logging.info(\'123\')

这个时候能够打印到屏幕上的同时,也能够记录在output4.log这个文档之中,方便terminal挂起的时候从log查看程序的输出

分类:

技术点:

相关文章:

  • 2021-11-21
  • 2021-09-22
  • 2022-03-02
  • 2022-02-07
  • 2021-05-30
  • 2021-12-14
  • 2022-12-23
  • 2021-08-04
猜你喜欢
  • 2021-12-17
  • 2021-12-17
  • 2021-08-30
  • 2021-12-10
  • 2022-12-23
相关资源
相似解决方案