# !/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. Bloom filter 2

    1 Bloom filter 计算方法 如需要判断一个元素是不是在一个集合中,我们通常做法是把所有元素保存下来,然后通过比较知道它是不是在集合内,链表.树都是基于这种思路,当集合内元素个数的变大,我们 ...

  2. php引用传值详解

    php的引用(就是在变量或者函数 .对象等前面加上&符号) 在PHP 中引用的意思是:不同的名字访问同一个变量内容. 与C语言中的指针是有差别的.C语言中的指针里面存储的是变量的内容在内存中存 ...

  3. JSP 分页显示数据 (Oracle)

    要实现分页,首先我们要做的就是如何来编写SQL语句,网上也有很多,大家可以搜一下.在这里,我们使用一种比较常用的方式来编写SQL语句.代码如下: ----分页显示 select * from (sel ...

  4. 排序算法入门之希尔排序(java实现)

    希尔排序是对插入排序的改进.插入排序是前面元素已经有序了,移动元素是一个一个一次往后移动,当插入的元素比前面排好序的所有元素都小时,则需要将前面所有元素都往后移动.希尔排序有了自己的增量,可以理解为插 ...

  5. Windows上模拟Linux环境的软件Cygwin

    Windows上模拟Linux环境的软件Cygwin 2010-10-11 15:19      我要评论(0) 字号:T|T Cygwin是一个用于在Windows上 模拟Linux环境的软件.它可 ...

  6. 关于Django Web应用架构设计开发的几个问题

    1.关于分层,做过传统JEE应用的同学肯定知道JEE应用会分很多个设计层.根据传统Web应用架构设计一般从上到下分这么几个层(太懒了,不画图了):Web前端层.Web后端交互层.业务层.基础数据设施层 ...

  7. Android反编译和再打包神器:Apktool

    首先推荐一下这东东,官网:https://ibotpeaches.github.io/Apktool/ 安装.使用之类的看官方文档吧,写这个博客主要是mark一下这东西. 这玩意只能供打包党来用,要想 ...

  8. c#与webapi交互

    public static string HttpConnectToServer(string ServerPage,string strData) { string postData =strDat ...

  9. Hbase出现ERROR: Can't get master address from ZooKeeper; znode data == null正确找到解决思路

    第一次配置时出现这样的错误,也很懵的,到处上网找博客看资料,都试了个遍,但是问题还是存在,以下这些博客写的或许是解决一类问题的方式. https://blog.csdn.net/whbo111/art ...

  10. AUTOSAR分层-MCAL辨析

    8. AUTOSAR中MCAL虽然包含各种drvier,但毕竟是AL即抽象层,不应包含architecture和device特定的信息.应该只包含模型定义,不包含实现细节.   AUTOSAR文档中的 ...