[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)
[Python] logging.logger的更多相关文章
- python logging
参考: https://docs.python.org/2/howto/logging.html#logging-basic-tutorial https://docs.python.org/2/li ...
- python logging模块详解[转]
一.简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.war ...
- python logging模块
1.logging模块提供了四个组件logger:日志类,有两个功能1)配置日志的等级,处理器handler,过滤器filterlogger.setLevel(logging.INFO)logger. ...
- python logging模块可能会令人困惑的地方
python logging模块主要是python提供的通用日志系统,使用的方法其实挺简单的,这块就不多介绍.下面主要会讲到在使用python logging模块的时候,涉及到多个python文件的调 ...
- python logging 配置
python logging 配置 在python中,logging由logger,handler,filter,formater四个部分组成,logger是提供我们记录日志的方法:handler是让 ...
- Python LOGGING使用方法
Python LOGGING使用方法 1. 简介 使用场景 场景 适合使用的方法 在终端输出程序或脚本的使用方法 print 报告一个事件的发生(例如状态的修改) logging.info()或log ...
- python logging 日志轮转文件不删除问题
前言 最近在维护项目的python项目代码,项目使用了 python 的日志模块 logging, 设定了保存的日志数目, 不过没有生效,还要通过contab定时清理数据. 分析 项目使用了 logg ...
- python logging模块使用
近来再弄一个小项目,已经到收尾阶段了.希望加入写log机制来增加程序出错后的判断分析.尝试使用了python logging模块. #-*- coding:utf-8 -*- import loggi ...
- python Logging的使用
日志是用来记录程序在运行过程中发生的状况,在程序开发过程中添加日志模块能够帮助我们了解程序运行过程中发生了哪些事件,这些事件也有轻重之分. 根据事件的轻重可分为以下几个级别: DEBUG: 详细信息, ...
随机推荐
- Java8:使用 Optional 处理 null
写过 Java 程序的同学,一般都遇到过 NullPointerException :) —— 为了不抛出这个异常,我们便会写如下的代码: User user = getUserById(id); i ...
- 我在Facebook学到的10个经验
1.坚持你的远景,但要对细节灵活. 作为一个领导者,你需要依赖你自己的远景(至少在你负责的业务领域内)而那些和你一起或为你工作的人将依赖你的远见.什么是远景?就是对最终状态的一种描述.是你需要你的团队 ...
- Designated Initializer
一个类,可能有很多初始化函数,但是有主次之分,最主要的初始函数应该对类内应当需要初始化的变量进行初始化.这个最主要的初始函数即Designated Initializer(指定初始化器),可以理解为是 ...
- sgu 326(经典网络流构图)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13349 题目大意:有N个球队在同一个赛区,已知他们胜利的场数,还剩 ...
- virtualbox虚拟机Linux系统与本地windows系统共享文件方法
转自:http://jingyan.baidu.com/article/2fb0ba40541a5900f2ec5f07.html
- UE4 Multiplayer多人局域网LAN联机打包参数设置
需要注意几点: A. 建好后我们先到项目根目录,在Config文件夹中的DefaultEngine.ini文件加上两行: [OnlineSubsystem] DefaultPlatformServi ...
- 网络代理-Firefox在shadow socks下面的使用
好久不写了,嘿嘿,中午好哈大家,给大家介绍下firefox下配置shadowsocks使用代理. 第一步:先下载一个firefox. 第二步: 打开设置 找到组件选项. 3.第三步: 4.第四步: 5 ...
- .NET 4.0 中的契约式编程
契约式编程不是一门崭新的编程方法论.C/C++ 时代早已有之.Microsoft 在 .NET 4.0 中正式引入契约式编程库.博主以为契约式编程是一种相当不错的编程思想,每一个开发人员都应该掌握.它 ...
- jquery remove() detach() empty()三种方法的区别
remove方法把事件删除掉了,数据并没有删除 detach方法保存了事件和数据 empty方法保留了元素本身,移除子节点,删除内容 举例: <!DOCTYPE html><html ...
- 01.ActiveMQ安装部署
1.下载安装ActiveMQ 下载地址:http://activemq.apache.org/download-archives.html选择相应的版本,笔者选择的是:apache-activem ...