zoukankan      html  css  js  c++  java
  • Python logging模块

    # -*- coding: utf-8 -*-

    import logging
    import sys

    # 获取logger实例,如果参数为空则返回root logger
    logger = logging.getLogger("AppName")

    # 指定logger输出格式
    formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s')

    # 文件日志
    file_handler = logging.FileHandler("test.log")
    file_handler.setFormatter(formatter)  # 可以通过setFormatter指定输出格式

    # 控制台日志
    console_handler = logging.StreamHandler(sys.stdout)
    console_handler.formatter = formatter  # 也可以直接给formatter赋值

    # 为logger添加的日志处理器
    logger.addHandler(file_handler)
    logger.addHandler(console_handler)

    # 指定日志的最低输出级别,默认为WARN级别
    logger.setLevel(logging.INFO)

    # 输出不同级别的log
    logger.debug('this is debug info')
    logger.info('this is information')
    logger.warn('this is warning message')
    logger.error('this is error message')
    logger.fatal('this is fatal message, it is same as logger.critical')
    logger.critical('this is critical message')

    # 2016-10-08 21:59:19,493 INFO    : this is information
    # 2016-10-08 21:59:19,493 WARNING : this is warning message
    # 2016-10-08 21:59:19,493 ERROR   : this is error message
    # 2016-10-08 21:59:19,493 CRITICAL: this is fatal message, it is same as logger.critical
    # 2016-10-08 21:59:19,493 CRITICAL: this is critical message

    # 移除一些日志处理器
    logger.removeHandler(file_handler)

  • 相关阅读:
    Hbase shell 常用命令
    HTable基本概念
    通过HBase Shell与HBase交互
    把Nutch爬虫部署到Hadoop集群上
    wso2esb安装及helloworld
    nDPI 的论文阅读和机制解析
    Ubuntu 编译出现 ISO C++ 2011 不支持的解决办法
    404 Note Found 队-课堂实战-项目UML设计
    nDPI的安装与测试
    精读 SBAR SDN flow-Based monitoring and Application Recognition
  • 原文地址:https://www.cnblogs.com/yingfei/p/8546857.html
Copyright © 2011-2022 走看看