打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python 的日志logging模块学习,日志输出屏幕,写入文件
# -*- coding: utf-8 -*-
import logging
import os
import sys
import datetime

log_path="./"

def init_logger(log_path, name="dispnet"):
#创建一个logger
root = logging.getLogger()
#设置日志级别
root.setLevel(logging.NOTSET)

#要保存的日志文件
logfile = os.path.join(log_path, "%s-%s.log" % (name, datetime.datetime.today()))
# 创建一个handler,用于写入日志文件
fileHandler = logging.FileHandler(logfile)
fileHandler.setLevel(logging.INFO)#设置fileHandler的日志级别
root.addHandler(fileHandler)# root添加handler

## 再创建一个handler,用于输出到控制台
consoleHandler = logging.StreamHandler(sys.stdout)
consoleHandler.setLevel(logging.DEBUG)#
consoleHandler.terminator = ""
root.addHandler(consoleHandler)
logging.debug("Logging to %s" % logfile)


init_logger(log_path)
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')


运行之后,屏幕输出:
Logging to ./dispnet-2017-06-09 13:57:53.102921.log
This is debug message
This is info message
This is warning message

同时:生成一个dispnet-2017-06-09 13:57:53.102921.log文件,用于记录屏幕输出的信息。




本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
使用Python的logging.config.fileConfig配置日志
python日志输出—logging配置文件
Java调试的变迁:从System.out.println到log4j
java.util.logging.Logger使用详解
tomcat中的几种log catalina localhost
nginx.conf配置——Nginx
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服