zoukankan      html  css  js  c++  java
  • Python logger /logging

    # !/user/bin/python
    # -*- coding: utf-8 -*-
    '''
    subprocess : 需要在linux平台上测试 shell
    logging
    '''
    
    import logging
    # 将日志输出在文件里
    # logging.basicConfig(filename="app.log", level=logging.DEBUG)
    logging.basicConfig(filename="app.log",
                        level=logging.WARNING,
                        format='%(asctime)s %(levelname)s  %(filename)s:%(lineno)d  - %(message)s',
                        datefmt='%m/%d/%Y %I:%M:%S %p')  # 在日志上加上时间. %p代表pm.  TODO 为什么没打出行数?
    logging.debug("test debug")
    logging.info("test info")
    logging.error("test error")
    logging.warning("User [alex] attempted wrong password more than 3 times")
    
    
    # 同时将日志打印在屏幕上并输出在文件里
    # step 1, create logger
    logger = logging.getLogger("TEST-LOG")
    logger.setLevel(logging.DEBUG)
    
    # step2, create console handler and set level to debug
    ch=logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    
    # step3, create file handler and set level to warning
    fh = logging.FileHandler("process.log")
    fh.setLevel(logging.ERROR)
    
    # step3, define format
    fh_formatter = logging.Formatter('%(asctime)s %(levelname)s  %(filename)s:%(lineno)d  - %(message)s')
    ch_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    
    fh.setFormatter(fh_formatter)
    ch.setFormatter(ch_formatter)
    
    # step4, connect handlers to logger
    logger.addHandler(fh)
    logger.addHandler(ch)
    
    logger.warning("ddddd")
    LevelWhen it’s used
    DEBUG Detailed information, typically of interest only when diagnosing problems.
    INFO Confirmation that things are working as expected.
    WARNING An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
    ERROR Due to a more serious problem, the software has not been able to perform some function.
    CRITICAL A serious error, indicating that the program itself may be unable to continue running.
  • 相关阅读:
    vue从入门到进阶:自定义指令directive,插件的封装以及混合mixins(七)
    js模板引擎mustache介绍及实例
    vue v-cloak 的作用和用法
    vue中$event理解和框架中在包含默认值外传参
    Node.js如何设置允许跨域
    前端常见跨域解决方案(全)
    http-server使用教程 hs -o
    JMeter性能测试,完整入门篇
    Java源码初学_LinkedHashMap
    Java源码初学_HashMap
  • 原文地址:https://www.cnblogs.com/cheese320/p/9061275.html
Copyright © 2011-2022 走看看