1、日志的使用

import logging

class CommonLog(object):

    def _common_log(self,level,message):
# 设定收集器,再设定收集的级别
my_logger = logging.getLogger('lisa')
my_logger.setLevel(level) # 设定转换器
formatter = logging.Formatter('%(asctime)s-%(levelname)s-%(filename)s-%(name)s-日志信息:%(message)s') # 设定控制台输出渠道,并规定输出控制台的级别,并规定输出格式
c_handler = logging.StreamHandler()
c_handler.setLevel(level)
c_handler.setFormatter(formatter) # 设定文件输出渠道,并规定输出文件的级别,并规定输出格式
f_handler = logging.FileHandler('log.txt', encoding='utf-8')
f_handler.setLevel(level)
f_handler.setFormatter(formatter) # 把收集器和输出渠道关联起来
my_logger.addHandler(c_handler)
my_logger.addHandler(f_handler) # 收集日志
if level == 'DEBUG':
my_logger.debug(message)
elif level == 'INFO':
my_logger.info(message)
elif level == 'WARNING':
my_logger.warning(message)
elif level == 'ERROR':
my_logger.error(message)
elif level == 'CRITIAL':
my_logger.critical(message) # 关闭渠道
my_logger.removeHandler(c_handler)
my_logger.removeHandler(f_handler) def debug_method(self,message):
self._common_log('DEBUG',message) def info_method(self,message):
self._common_log('INFO',message) def warning_method(self, message):
self._common_log('WARNING', message) def error_method(self, message):
self._common_log('ERROR', message) def critical_method(self, message):
self._common_log('CRITIAL', message) if __name__ == '__main__':
message = '你从晨雾中走来,放下了所有的傲慢与偏见'
CommonLog().debug_method(message) 控制台输出结果:
2019-12-23 13:33:37,631-DEBUG-log_file.py-lisa-日志信息:你从晨雾中走来,放下了所有的傲慢与偏见

python --- 日志模块 logging的更多相关文章

  1. python日志模块logging

    python日志模块logging   1. 基础用法 python提供了一个标准的日志接口,就是logging模块.日志级别有DEBUG.INFO.WARNING.ERROR.CRITICAL五种( ...

  2. 【python】【logging】python日志模块logging常用功能

    logging模块:应用程序的灵活事件日志系统,可以打印并自定义日志内容 logging.getLogger 创建一个log对象 >>> log1=logging.getLogger ...

  3. Python 日志模块logging

    logging模块: logging是一个日志记录模块,可以记录我们日常的操作. logging日志文件写入默认是gbk编码格式的,所以在查看时需要使用gbk的解码方式打开. logging日志等级: ...

  4. Python日志模块logging用法

    1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICAL. DEBUG:详细的信息,通常只出现在诊断问题上 INFO:确认一切按预期运行 ...

  5. python日志模块logging学习

    介绍 Python本身带有logging模块,其默认支持直接输出到控制台(屏幕),或者通过配置输出到文件中.同时支持TCP.HTTP.GET/POST.SMTP.Socket等协议,将日志信息发送到网 ...

  6. Python日志模块logging简介

    日志处理是项目的必备功能,配置合理的日志,可以帮助我们了解系统的运行状况.定位位置,辅助数据分析技术,还可以挖掘出一些额外的系统信息. 本文介绍Python内置的日志处理模块logging的常见用法. ...

  7. Python 日志模块 logging通过配置文件方式使用

    vim logger_config.ini[loggers]keys=root,infoLogger,errorlogger [logger_root]level=DEBUGhandlers=info ...

  8. Python日志模块logging&JSON

    日志模块的用法 json部分 先开一段测试代码:注意  str可以直接处理字典   eval可以直接将字符串转成字典的形式 dic={'key1':'value1','key2':'value2'} ...

  9. 『无为则无心』Python日志 — 64、Python日志模块logging介绍

    目录 1.日志的作用 2.为什么需要写日志 3.Python中的日志处理 (1)logging模块介绍 (2)logging模块的四大组件 (3)logging日志级别 1.日志的作用 从事与软件相关 ...

  10. python日志模块---logging

    1.将日志打印到屏幕 import logging logging.debug('This is debug message---by liu-ke') logging.info('This is i ...

随机推荐

  1. web-pc项目中index页面分析

    先上HTML代码: <%@ page language="java" contentType="text/html; charset=UTF-8" pag ...

  2. Ubuntu 编译 LAMP

    下载apache源码 http://httpd.apache.org/ 解压缩apache安装包,进入apache文件夹. 安装: apache2.2.9./configure --prefix=/u ...

  3. [Linux] day03——REHL部署

    REHL 部署 Linux 树形目录结构  最顶层 根 / 在Linux中 一切皆文件 文件系统 swap / 安装方式 PXE U盘 光盘 配置安装程序 选择包   5.9    6.4

  4. list.Except()

    差集 List<string> list = new List<string>() { ", ", ", }; List<string> ...

  5. NSIndexPath等结构体的比较

    1.NSIndexPath的比较方式,需要将结构体内部的属性一一对比.比如, if ((indexPath.section == self.selectIndexPath.section) & ...

  6. 普通用户切换不到root用户--权限更改

    https://blog.csdn.net/lianjoke0/article/details/82598149 [root@java133 ~]# ll /etc/passwd -rw-r--r-- ...

  7. freeswitch install

    https://freeswitch.org/confluence/display/FREESWITCH/CentOS+7+and+RHEL+7

  8. swift正点

    Openstack Swift 原理.架构与 API 介绍 http://www.openstack.cn/?p=776 ——Openstack Swift 开源云存储技术解析 OpenStack S ...

  9. PaperReading20200221

    CanChen ggchen@mail.ustc.edu.cn Busy... Human-level concept learning through probabilistic program i ...

  10. day16-Python运维开发基础(os / os.path / shutil模块)

    1. OS模块与shutil模块 os :新建/删除shutil: 复制/移动 # ### os模块 与 shutil模块 """ os 新建/删除 shutil 复制/ ...