python之logging
1.简单使用
# CRITICAL, ERROR, WARNING, INFO, DEBUG) cewid
import logging
logging.basicConfig(level=logging.DEBUG) #filename='log_debug.txt', console和文件输出二选一。。。
logger = logging.getLogger('%s %s'%(os.path.split(__file__)[1], __name__)) #脚本文件名字
# 放在类里面
# logger = logging.getLogger(__name__)
# 放在类的函数里面
# ##self.logger = logging.getLogger(self.__class__.__name__)
# self.logger.debug('xxx')
# 放在函数内部
logger.debug('%s: %s'%(sys._getframe().f_code.co_name, 'abcde')) #函数名字
2.console输出并保存到文件
(1)参考
python logging 日志输出 学习笔记 时间格式化
第二个链接还没好好利用!
(2)更新代码
import logging
def print_save_log(logFilename):
''''' Output log to file and console '''
# Define a Handler and set a format which output to file
logging.basicConfig(
level = logging.DEBUG,
format = '%(asctime)s %(filename)s : %(levelname)s %(message)s',
# 默认 2017-08-02 12:35:38,956
# 设置 2017-08-02 Wednesday 12:36:26
datefmt = '%Y-%m-%d %A %H:%M:%S',
filename = logFilename,
filemode = 'a') # Define a Handler and set a format which output to console
console = logging.StreamHandler()
console.setLevel(logging.INFO) #可以与记录文件不同级别
formatter = logging.Formatter('%(asctime)s %(filename)s : %(levelname)s %(message)s')
console.setFormatter(formatter) # Create an instance
logger = logging.getLogger()
logger.addHandler(console) # 实例化添加handler
return logger logger = print_save_log('logging.log')
logger.debug('logger debug message')
logger.info('logger info message')
logger.warning('logger warning message')
logger.error('logger error message')
logger.critical('logger critical message')
3. cookielib.py
debug = True # set to True to enable debugging via the logging module
logger = None def _debug(*args):
if not debug:
return
global logger
if not logger:
import logging
logger = logging.getLogger("cookielib")
return logger.debug(*args) #实际只支持单个参数?!
python之logging的更多相关文章
- Python之logging模块
一.引言 之前在写一些小程序的时候想把日志内容打到文件中,所以就自己写了一个logger.py的程序,如下: #!/usr/bin/python # -*- coding=utf-8 -*- impo ...
- python模块 ---logging模块
摘要by crazyhacking: 与log4cxx一样,分为三个部分,logger, handler,formatter. 详细内容参考:1官网http://docs.python.org/2/h ...
- python的logging模块
python提供了一个日志处理的模块,那就是logging 导入logging模块使用以下命令: import logging logging模块的用法: 1.简单的将日志打印到屏幕上 import ...
- python 多进程 logging:ConcurrentLogHandler
python 多进程 logging:ConcurrentLogHandler python的logging模块RotatingFileHandler仅仅是线程安全的,如果多进程多线程使用,推荐 Co ...
- python的logging模块之读取yaml配置文件。
python的logging模块是用来记录应用程序的日志的.关于logging模块的介绍,我这里不赘述,请参见其他资料.这里主要讲讲如何来读取yaml配置文件进行定制化的日志输出. python要读取 ...
- Python:logging 的巧妙设计
引言 logging 的基本用法网上很多,这里就不介绍了.在引入正文之前,先来看一个需求: 假设需要将某功能封装成类库供他人使用,如何处理类库中的日志? 数年前在一个 C# 开发的项目中,我用了这样的 ...
- django/python日志logging 的配置以及处理
日志在程序开发中是少不了的,通过日志我们可以分析到错误在什么地方,有什么异常.在生产环境下有很大的用处.在java 开发中通常用 log4j,logback 等三方组件.那么在 django中是怎么处 ...
- python中logging模块的用法
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...
- Python模块——logging模块
logging模块简介 logging模块定义的函数和类为应用程序和库的开发实现了一个灵活的事件日志系统.logging模块是Python的一个标准库模块, 由标准库模块提供日志记录API的关键好处是 ...
- python 运行日志logging代替方案
以下是自己写的 记录日志的代码.(和logging不搭嘎,如果如要学loggging模块,本文末尾有他人的链接.) # prtlog.py ############################## ...
随机推荐
- VS2015 代码片段整理
1.什么是代码段? 将一段代码行提取出来,可以多次重复的使用.VS IDE提供对代码段的完整支持.使代码编写更快.更容易.更可靠. 2.系统默认代码段 对于开发人员的例行任务,Visual Studi ...
- noi.ac 集合
A.集合 --- 题面 不知道有没有用的传送门[滑稽 就是给你一个 包含 1~n 的集合,让你求它的大小为 k 的子集 s 的 \(T^{min(s)}\) 的期望值, T 为给出值, min(s) ...
- Linux内核之进程地址空间
Linux内核之进程地址空间 内核中的函数以相当直接了当的方式获得动态内存: __get_free_pages 或 alloc_pages从分区页框分配器中获得页框; kmem_cache_alloc ...
- ansible笔记(12):handlers的用法
ansible笔记():handlers的用法 这篇文章会介绍playbook中handlers的用法. 在开始介绍之前,我们先来描述一个工作场景: 当我们修改了某些程序的配置文件以后,有可能需要重启 ...
- zabbix添加对centos系统cpu使用率百分比的监控
cpu使用率key: system.cpu.util[] 在grafana现实的时候配置,单位选择percent(0-100),范围0-100
- 通过python操作smtplib模块发送邮件
# gconf.py SMTP_SERVER_HOST='smtp.exmail.qq.com' SMTP_SERVER_PORT=25 SMTP_USER='jack@qq.com' # 邮箱客户端 ...
- Record && Limit
案例一:Record 预期效果:在 IVR 与用户交互的时候,比如让用户读一段语音,当用户读完之后,按键结束录音. <action application="set" dat ...
- springcloud-3:required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found.
在写客户端程序的时候,总是报'com.netflix.discovery.DiscoveryClient' that could not be found. 原因在于导入了错误的类:com.netfl ...
- 深入Golang调度器之GMP模型
前言 随着服务器硬件迭代升级,配置也越来越高.为充分利用服务器资源,并发编程也变的越来越重要.在开始之前,需要了解一下并发(concurrency)和并行(parallesim)的区别. 并发: 逻 ...
- JVM·垃圾收集器与内存分配策略之垃圾收集器!
1.Serial(串行)收集器(新生代都采用复制算法) 这是个单线程的收集器:即 当他工作的时候,会停掉虚拟机所有的线程!(Stop The World)