Python牛刀小试(五)--logging模块

简介: 由于想给自己的Python程序添加日志功能,学习下Python的日志模块。一个简单的例子: 点击(此处)折叠或打开 import logging ##第一个简单的例子 logging.
由于想给自己的Python程序添加日志功能,学习下Python的日志模块。

一个简单的例子:

点击(此处)折叠或打开

  1. import logging

  2. ##第一个简单的例子
  3. logging.warning('Watch out!') # will print a message to the console 输出到控制台
  4. logging.info('I told you so') # will not print anything 没有任何输出信息
输出至控制台的信息,如下:

点击(此处)折叠或打开

  1. D:\stock>python test4_logger.py
  2. WARNING:root:Watch out!
输出日志到文件中

点击(此处)折叠或打开

import logging

##输出到example日志文件中
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
在当前目录中,有一个example.log日志文件,输出日志如下:

点击(此处)折叠或打开

DEBUG:root:This message should go to the log file INFO:root:So should this WARNING:root:And this, too
----------------------------------------简单的日志的分割线-----------------------------------------------------------
由于自己写的模块,需要模块化,要有配置文件,能复用。控制台和文件都能输出。
源代码如下:

配置文件如下:

点击(此处)折叠或打开

  1. [loggers]
  2. keys=root,simpleExample
  3. [handlers]
  4. keys=consoleHandler,fileHandler
  5. [formatters]
  6. keys=simpleFormatter
  7. [logger_root]
  8. level=DEBUG
  9. handlers=consoleHandler,fileHandler
  10. [logger_simpleExample]
  11. level=DEBUG
  12. handlers=consoleHandler,fileHandler
  13. qualname=simpleExample
  14. propagate=0
  15. [handler_consoleHandler]
  16. class=StreamHandler
  17. level=DEBUG
  18. formatter=simpleFormatter
  19. args=(sys.stdout,)
  20. [formatter_simpleFormatter]
  21. format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
  22. datefmt=
  23. [handler_fileHandler]
  24. class=logging.handlers.TimedRotatingFileHandler
  25. level=DEBUG
  26. formatter=simpleFormatter
  27. args=("zsd.log", "midnight")

Python执行文件:

点击(此处)折叠或打开

  1. import logging
  2. import logging.config

  3. logging.config.fileConfig('logging.conf')
  4. # create logger
  5. logger = logging.getLogger('simpleExample')

  6. # 'application' code
  7. logger.debug('debug message')
  8. logger.info('info message')
  9. logger.warn('warn message')
  10. logger.error('error message')
  11. logger.critical('critical message')


相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
1天前
|
人工智能 安全 Java
Python 多线程编程实战:threading 模块的最佳实践
Python 多线程编程实战:threading 模块的最佳实践
12 5
|
1天前
|
人工智能 数据库 开发者
Python中的atexit模块:优雅地处理程序退出
Python中的atexit模块:优雅地处理程序退出
8 3
|
4天前
|
开发者 Python
Python的os模块详解
Python的os模块详解
15 0
|
7天前
|
数据挖掘 API 数据安全/隐私保护
python请求模块requests如何添加代理ip
python请求模块requests如何添加代理ip
|
9天前
|
测试技术 Python
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
|
9天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
40 1
|
11天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
55 0
|
12天前
|
Python
python学习14-模块与包
python学习14-模块与包
|
14天前
|
SQL 关系型数据库 数据库
Python中SQLite数据库操作详解:利用sqlite3模块
【4月更文挑战第13天】在Python编程中,SQLite数据库是一个轻量级的关系型数据库管理系统,它包含在一个单一的文件内,不需要一个单独的服务器进程或操作系统级别的配置。由于其简单易用和高效性,SQLite经常作为应用程序的本地数据库解决方案。Python的内置sqlite3模块提供了与SQLite数据库交互的接口,使得在Python中操作SQLite数据库变得非常容易。
|
19天前
|
索引 Python
「Python系列」Python operator模块、math模块
Python的`operator`模块提供了一系列内置的操作符函数,这些函数对应于Python语言中的内建操作符。使用`operator`模块可以使代码更加清晰和易读,同时也能提高性能,因为它通常比使用Python内建操作符更快。
28 0