博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Python] logging.logger
阅读量:5165 次
发布时间:2019-06-13

本文共 2093 字,大约阅读时间需要 6 分钟。

<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:

转载于:https://www.cnblogs.com/lxw0109/p/logging_logger.html

你可能感兴趣的文章
2017.3.24上午
查看>>
Python-常用模块及简单的案列
查看>>
LeetCode 159. Longest Substring with At Most Two Distinct Characters
查看>>
基本算法概论
查看>>
jquery动态移除/增加onclick属性详解
查看>>
JavaScript---Promise
查看>>
暖暖的感动
查看>>
Java中的日期和时间
查看>>
Django基于admin的stark组件创建(一)
查看>>
抛弃IIS,利用FastCGI让Asp.net与Nginx在一起
查看>>
C. Tanya and Toys_模拟
查看>>
springboot jar包运行中获取资源文件
查看>>
基于FPGA实现的高速串行交换模块实现方法研究
查看>>
Java Scala获取所有注解的类信息
查看>>
delphi ,安装插件
查看>>
case when then的用法-leetcode交换工资
查看>>
11.28.cookie
查看>>
BeanShell简介
查看>>
python字符串操作
查看>>
不同程序语言的注释和变量要求
查看>>