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 日志输出 学习笔记 时间格式化

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的更多相关文章

  1. Python之logging模块

    一.引言 之前在写一些小程序的时候想把日志内容打到文件中,所以就自己写了一个logger.py的程序,如下: #!/usr/bin/python # -*- coding=utf-8 -*- impo ...

  2. python模块 ---logging模块

    摘要by crazyhacking: 与log4cxx一样,分为三个部分,logger, handler,formatter. 详细内容参考:1官网http://docs.python.org/2/h ...

  3. python的logging模块

    python提供了一个日志处理的模块,那就是logging 导入logging模块使用以下命令: import logging logging模块的用法: 1.简单的将日志打印到屏幕上 import ...

  4. python 多进程 logging:ConcurrentLogHandler

    python 多进程 logging:ConcurrentLogHandler python的logging模块RotatingFileHandler仅仅是线程安全的,如果多进程多线程使用,推荐 Co ...

  5. python的logging模块之读取yaml配置文件。

    python的logging模块是用来记录应用程序的日志的.关于logging模块的介绍,我这里不赘述,请参见其他资料.这里主要讲讲如何来读取yaml配置文件进行定制化的日志输出. python要读取 ...

  6. Python:logging 的巧妙设计

    引言 logging 的基本用法网上很多,这里就不介绍了.在引入正文之前,先来看一个需求: 假设需要将某功能封装成类库供他人使用,如何处理类库中的日志? 数年前在一个 C# 开发的项目中,我用了这样的 ...

  7. django/python日志logging 的配置以及处理

    日志在程序开发中是少不了的,通过日志我们可以分析到错误在什么地方,有什么异常.在生产环境下有很大的用处.在java 开发中通常用 log4j,logback 等三方组件.那么在 django中是怎么处 ...

  8. python中logging模块的用法

    很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...

  9. Python模块——logging模块

    logging模块简介 logging模块定义的函数和类为应用程序和库的开发实现了一个灵活的事件日志系统.logging模块是Python的一个标准库模块, 由标准库模块提供日志记录API的关键好处是 ...

  10. python 运行日志logging代替方案

    以下是自己写的 记录日志的代码.(和logging不搭嘎,如果如要学loggging模块,本文末尾有他人的链接.) # prtlog.py ############################## ...

随机推荐

  1. Linux 网络侦错:无法联机原因分析

    所谓的软件问题,绝大部分就是 IP 参数设定错误啊,路由不对啊,还有 DNS 的 IP 设定错误等等的, 这些问题都是属于软件设定啦!只要将设定改一改,利用一些侦测软件查一查,就知道问题出在哪里了!基 ...

  2. 004_LVS及haproxy

    一.lvs工作在哪层(新浪面试题) http://blog.chinaunix.net/uid-346158-id-2131163.html 二.四层.七层负载均衡的区别 所谓四层就是基于IP+端口的 ...

  3. web@css盒模型详解

    Margin(外边距) - 清除边框外的区域,外边距是透明的./*上 右 下 左*/ 上 左右 下 /*上下 左右*/ 四方 /Border(边框) - 围绕在内边距和内容外的边框.      可以用 ...

  4. Go断言

    用Go语言提供的类型检测方法,同时也可作为断言的解决方案: package main import ( "fmt" "reflect" ) type MyStr ...

  5. ebs 12.1.1 单节点多用户安装

    本次测试环境:操作系统 oracle linux 6.9   oracle ebs 12.1.1   192.168.20.210  erpapp1.hthorizon.com erpapp1 yum ...

  6. mysql生成数据字典

    git clone https://github.com/twindb/undrop-for-innodb.git make [root@redis01 undrop-for-innodb]# mak ...

  7. HDU 5297

    用x ^ (1 / n) 来求个数,容斥原理 , Num会向后移动, 迭代到不再变化,退出循环 #include<iostream> #include<cstdio> #inc ...

  8. JsonResponse

    1.JsonResponse class JsonResponse(data, encoder=DjangoJSONEncoder, safe=True, json_dumps_params=None ...

  9. 关于《common-net》的ftp上传

    1:jar的maven的引用: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...

  10. 【Java】SpringBoot配置文件读取中文乱码

    [问题]在配置文件application.properties中配置一个值含有中文的变量.spring加载配置之后,读取的变量中文部分出现乱码.根据CSDN说的一堆办法,改encoding为UTF-8 ...