python 通过logging写入日志到文件和控制台的实例
作者:jingxian 发布时间:2021-04-26 00:04:38
标签:python,logging,文件,控制台
如下所示:
import logging
# 创建一个logger
logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler('test.log')
fh.setLevel(logging.DEBUG)
# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# 定义handler的输出格式
formatter = logging.Formatter('[%(asctime)s][%(thread)d][%(filename)s][line: %(lineno)d][%(levelname)s] ## %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# 给logger添加handler
logger.addHandler(fh)
logger.addHandler(ch)
# 记录一条日志
logger.info('foorbar')
关于formatter的配置,采用的是%(<dict key>)s的形式,就是字典的关键字替换。提供的关键字包括:
Format | Description |
---|---|
%(name)s | Name of the logger (logging channel). |
%(levelno)s | Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL). |
%(levelname)s | Text logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'). |
%(pathname)s | Full pathname of the source file where the logging call was issued (if available). |
%(filename)s | Filename portion of pathname. |
%(module)s | Module (name portion of filename). |
%(funcName)s | Name of function containing the logging call. |
%(lineno)d | Source line number where the logging call was issued (if available). |
%(created)f | Time when the LogRecord was created (as returned by time.time()). |
%(relativeCreated)d | Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded. |
%(asctime)s | Human-readable time when the LogRecord was created. By default this is of the form “2003-07-08 16:49:45,896” (the numbers after the comma are millisecond portion of the time). |
%(msecs)d | Millisecond portion of the time when the LogRecord was created. |
%(thread)d | Thread ID (if available). |
%(threadName)s | Thread name (if available). |
%(process)d | Process ID (if available). |
%(message)s | The logged message, computed as msg % args. |
来源:https://blog.csdn.net/shjh369/article/details/50585505


猜你喜欢
- Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进
- 首先说明一下SQL Server内存占用由哪几部分组成。SQL Server占用的内存主要由三部分组成:数据缓存(Data Buffer)、
- 正则表达式在 PHP 中的应用在 PHP 应用中,正则表达式主要用于:•正则匹配:根据正则表达式匹配相应的内容•正则替换:根据正则表达式匹配
- 是在客户端确认还是在服务器端确认? <SCRIPT LANGUAGE="VBSc
- PHP getName() 函数实例返回 XML 元素及其子元素的名称:<?php $xml=<<<XML<?
- 通常说到外键,只会提到“外键的目的是确定资料的参考完整性(referential integrity)。”,但是外键具体包含哪些动作和含义呢
- 前言在跑项目时,常常会安装很多的包,也通常会遇到需要安装指定版本的包,以及包与包不兼容的问题。比如:在项目1中安装librosa时,会自动安
- 1,SELECT 语句 在SQL的世界里,最最基础的操作就是SELECT 语句了。在数据库工具下直接采用SQL
- JS脚本语言的基础语法:输出语法 alert("警告!"); confirm("确定吗
- 最近自己在抢冰墩墩钥匙扣,发现一秒瞬间就没了。于是自己网上学习了一下,写了一个抢购脚本。亲测可用。具体使用步骤如下:一、官网下载火狐浏览器二
- 关于数据库的逻辑设计,是一个很广泛的问题。本文主要针对开发应用中遇到在MS SQL Server上进行表设计时,对表的主键设计应注意的问题以
- 一、 背景介绍web应用采用的是ssh框架,数据库使用的sql server2014版本。二、问题:客户要求,ID列的数据类型必须是uniq
- 前言:看似简单的任务,往往隐藏陷阱!一个常见的任务是在一个列表上迭代,并根据条件删除一些元素。本文将展示如何完成该任务的不同方法,同时展示一
- 前言如果电脑是第一次安装MySQL,一般不会出现这样的报错。如下图所示。starting the server失败,通常是因为上次安装的该软
- pytorch 库pytorch 本身具有载入cifar10等数据集的函数,但是载入的是3*200*200的张量,当碰到要使用灰度图像时,可
- 本文实例讲述了php隐藏IP地址后两位显示为星号的方法。分享给大家供大家参考。具体实现方法如下:我们在很多的公共网站中都会有碰到显示用户的I
- 本文用的是sciki-learn库的iris数据集进行测试。用的模型也是最简单的,就是用贝叶斯定理P(A|B) = P(B|A)*P(A)/
- 什么是存储过程存储过程(Stored Procedure)也成为存储程序,是一种在数据库中存储复杂程序,以便外部程序调用的一种数据库对象。即
- MySql版本问题sql_mode=only_full_group_by查看sql_modeselect @@sql_mode查询出来的值为
- 1.sort()方法sort()是列表的方法,修改原列表使得它按照大小排序,没有返回值,返回NoneIn [90]: x = [4, 6,