zoukankan      html  css  js  c++  java
  • python日志模块logging

    import logging.handlers
    import os
    
    #baseconfig
    logger=logging.getLogger()
    logger.setLevel(logging.DEBUG)
    #设置log文件所在的路径和文件名名
    log_path=os.path.dirname(os.path.abspath(__file__))
    logname=log_path+'/'+'test.log'
    
    fh=logging.handlers.TimedRotatingFileHandler(logname,when='s',interval=1,backupCount=2,encoding="utf-8")
    #设置日志输出格式
    formater=logging.Formatter(fmt="%(asctime)s-%(filename)s-%(module)s-[line:%(lineno)d]%(levelname)s-%(message)s",datefmt="%Y-%m-%d %X")
    
    #创建一个file handler
    fh=logging.FileHandler(logname,mode='a')
    fh.setLevel(logging.DEBUG)
    
    #创建一个串口console handler
    ch=logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    
    #为logger添加日志处理器
    logger.addHandler(fh)
    logger.addHandler(ch)
    
    #为logger制定日志输出格式
    ch.setFormatter(formater)
    fh.setFormatter(formater)
    
    #日志消息
    logger.info("this is a info message")
    logger.debug("this is a debug message")
    logger.critical("this is a critical messsage")
    logger.error("this is a error message")
    logger.warning("this is a warning message")
    

     执行效果:

    控制台输入:

     文件输出:

  • 相关阅读:
    (转载)Bonding技术指南
    Linux配置虚拟地址
    VB6之写注册表
    Tomcat集群搭建
    VBS连接远程Oracle
    机器学习 目标函数,损失函数
    深度学习理解内容 初
    leetcode 39. Combination Sum
    leetcode 33. Search in Rotated Sorted Array
    leetcode 29. Divide Two Integers
  • 原文地址:https://www.cnblogs.com/maisha/p/12744947.html
Copyright © 2011-2022 走看看