官方文档:https://docs.python.org/2/library/logging.html

logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有

filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。

filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。

format:指定handler使用的日志显示格式。

datefmt:指定日期时间格式。

level:设置rootlogger; 默认的日志级别设置为WARNING(日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET)

stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。

只打印到控制台

# coding:utf-8
import logging
import sys # 日志格式化方式
#LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" LOG_FORMAT = "%(asctime)s\tFile \"%(filename)s\",LINE %(lineno)-4d : %(levelname)-8s %(message)s"
# 日期格式化方式
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
def initLogging(logFilename): #logging.basicConfig(filename=logFilename, level=logging.DEBUG, format=LOG_FORMAT)
#formatter = logging.Formatter(LOG_FORMAT); #handler=logging.FileHandler(logFilename)
#handler.setLevel(logging.DEBUG)
#handler.setFormatter(formatter)
#logging.getLogger('').addHandler(handler); logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT) #console = logging.StreamHandler();
#console.setLevel(logging.INFO);
#console.setFormatter(formatter);
#logging.getLogger('').addHandler(console); initLogging("mylog.txt") logging.info("Hello world")

只打印到文件

# coding:utf-8
import logging
import sys # 日志格式化方式
#LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" LOG_FORMAT = "%(asctime)s\tFile \"%(filename)s\",LINE %(lineno)-4d : %(levelname)-8s %(message)s"
# 日期格式化方式
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
def initLogging(logFilename): logging.basicConfig(filename=logFilename, level=logging.DEBUG, format=LOG_FORMAT)
#formatter = logging.Formatter(LOG_FORMAT); #handler=logging.FileHandler(logFilename)
#handler.setLevel(logging.DEBUG)
#handler.setFormatter(formatter)
#logging.getLogger('').addHandler(handler); #logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT) #console = logging.StreamHandler();
#console.setLevel(logging.INFO);
#console.setFormatter(formatter);
#logging.getLogger('').addHandler(console); initLogging("mylog.txt") logging.info("Hello world")

同时打印到控制台和文件

# coding:utf-8
import logging
import sys # 日志格式化方式
#LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" LOG_FORMAT = "%(asctime)s\tFile \"%(filename)s\",LINE %(lineno)-4d : %(levelname)-8s %(message)s"
# 日期格式化方式
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
def initLogging(logFilename): logging.basicConfig(filename=logFilename, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
formatter = logging.Formatter(LOG_FORMAT); #handler=logging.FileHandler(logFilename)
#handler.setLevel(logging.DEBUG)
#handler.setFormatter(formatter)
#logging.getLogger('').addHandler(handler); #logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT) console = logging.StreamHandler();
console.setLevel(logging.INFO);
console.setFormatter(formatter);
logging.getLogger('').addHandler(console); initLogging("mylog.txt") logging.info("Hello world")

https://www.cnblogs.com/heenhui2016/p/11424154.html

https://blog.csdn.net/energysober/article/details/53263295

Python日志记录的更多相关文章

  1. Python 日志记录与程序流追踪(基础篇)

    日志记录(Logging) More than print: 每次用 terminal debug 时都要手动在各种可能出现 bug 的地方 print 相关信息来确认 bug 的位置: 每次完成 d ...

  2. Python日志记录(Logging)

    日志记录跟程序的测试相关,并且在大幅度更改程序内核时很有用,它可以帮助我们找到问题和错误的所在.日志记录基本上就是收集与程序运行有关的数据,这样可以在随后进行检查或者累计数据. 1.简单示例 在Pyt ...

  3. python日志记录-logging模块

    1.logging模块日志级别 使用logging模块简单示例: >>>import logging >>>logging.debug("this's a ...

  4. [蟒蛇菜谱]Python日志记录最佳实践

    # -*- coding: utf8 -*- import logging # 创建一个logger logger = logging.getLogger('mylogger') logger.set ...

  5. Python日志记录(logging)

    import logging logfile = 'e:\\a.txt' # logging.basicConfig(filename=logfile,level=logging.INFO) # lo ...

  6. python中利用logging包进行日志记录时的logging.level设置选择

    之前在用python自带的logging包进行日志输出的时候发现有些logging语句没有输出,感到比较奇怪就去查了一下logging文档.然后发现其在设置和引用时的logging level会影响最 ...

  7. Python开发之日志记录模块:logging

    1 引言 最近在开发一个应用软件,为方便调试和后期维护,在代码中添加了日志,用的是Python内置的logging模块,看了许多博主的博文,颇有所得.不得不说,有许多博主大牛总结得确实很好.似乎我再写 ...

  8. python添加fluent日志记录

    istio默认会进行日志的记录,但是仅仅记录到服务.以及服务之间调用的信息,不记录业务日志. 如: 所以需要添加业务日志记录. 1.python引入package fluentmsgpack 2.代码 ...

  9. Python的日志记录-logging模块的使用

    一.日志 1.1什么是日志 日志是跟踪软件运行时所发生的事件的一种方法,软件开发者在代码中调用日志函数,表明发生了特定的事件,事件由描述性消息描述,同时还包含事件的重要性,重要性也称为级别或严重性. ...

随机推荐

  1. Sentinel流控与熔断

    参考: https://thinkwon.blog.csdn.net/article/details/103770879 项目结构 com.guo     ├── guo-sentinel       ...

  2. php微信jsapi支付 支付宝支付 两码合一

    产品开会提出了这样的需求:一个二维码可以微信支付也可以支付宝支付 经过自己的钻研以及询问技术高人(本人代码一般般)和网上搜索 最终实现其功能  我用微信jsapi 和 支付宝网页支付 其实并不怎么难: ...

  3. ESP32音频输入-MAX4466,MAX9814,SPH0645LM4H,INMP441(翻译)

    有几种方法可以将模拟音频数据输入到ESP32中. 直接从内置的模数转换器(ADC)读取 这对于一次性读取很有用,但不适用于高采样率. 使用I2S通过DMA读取内置ADC 适用于模拟麦克风,例如MAX4 ...

  4. shell编程之循环语句for / while / until

    shell编程之循环语句与函数 一.条件测试 二.循环语句 ① for循环语句结构(遍历) 示例1 示例2 ② while循环语句结构(迭代) 示例1 示例2 ③ until 循环语句结构 示例1 一 ...

  5. 【动画消消乐】HTML+CSS 自定义加载动画 062

    效果展示 Demo代码 HTML <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  6. ajax请求对返回数据data的处理

    1,ajax请求会根据响应头的返回数据类型对返回的数据data变量进行不同的处理 $.get("data/user-permission-submit-" + ddo.manipu ...

  7. JAVA-Scaneer对象

    Scanner对象 我们可以通过scanner来获取用户的输入 基本语法 Scanner s = new Scanner(System.in); nextLine():输入 import java.u ...

  8. [考试总结]noip模拟16

    达成成就,一天更3篇总结. 又是一个暴力场 别问我为什么开局 \(5\) 分钟就问老师为什么 \(T3\) 没有提交的窗口. 开题读题,一路自闭到 \(T3\) ,发现 \(T3\) 可打暴力,所以一 ...

  9. 流畅的python学习1

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Apr 20 21:08:08 202 ...

  10. 第四篇 -- Go语言string转其他类型

    1. string转int // 法1:string转int num_str := "1234567" /* ParseInt():查看文档https://studygolang. ...