日志打印longging模块(控制台和文件同时输出)
在把日志写入文件的同时在控制台输出
示例代码如下:
#coding=utf-8 import logging
import time
import os
dir = os.path.dirname(os.path.abspath('.'))
log_path = dir + '\\log' class Log:
def __init__(self):
self.logname = os.path.join(log_path, '{0}.log'.format(time.strftime('%Y-%m-%d'))) def __printconsole(self, level, message):
# 创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler(self.logname,'a',encoding='utf-8')
fh.setLevel(logging.DEBUG)
# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# 定义handler的输出格式
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# 给logger添加handler
logger.addHandler(fh)
logger.addHandler(ch)
# 记录一条日志
if level == 'info':
logger.info(message)
elif level == 'debug':
logger.debug(message)
elif level == 'warning':
logger.warning(message)
elif level == 'error':
logger.error(message)
logger.removeHandler(ch)
logger.removeHandler(fh)
# 关闭打开的文件
fh.close() def debug(self,message):
self.__printconsole('debug', message) def info(self,message):
self.__printconsole('info', message) def warning(self,message):
self.__printconsole('warning', message) def error(self,message):
self.__printconsole('error', message) if __name__ == '__main__':
log = Log()
log.info('info msg1000013333')
log.debug('debug msg')
log.warning('warning msg')
log.error('error msg')
输出结果如下:
> "D:\Python27\python.exe" "D:\work\code\test\test\wirteLog.py"
2017-05-09 15:09:32,763 - root - INFO - info msg1000013333
2017-05-09 15:09:32,769 - root - DEBUG - debug msg
2017-05-09 15:09:32,776 - root - WARNING - warning msg
2017-05-09 15:09:32,782 - root - ERROR - error msg
日志打印longging模块(控制台和文件同时输出)的更多相关文章
- Python+Selenium进行UI自动化测试项目中,常用的小技巧4:日志打印,longging模块(控制台和文件同时输出)
在前段时间,为了给项目中加入日志功能,就想到了 logging 模块,百度logging一大推,都是各种复制的,并没有找到自己想要的结果:我的目的很简单,就是:在把日志写入文件的同时在控制台输出,更加 ...
- Python logging模块 控制台、文件输出
步骤 导入logging模块 设置level(此处是DEBUG) 添加文件handler和流handler import logging logger=logging.getLogger(__name ...
- Log4j 把不同包的日志打印到不同位置
如果需要将不同的日志打印到不同的地方,则需要定义不同的Appender,然后定义每一个 Appender的日志级别.打印形式.输出位置! 配置log4j.properties文件如下: ####### ...
- Python同时向控制台和文件输出日志logging的方法 Python logging模块详解
Python同时向控制台和文件输出日志logging的方法http://www.jb51.net/article/66756.htm 1 #-*- coding:utf-8 -*- 2 import ...
- python中使用logging将日志写入文件或输出到控制台
import logging import os class Logger: def __init__(self, name=__name__): # 创建一个loggger self.__name ...
- Python+request 分模块存放接口,多接口共用参数URL、headers的抽离,添加日志打印等《三》
主要介绍内容如下: 1.分模块存放接口 2.多接口共用参数URL.headers的抽离为配置文件 3.添加日志打印 4.一个py文件运行所有所测的接口 如上介绍内容的作用: 1.分模块存放接口:方便多 ...
- 在使用hibernate的getHibernateTemplate()时怎么让控制台输出封装好的SQL? 怎么用日志打印出来?
我们在使用 Hibernate 时一般只会关注是否显示生成的 SQL 语句,不过有些时候还不够.默认时 Hibernate 执行的 SQL 语句是打印在控制台上的,它也可以配置为输出给 Log4J 或 ...
- 【docker】docker部署spring boot服务,但是docker logs查看容器输出控制台日志,没有日志打印,日志未打印,docker logs不打印容器日志
如题: docker部署spring boot服务,但是docker logs查看容器输出控制台日志,没有日志打印,日志未打印,docker logs不打印容器日志 场景再现: docker部署并启动 ...
- 转【Python】同时向控制台和文件输出日志logging
#-*- coding:utf-8 -*-import logging # 配置日志信息logging.basicConfig(level=logging.DEBUG, format='%(ascti ...
随机推荐
- 【bzoj4269】再见Xor 高斯消元求线性基
题目描述 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. 输入 第一行一个正整数N. 接下来一行N个非负整数. 输出 一行,包含两 ...
- Angular JS知识小总结
1.什么是Angular JS? AngularJS 是一个为动态WEB应用设计的 JavaScript结构框架. 2.Angular JS的用处? --它是为了克服HTML在构建应用上的不足而设计 ...
- BZOJ2302 [HAOI2011]Problem c 【dp】
题目 给n个人安排座位,先给每个人一个1~n的编号,设第i个人的编号为ai(不同人的编号可以相同),接着从第一个人开始,大家依次入座,第i个人来了以后尝试坐到ai,如果ai被占据了,就尝试ai+1,a ...
- QBXT 二月五号整理
给你一列数, 询问和最大的子串. N<=10^6 // N <=10^6 #include<cstdio> #include<iostream> using nam ...
- ajax cache enable and ajax concurrency!
Today, forget to close ajax cache which leads to duplicate result from cache as to Jquery, this way, ...
- angular中关于文件引入
var angular = require('angular'); module.exports = angular.module('app.mymodule2', []).controller('H ...
- Cython 一篇通
Cython的类型 1 类型定义 1.1 定义一个C变量: 1.1.1 在Cython里定义一个C变量和C语言类似,不同的地方就是在声明的最前面要加上cdef,另外,末尾不用加分号";“如: ...
- declaration specifier, declarator, type specifier
static struct abc * b; static struct abc : declaration specifier * b : declarator struct abc : type ...
- poj 1274(网络流解二分图的最大匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22539 Accepted: 100 ...
- LeetCode OJ--Merge Two Sorted Lists
http://oj.leetcode.com/problems/merge-two-sorted-lists/ 有序链表的归并排序 #include <iostream> using na ...