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)
  • 相关阅读:
    poj 3261 Milk Patterns 后缀数组+二分
    poj 2774 Long Long Message(后缀数组入门题)
    hdu 5719 Arrange
    hdu 5720 Wool
    DROP TABLE 恢复
    MySQL数据库改名的三种方法
    MySQL 误操作后数据恢复(update,delete忘加where条件)
    MySQL常用SQL语句优化
    EXPLAIN 命令详解
    mysql sql语句大全
  • 原文地址:https://www.cnblogs.com/lxw0109/p/logging_logger.html
Copyright © 2011-2022 走看看