别院牧志知识库 别院牧志知识库
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 辨析

  • Sockets编程

  • Django

  • stackoverflow

  • Flask

  • 全栈之路

    • 基础篇

    • 进阶篇

    • 面向对象

    • 网络编程

    • 操作系统

    • 标准库

      • Python 标准库系列之 collections 模块
      • Python 标准库系列之 configparser 模块
      • Python 标准库系列之 datetime 模块
      • Python 标准库系列之 hashlib 模块
      • Python 标准库系列之模块介绍
      • Python 标准库系列之 json 模块
      • Python 标准库系列之 logging 模块
        • 日志级别
        • 实例
        • 多文件日志
        • 更多参考
      • Python 标准库系列之 os 模块
      • Python 标准库系列之 random 模块
      • Python 标准库系列之 shutil 模块
      • Python 标准库系列之 subprocess 模块
      • Python 标准库系列之 sys 模块
      • Python 标准库系列之 tarfile 模块
      • Python 标准库系列之 time 模块
      • Python 标准库系列之 xml 模块
      • Python 标准库系列之 zipfile 模块
    • 第三方库

    • Python 全栈之路
  • 面试

  • 代码片段

  • 异步编程

  • 😎Awesome资源

  • PEP

  • Python工匠系列

  • 高阶知识点

  • Python 学习资源待整理
  • 设计模式

  • 好“艹蛋”的 Python 呀!
  • FIFO | 待学清单📝
  • pip 安装及使用
  • 数据分析

  • 源码阅读计划

  • OOP

  • 关于 python 中的 setup.py
  • 并行分布式框架 Celery
  • 七种武器,让你的代码提高可维护性
  • 使用 pdb 调试 Python 代码
  • 每周一个 Python 标准库
  • 🐍Python
  • 全栈之路
  • 标准库
佚名
2020-05-23
目录

Python 标准库系列之 logging 模块

This module defines functions and classes which implement a flexible event logging system for applications and libraries.

The key benefit of having the logging API provided by a standard library module is that all Python modules can participate in logging, so your application log can include your own messages integrated with messages from third-party modules.

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

logging 模块用于便捷记录日志且线程安全。

# 日志级别

Level Numeric value
CRITICAL 50
ERROR 40
WARNING 30
INFO 20
DEBUG 10
NOTSET 0

只有大于当前日志等级的操作才会被记录。

# 实例

写入单文件

代码

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# 导入logging模块
import logging

# 创建一个log.log日志文件
logging.basicConfig(filename='log.log',
					# 格式化的字符串
                    format='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s',
                    # 时间
                    datefmt='%Y-%m-%d %H:%M:%S %p',
                    # 错误级别
                    level=logging.NOTSET
                    )

logging.critical('critical')
logging.error('error')
logging.warning('warning')
logging.info('info')
logging.debug('debug')
logging.log(logging.INFO, 'NOTSET')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

执行结果

ansheng@ansheng-me:~$ ls 
log.py
ansheng@ansheng-me:~$ python log.py 
ansheng@ansheng-me:~$ ls
log.log  log.py
ansheng@ansheng-me:~$ cat log.log 
2016-05-27 21:46:15 PM - root - CRITICAL - log: critical
2016-05-27 21:46:15 PM - root - ERROR - log: error
2016-05-27 21:46:15 PM - root - WARNING - log: warning
2016-05-27 21:46:15 PM - root - INFO - log: info
2016-05-27 21:46:15 PM - root - DEBUG - log: debug
2016-05-27 21:46:15 PM - root - INFO - log: NOTSET
1
2
3
4
5
6
7
8
9
10
11
12

logging.basicConfig 函数各参数

参数 说明
filename 指定日志文件名
filemode 和 file 函数意义相同,指定日志文件的打开模式,'w'或'a'
format 指定输出的格式和内容,format 可以输出很多有用信息,如下所示
datefmt 指定时间格式,同 time.strftime()
level 设置日志级别,默认为 logging.WARNING

format 参数

参数 说明
%(levelno)s 打印日志级别的数值
%(levelname)s 打印日志级别名称
%(pathname)s 打印当前执行程序的路径,其实就是 sys.argv[0]
%(filename)s 打印当前执行程序名
%(funcName)s 打印日志的当前函数
%(lineno)d 打印日志的当前行号
%(asctime)s 打印日志的时间
%(thread)d 打印线程 ID
%(threadName)s 打印线程名称
%(process)d 打印进程 ID
%(message)s 打印日志信息

# 多文件日志

对于上述记录日志的功能,只能将日志记录在单文件中,如果想要设置多个日志文件,logging.basicConfig 将无法完成,需要自定义文件和日志操作对象。

#!/usr/bin/env python
# _*_ coding:utf-8 _*_

import logging

# 创建文件
file_1 = logging.FileHandler("log1.log", "a")
# 创建写入的日志格式
fmt1 = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s - %(module)s : %(message)s")
# 文件用格式
file_1.setFormatter(fmt1)

file_2 = logging.FileHandler("log2.log", "a")
fmt2 = logging.Formatter()
file_2.setFormatter(fmt2)

logger1 = logging.Logger("s1", level=logging.ERROR)
logger1.addHandler(file_1)
logger1.addHandler(file_2)

logger1.critical("1111")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 定义文件
file_2_1 = logging.FileHandler('l2_1.log', 'a')
fmt = logging.Formatter()
file_2_1.setFormatter(fmt)

# 定义日志
logger2 = logging.Logger('s2', level=logging.INFO)
logger2.addHandler(file_2_1)
1
2
3
4
5
6
7
8

如上述创建的两个日志对象

  1. 当使用logger1写日志时,会将相应的内容写入 l1_1.log 和 l1_2.log 文件中
  2. 当使用logger2写日志时,会将相应的内容写入 l2_1.log 文件中

# 更多参考

Python 之日志处理(logging 模块) - 云游道士 - 博客园 (opens new window) python logging 模块使用教程 - 简书 (opens new window) 第 32 天:Python logging 模块详解 - 纯洁的微笑博客 (opens new window) Python 日志库 logging 总结-可能是目前为止将 logging 库总结的最好的一篇文章 - 掘金 (opens new window)

编辑 (opens new window)
#编码#面向对象
上次更新: 2024-07-23, 01:00:43
Python 标准库系列之 json 模块
Python 标准库系列之 os 模块

← Python 标准库系列之 json 模块 Python 标准库系列之 os 模块→

最近更新
01
2025 面试记录
05-28
02
提升沟通亲和力的实用策略
03-26
03
工作
07-15
更多文章>
Theme by Vdoing | Copyright © 2019-2025 IMOYAO | 别院牧志
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式