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)
  • 相关阅读:
    【SpringBoot1.x】SpringBoot1.x 安全
    WebStorm快捷键(Mac版)
    iOS 容联离线消息推送
    iOS使用TestFlight进行内部和外部人员测试
    iOS Runtime常用方法整理
    模仿斗鱼直播
    一个很好用的侧滑框架ICSDrawerController实现的 QQ 侧滑及换肤功能
    swift3.0 项目引导页
    使用 swift3.0高仿新浪微博
    swift 监听键盘弹出的高度
  • 原文地址:https://www.cnblogs.com/lxw0109/p/logging_logger.html
Copyright © 2011-2022 走看看