在Python编程中,日志是很重要的,不仅可以帮助开发者调试程序,还能够在应用程序出现故障时快速定位问题。Python内置了日志模块,可以用来方便地记录和输出日志信息。本文从多个方面介绍Python如何打印日志。
一、Python打印日志信息
1、Python中的日志函数有:debug(), info(), warning(), error(), critical()。除了debug()以外,info()、warning()、error()、critical()这四种日志函数都有相应的level,分别对应日志记录的级别。
import logging
logging.warning("这是一个警告")
logging.error("这是一个错误")
logging.info("这是一条信息")
logging.debug("这是一条调试信息")
logging.critical("这是一个严重的错误")
2、Python的日志记录器(logger)允许按层次结构来设定不同的日志级别,从而实现对各个模块的日志处理。
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)
logger.debug("This is a debug message")
logger.info("This is an information")
logger.warning("This is a warning")
logger.error("This is an error")
logger.critical("This is a critical")
二、Python打印网页
在Python中,可以使用requests库来获取网页内容,并使用logging模块打印出来。
import requests import logging url = "http://www.example.com" res = requests.get(url) logging.debug(res.text)
三、Python打印日志函数
在Python中,使用logging模块的basicConfig函数或者dictConfig函数来设定日志格式,代码示例如下:
import logging
logging.basicConfig(filename='example.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This message will be logged')
四、Python打印日历
使用Python中的calendar模块,可以实现打印日历,代码示例如下:
import logging import calendar c = calendar.TextCalendar(calendar.MONDAY) str = c.formatmonth(2018,10) logging.info(str)
五、Python打印日志到屏幕
Python的logging模块默认把日志输出到了标准错误,可以通过basicConfig函数的stream参数来改变输出的位置,代码示例如下:
import logging
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This message will be printed on stdout')
六、Python打印日志没有行号
有时候在打印日志的时候,输出中会出现行号,可以使用Formatter类去掉行号:
import logging
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
formatter = logging.Formatter(log_format)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.debug('Debugging...')
七、Python打印图纸
使用Python的turtle模块画图,然后使用logging模块打印图形:
import turtle
import logging
logging.basicConfig(filename='example.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
t = turtle.Turtle()
for i in range(4):
t.fd(100)
t.lt(90)
logging.info("Drawing a square using turtle module")
八、Python打印log日志
使用Python的logging模块可以方便地打印log日志:
import logging
logging.basicConfig(filename='example.log', level=logging.DEBUG, format='%(asctime)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')
九、Python打印图形
使用Python的matplotlib模块绘制图形,并将图形信息打印到日志文件中:
import matplotlib.pyplot as plt
import numpy as np
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
x = np.arange(0,2*np.pi,0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()
logging.info("Drawing a sine wave using matplotlib module")
本文介绍了Python打印日志的多个方面,包括日志函数、日志记录器、打印网页、打印日历、打印日志到屏幕、去掉行号、打印图纸、打印log日志以及打印图形等。使用Python的logging模块,可以方便地记录和输出日志信息,帮助开发者轻松调试程序。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/294060.html
微信扫一扫
支付宝扫一扫