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)
  • 相关阅读:
    ES6特性
    使用mybatis插件拦截SQL
    前端下载文件的几种方式
    Electron-vue项目使用 Inno Setup 创建安装包
    Windows powershell 常用代码段
    Java8之Predicate, Consumer,Function基础使用
    Java8之Predicate接口使用
    使用Replica Set副本集方式搭建mongodb副本集群
    Typora的一些偏好设置
    使用Typora编写md文档并优雅地上传到博客园
  • 原文地址:https://www.cnblogs.com/lxw0109/p/logging_logger.html
Copyright © 2011-2022 走看看