# !/user/bin/python
# -*- coding: utf-8 -*-
'''
subprocess : 需要在linux平台上测试 shell
logging
''' import logging
# 将日志输出在文件里
# logging.basicConfig(filename="app.log", level=logging.DEBUG)
logging.basicConfig(filename="app.log",
level=logging.WARNING,
format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p') # 在日志上加上时间. %p代表pm. TODO 为什么没打出行数?
logging.debug("test debug")
logging.info("test info")
logging.error("test error")
logging.warning("User [alex] attempted wrong password more than 3 times") # 同时将日志打印在屏幕上并输出在文件里
# step 1, create logger
logger = logging.getLogger("TEST-LOG")
logger.setLevel(logging.DEBUG) # step2, create console handler and set level to debug
ch=logging.StreamHandler()
ch.setLevel(logging.DEBUG) # step3, create file handler and set level to warning
fh = logging.FileHandler("process.log")
fh.setLevel(logging.ERROR) # step3, define format
fh_formatter = logging.Formatter('%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s')
ch_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(fh_formatter)
ch.setFormatter(ch_formatter) # step4, connect handlers to logger
logger.addHandler(fh)
logger.addHandler(ch) logger.warning("ddddd")
Level When it’s used
DEBUG Detailed information, typically of interest only when diagnosing problems.
INFO Confirmation that things are working as expected.
WARNING An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
ERROR Due to a more serious problem, the software has not been able to perform some function.
CRITICAL A serious error, indicating that the program itself may be unable to continue running.

Python logger /logging的更多相关文章

  1. [Python 模块] logging模块、Logger类

    logging模块: 标准库里面的logging模块,在前面学习线程安全时曾用来解决print被打断的问题,这里会介绍logging模块的功能. logging模块是线程安全的,不需要客户做任何特殊的 ...

  2. Python日志logging

    logging 用于便捷记录日志且线程安全的模块 1.单文件日志 import logging logging.basicConfig(filename='log.log', format='%(as ...

  3. Python之logging模块

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

  4. python 记录日志logging

    在项目开发中,往往要记录日志文件.用python记录日志有两种方式: 1.利用python 自带的logging库,例如: # -*- coding: utf-8 -*- import osimpor ...

  5. python模块 ---logging模块

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

  6. python的logging模块

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

  7. python 多进程 logging:ConcurrentLogHandler

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

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

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

  9. Python:logging 的巧妙设计

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

随机推荐

  1. Mac OS 的属性列表文件plist装换

    Mac OS系统自身包含有转换plist的工具:plutil.其中-p是以human可读方式显示plist文件,而convert就是转换参数,其中支持的格式有:xml,二进制和json.下面拿一个实际 ...

  2. pywinauto处理UI自动化

    之前一个项目的特殊性, 以及一些操作权限上的问题,不能使用现有工具进行UI自动化. 在一些资深tester建议下决定采用Python的pywinauto模块来处理Windows控件的UI操作. 1. ...

  3. html5中的全局属性

    在html5中,新增了一个"全局属性"的概念,所谓全局属性,是指可以对任何属性都使用的属性.下面列出常用的全局属性. 1.contentEditable属性,是微软开发的,该属性主 ...

  4. path sum II(深度优先的递归实现掌握)

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  5. JSON 的含义?

    JSON 的全称是 JavaScript Object Notation,是一种轻量级的数据交换格式.JS ON 与 XML 具有相同的特性,例如易于人编写和阅读,易于机器生成和解析.但是 JSON ...

  6. 人手一份核武器 - Hacking Team 泄露(开源)资料导览手册

    https://zhuanlan.zhihu.com/p/20102713 author:蒸米 0x00 序 事先声明本人并不是全栈安全工程师,仅仅是移动安全小菜一枚,所以对泄漏资料的分析难免会有疏忽 ...

  7. DELETE_FAILED_INTERNAL_ERROR Error while Installing APK

    真是Android2.3的特殊版本问题,问题原因是android2.3的instant run的测试版安装方式有所特别,解决办法有2: 1.手动adb install 安装包 2.把Instant r ...

  8. Django Push 的一些资料

    先来中文的: 主要讲Orbited: http://sunsetsunrising.com/2009/django_comet.html#gsc.tab=0 再来英文的: http://www.rkb ...

  9. Go语言开发区块链只需180行代码

    区块链开发用什么语言?通过本文你将使用Go语言开发自己的区块链(或者说用go语言搭建区块链).理解哈希函数是如何保持区块链的完整性.掌握如何用Go语言编程创造并添加新的块.实现多个节点通过竞争生成块. ...

  10. flex与js通信、在浏览器中打开新窗口

    一.flex与js通信(通过flex调用js方法) var urlR:URLRequest = new URLRequest("javascript:test('from flex')&qu ...