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. ASP.NET WEBAPI 使用Swagger生成API文档

    一.安装 新建一个没有身份验证的mvc项目 - SwaggerMvc5Demo,然后添加一个名为Remote(自定义)且包含基础读写(不想手写)的ApiController   开源地址:https: ...

  2. $Django cbv源码分析 djangorestframework框架之APIView源码分析

    1 CBV的源码分析 #视图 class login (View): pass #路由 url(r'^books/$', views.login.as_view()) #阅读源码: #左侧工程栏--- ...

  3. 查询每个分组中第N的一条记录

    查询每个分组中第N的一条记录 -- 天气表,每天每个地区采集了多条记录的天气信息,但是时间只记录到了天,导致同一个地区同一天出现了多条天气记录 -- 目的:获取所有地区在每天中第N的一条记录 sele ...

  4. centos如何安装Python3

    Linux下默认系统自带python2.6的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3那么我们知道编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装py ...

  5. centos6.5 有趣但是没有用的linux命令

    小火车 get http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -ivh epel-rele ...

  6. C#操作excel(多种方法比较)

    1.用查询表的方式查询并show在数据集控件上. public static string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; D ...

  7. JAVA 语言如何进行异常处理,关键字: throws,throw,try,catch,finally分别代表什么意义? 在try块中可以抛 出异常吗?

    Java通过面向对象的方法进行异常处理,把各种不同的异常进行分类, 并提供了良好的接口.        在 Java中,每个异常都是一个对象,它是 Throwable 类或其它子类的实例.当一个方法出 ...

  8. Confluence 6 配置 Windows 服务

    当你使用 Start Confluence Automatically on Windows as a Service 的方式启动的时候,你有下面 2 种方式来配置你的系统属性:通过 command ...

  9. D3.js+Es6+webpack构建人物关系图(力导向图),动态更新数据,点击增加节点,拖拽增加连线...

    觉得不错的麻烦加个Star:https://github.com/zhangzn3/D3-Es6 在线预览地址:https://zhangzn3.github.io/D3-Es6 功能列表:1. 增加 ...

  10. Java并发编程基础-ReentrantLock的机制

    同步锁: 我们知道,锁是用来控制多个线程访问共享资源的方式,一般来说,一个锁能够防止多个线程同时访问共享资源,在Lock接口出现之前,Java应用程序只能依靠synchronized关键字来实现同步锁 ...