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 2312 Battle City(优先队列+bfs)
    hdu 2112 HDU Today (最短路)
    hdu 1874 畅通工程续
  • 原文地址:https://www.cnblogs.com/lxw0109/p/logging_logger.html
Copyright © 2011-2022 走看看