python log封装
# _*_ coding:utf-8 _*_ import logging
import os
import sys
import time log_path = os.path.dirname(sys.path[0]) + '/log_path' class Log():
def __init__(self):
filename = 'test_' + time.strftime('%Y_%m_%d_%H%M%S') + '.log' # 设置log名
self.logname = os.path.join(log_path, filename)
self.logger = logging.getLogger()
self.logger.setLevel(logging.DEBUG)
#设置日志输出格式
self.formatter = logging.Formatter('%(asctime)s [%(levelname)s] [%(name)s:%(lineno)d] - %(message)s') def output(self, level, message):
"""
:param level: 日志等级
:param message: 日志需要打印的信息
:return:
"""
# send logging output to a disk file
fh = logging.FileHandler(self.logname, 'a', encoding='utf-8')
fh.setLevel(logging.DEBUG)
fh.setFormatter(self.formatter)
self.logger.addHandler(fh) # send logging output to streams
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(self.formatter)
self.logger.addHandler(ch) if level == 'info':
self.logger.info(message)
elif level == 'debug':
self.logger.debug(message)
elif level == 'warn':
self.logger.warn(message)
elif level == 'error':
self.logger.error(message) #防止重复打印
self.logger.removeHandler(fh)
self.logger.removeHandler(ch) fh.close() def info(self, message: object) -> object:
self.output('info', message) def debug(self, message):
self.output('debug', message) def warn(self, message):
self.output('warn', message) def error(self, message):
self.output('error', message)
python log封装的更多相关文章
- python log
python的日志模块为logging,它可以将我们想要的信息输出保存到一个日志文件中. # cat log import logging logging.debug('This is debug m ...
- 将Python脚本封装成exe可执行文件 转
将Python脚本封装成exe可执行文件 http://www.cnblogs.com/renzo/archive/2012/01/01/2309260.html cx_freeze是用来将 Pyt ...
- python继承——封装
python继承--封装 1 为什么要封装 封装数据的主要原因是:保护隐私 封装方法的主要原因是:隔离复杂度 2 封装分为两个层面 第一个层面的封装(什么都不用做):创建类和对象会分别创建二者的名称空 ...
- 【转】Python基础-封装与扩展、静态方法和类方法
[转]Python基础-封装与扩展.静态方法和类方法 一.封装与扩展 封装在于明确区分内外,使得类实现者可以修改封装内的东西而不影响外部调用者的代码:而外部使用者只知道一个接口(函数),只要接口(函数 ...
- python log的处理方式
python log的处理方式 配置文件 #! /usr/bin/env python # -*- coding: utf-8 -*- # __author__ = "Q1mi" ...
- Python log 模块介绍
刚用Python log模块写了一个例子,记录一下. import logging import logging.handlers import os from datetime import dat ...
- python 3 封装
python 3 封装 从封装本身的意思去理解,封装就好像是拿来一个麻袋,把小鱼,小虾,小王八,一起装进麻袋,然后把麻袋封上口子.照这种逻辑看,封装=‘隐藏’,这种理解是相当片面的. 先看如何隐藏 在 ...
- python面向对象-封装-property-接口-抽象-鸭子类型-03
封装 什么是封装: # 将复杂的丑陋的隐私的细节隐藏到内部,对外提供简单的使用接口 或 # 对外隐藏内部实现细节,并提供访问的接口 为什么需要封装 1.为了保证关键数据的安全性 2.对外部隐藏内部的实 ...
- python学习笔记:安装boost python库以及使用boost.python库封装
学习是一个累积的过程.在这个过程中,我们不仅要学习新的知识,还需要将以前学到的知识进行回顾总结. 前面讲述了Python使用ctypes直接调用动态库和使用Python的C语言API封装C函数, C+ ...
随机推荐
- ZT 二叉树的非递归遍历
ZT 二叉树的非递归遍历 二叉树的非递归遍历 二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的.对于二叉树,有前序.中序以及后序三种遍历方法.因为树的定义本身就 是递归定 ...
- Ubuntu 12.04常用快捷键
===== 桌面 ===== ALT + F1: 聚焦到桌面左侧任务导航栏,可按上下键导航. ALT + F2: 运行命令 ALT + F4: 关闭窗口 ALT + TAB: 切换程序窗口 ALT + ...
- 用php代码统计数据库中符合条件的行数
$sql1 = "select count(*) from t_user where age<17"; $data1 = mysql_query($sql1); $rows1 ...
- C语言利用 void 类型指针实现面向对象类概念与抽象。
不使用C++时,很多C语言新手可能认为C语言缺乏了面向对象和抽象性,事实上,C语言通过某种组合方式,可以间接性的实现面对对象和抽象. 不过多态和继承这种实现,就有点小麻烦,但是依然可以实现. 核心: ...
- k8s存储 pv pvc ,storageclass
1. pv pvc 现在测试 glusterfs nfs 可读可写, 多个pod绑定到同一个pvc上,可读可写. 2. storageclass 分成两种 (1) 建立pvc, 相当于多个 ...
- mac下用xattr命令来删除文件的扩展属性
mac下发现不能用记事本打开文本文件,ls -la 发现格式后面有个@ wenke-mini:changeServer wenke$ ls -la total 144 drwxr-xr-x 20 w ...
- SRcnn:神经网络重建图片的开山之作
% ========================================================================= % Test code for Super-Re ...
- maven +IEDA+log4j
一.pom.xml加入log4j的依赖包 <!-- 日志文件管理包 --><dependency> <groupId>log4j</groupId> ...
- ZooKeeper(一)基本介绍
本文转载自LDB's Blog,原文链接 ZooKeeper原理及其在Hadoop和HBase中的应用 目录 一.简介 二.基本概念 1. 集群角色 2. 会话(Session) 3. 数据节点(ZN ...
- HDU 1025 LIS二分优化
题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...