zoukankan      html  css  js  c++  java
  • [Python] logging.logger

    <1>.

    mylogger = logging.getLogger("abc")
    

    logging.debug()/logging.info()/logging.warning()/logging.error()等使用的是全局的root logger实例, mylogger是一个新实例,它默认继承root logger的设置。

    The root of the hierarchy of loggers is called the root logger. That’s the logger used by the functions debug(), info(), warning(), error() and critical(), which just call the same-named method of the root logger. The functions and the methods have the same signatures. The root logger’s name is printed as 'root' in the logged output.

    <2>.
    A good convention to use when naming loggers is to use a module-level logger, in each module which uses logging, named as follows:

    logger = logging.getLogger(__name__)
    

    This means that logger names track the package/module hierarchy, and it’s intuitively obvious where events are logged just from the logger name.

    <3>.
    getLogger() returns a reference to a logger instance with the specified name if it is provided, or root if not. The names are period-separated hierarchical structures. Multiple calls to getLogger() with the same name will return a reference to the same logger object.


    def loggerDemo():
        logging.basicConfig(filemode="w", level=logging.DEBUG)  #filemode not working for logger()?
    
        loggerOut = logging.getLogger("output")
        #loggerOut.setLevel(logging.DEBUG)
        #创建一个handler,用于写入日志文件
        fh = logging.FileHandler(os.path.join(os.getcwd(),"{0}_debug.log".format(__name__ if __name__ != "__main__" else "basicPython")))
        formatter = logging.Formatter("%(asctime)s - %(message)s")
        fh.setFormatter(formatter)
        loggerOut.addHandler(fh)
        loggerOut.debug("hello in Logger(Output)")
    
        loggerErr = logging.getLogger("error")
        #loggerErr.setLevel(logging.ERROR)
        #创建一个handler,用于写入日志文件
        fh = logging.FileHandler(os.path.join(os.getcwd(),"{0}_error.log".format(__name__ if __name__ != "__main__" else "basicPython")))
        formatter = logging.Formatter("%(asctime)s - %(message)s")
        fh.setFormatter(formatter)
        loggerErr.addHandler(fh)
        loggerErr.debug("hello in Logger(Error)")
    
    loggerDemo()
    

    **References:** [Python之Logger](http://blog.csdn.net/kzjay/article/details/5655039) [Logging HOWTO](https://docs.python.org/2.7/howto/logging.html#logging-basic-tutorial) [Python中的logger和handler到底是个什么鬼](http://www.cnblogs.com/anpengapple/p/5048123.html)
  • 相关阅读:
    loadrunner11 您不具有该 Vuser 类型的许可证. 请与 HP Software 联系以更新许可证.
    LoadRunner安装+汉化+破解
    C# HttpWebRequest和WebClient的区别 通过WebClient/HttpWebRequest实现http的post/get方法
    C# -- 等待异步操作执行完成的方式
    UART中的硬件流控RTS与CTS
    ubuntu12.04 添加程序启动器
    10054错误
    float使用0xFF
    电路笔记
    linux ps 命令查看进程状态
  • 原文地址:https://www.cnblogs.com/lxw0109/p/logging_logger.html
Copyright © 2011-2022 走看看