首页 > 编程语言 >python 日志写入文件

python 日志写入文件

时间:2022-08-17 19:14:55浏览次数:53  
标签:line logging Aug 17 python 18 写入 日志 logger

import logging


fmt = "%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s: %(message)s"
logging.basicConfig(
    level=logging.DEBUG,
    format=fmt,
    filename="D:\logs-220817.txt",
    filemode="w",
    datefmt="%a, %d %b %Y %H:%M:%S"
)

logging.debug("this is debug logger")
logging.info("this is info logger")

logging.warn("this is warn logger")
logging.error("this is error logger")
logging.critical("this is critical logger")

# console输出
# WARNING:root:this is warn logger
# ERROR:root:this is error logger
# CRITICAL:root:this is critical logger


# 文件输出
# Wed, 17 Aug 2022 18:59:23 testlogger.py[line:13] DEBUG: this is debug logger
# Wed, 17 Aug 2022 18:59:23 testlogger.py[line:14] INFO: this is info logger
# Wed, 17 Aug 2022 18:59:23 testlogger.py[line:16] WARNING: this is warn logger
# Wed, 17 Aug 2022 18:59:23 testlogger.py[line:17] ERROR: this is error logger
# Wed, 17 Aug 2022 18:59:23 testlogger.py[line:18] CRITICAL: this is critical logger

 

标签:line,logging,Aug,17,python,18,写入,日志,logger
From: https://www.cnblogs.com/oktokeep/p/16596457.html

相关文章

  • 【python error】FutureWarning: The error_bad_lines argument has been deprecated
    前言博主运行python代码的时候出现了warning,主要是模块版本引起的。drawlog.py  warningdrawlog.py:76:FutureWarning:Theerror_bad_linesargumenthasbeend......
  • 【python基础】super的理解
    super() 函数是用于调用父类(超类)的一个方法。super() 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序......
  • 【python基础】os.listdir乱序问题
    前言想要获取之前某个目录的有序文件,除了文件名称,其他的比如文件内容、创建时间等都发生了改变,不清楚使用os.listdir是否会改变前后的文件排序。可以使用帮助文档查看os.......
  • Python 字符串插值 All In One
    Python字符串插值AllInOne#!/usr/bin/envpython3#coding=utf-8__author__='xgqfrms'__editor__='vscode'__version__='1.0.1'__copyright__="""Co......
  • 【InventWithPython 第一部分】校对活动正式启动
    仓库:https://github.com/apachecn/invent-with-python-zh整体进度:https://github.com/apachecn/invent-with-python-zh/issues/1贡献指南:https://github.com/apachecn/in......
  • python 中的re 常用命令
    importrephoneNumRegex=re.compile(r'zhang(wei|yang|hao)')mo=phoneNumRegex.search('mynumberzhangwei,zhangyang')print(mo.groups())#?前面字符是可选......
  • Python快速排序
    defquicksort(array):less=[]greater=[]iflen(array)<=1:returnarraypivot=array.pop()forxinarray:ifx<=p......
  • Python & PEP 8 & Style Guide All In One
    Python&PEP8&StyleGuideAllInOnePEP8–StyleGuideforCodehttps://peps.python.org/pep-0008/'单引号'vs"双引号"https://peps.python.org/pep-0......
  • 13行python代码实现对微信进行推送消息
    Python可以实现给QQ邮箱、企业微信、微信等等软件推送消息,今天咱们实现一下Python直接给微信推送消息。这里咱们使用了一个第三方工具pushplus单人推送实现步骤......
  • [Python学习笔记]Python基础-12 面向对象编程_属性和方法
    内置属性可以使用内置函数dir查看一个对象支持的所有属性和方法,Python中存在很多的内置属性classPerson(object):def__init__(self,name,age)->None:......