#coding: utf-8
import logging
import logging.config
 
class SingleLevelFilter(object):
    def __init__(self, pass_level):
        self.pass_level = pass_level
 
    def filter(self, record):
        if self.pass_level == record.levelno:
            return True
        return False
 
LEVEL_COLOR = {
    logging.DEBUG: '\33[2;39m',
    logging.INFO: '\33[0;37m',
    logging.WARN: '\33[4;35m',
    logging.ERROR: '\33[5;31m',
    logging.FATAL: '\33[7;31m'
}
 
 
class ScreenHandler(logging.StreamHandler):
    def emit(self, record):
        try:
            msg = self.format(record)
            stream = self.stream
            fs = LEVEL_COLOR[record.levelno] + "%s\n" + '\33[0m'
            try:
                if isinstance(msg, unicode) and getattr(stream, 'encoding', None):
                    ufs = fs.decode(stream.encoding)
                    try:
                        stream.write(ufs % msg)
                    except UnicodeEncodeError:
                        stream.write((ufs % msg).encode(stream.encoding))
                else:
                    stream.write(fs % msg)
            except UnicodeError:
                stream.write(fs % msg.encode("UTF-8"))
 
            self.flush()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)
 
def init_logger():
    conf = {'version': 1,
            'disable_existing_loggers': True,
            'incremental': False,
            'formatters': {'myformat1': {'class': 'logging.Formatter',
                                         'format': '|%(asctime)s|%(name)s|%(filename)s|%(lineno)d|%(levelname)s|%(message)s',
                                         'datefmt': '%Y-%m-%d %H:%M:%S'}
                          },
            'filters': {'filter_by_name': {'class': 'logging.Filter',
                                           'name': 'logger_for_filter_name'},
 
                        'filter_single_level_pass':{'()': 'mylogger.SingleLevelFilter',
                                                    'pass_level': logging.WARN}
                        },
            'handlers': {'console': {'class': 'logging.StreamHandler',
                                      'level': 'INFO',
                                      'formatter': 'myformat1',
                                      'filters': ['filter_single_level_pass', ]},
 
                         'screen': {'()': 'mylogger.ScreenHandler',
                                    'level': logging.INFO,
                                    'formatter': 'myformat1',
                                    'filters': ['filter_by_name', ]}
                        },
            'loggers': {'logger_for_filter_name': {'handlers': ['console', 'screen'],
                                                   'filters': ['filter_by_name', ],
                                                   'level': 'INFO'},
                        'logger_for_all': {'handlers': ['console', ],
                                           'filters': ['filter_single_level_pass',],
                                           'level': 'INFO',
                                           'propagate': False}
                       }
            }
    logging.config.dictConfig(conf)
 
if __name__ == '__main__':
    init_logger()
    logger_for_filter_name = logging.getLogger('logger_for_filter_name')
    logger_for_filter_name.debug('logger_for_filter_name')
    logger_for_filter_name.info('logger_for_filter_name')
    logger_for_filter_name.warn('logger_for_filter_name')
    logger_for_filter_name.error('logger_for_filter_name')
    logger_for_filter_name.critical('logger_for_filter_name')
 
    logger_for_all = logging.getLogger('logger_for_all')
    logger_for_all.debug('logger_for_all')
    logger_for_all.info('logger_for_all')
    logger_for_all.warn('logger_for_all')
    logger_for_all.error('logger_for_all')
    logger_for_all.critical('logger_for_all')</span>

logging dictconfig的更多相关文章

  1. Python标准模块--logging

    1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不同 ...

  2. 以打印日志为荣之logging模块详细使用

    啄木鸟社区里的Pythonic八荣八耻有一条: 以打印日志为荣 , 以单步跟踪为耻; 很多程序都有记录日志的需求,并且日志中包含的信息既有正常的程序访问日志,还可能有错误.警告等信息输出,python ...

  3. (转)python logging模块

    python logging模块 原文:http://www.cnblogs.com/dahu-daqing/p/7040764.html 1 logging模块简介 logging模块是Python ...

  4. Python常用模块--logging

    (转载) 原文:http://www.cnblogs.com/dahu-daqing/p/7040764.html 1 logging模块简介 logging模块是Python内置的标准模块,主要用于 ...

  5. Python入门之logging模块

    本章目录: 一.logging模块简介 二.logging模块的使用 三.通过JSON或者YMAL文件配置logging模块 ===================================== ...

  6. python基础学习十 logging模块详细使用【转载】

    很多程序都有记录日志的需求,并且日志中包含的信息既有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,主要用于输出 ...

  7. Python标准模块--logging(转载)

    转载地址:http://www.cnblogs.com/zhbzz2007/p/5943685.html#undefined Python标准模块--logging 1 logging模块简介 log ...

  8. python logging模块【转载】

    转自:https://www.cnblogs.com/dahu-daqing/p/7040764.html 参考:老顽童log模块,讲的很细致,基本上拿到手就可以直接用了,很赞 1 logging模块 ...

  9. [转载] 每个 Python 程序员都要知道的日志实践

    原文: http://python.jobbole.com/81666/ 在现实生活中,记录日志非常重要.银行转账时会有转账记录:飞机飞行过程中,会有黑盒子(飞行数据记录器)记录飞行过程中的一切.如果 ...

随机推荐

  1. IOS Xcode编译项目-报错“ld: library not found for -XX”

    一般是因为导入新项目的时候报错的.原因是引入的依赖库的问题.重新执行pod install应该可以解决.不过,有时候如果重新执行pod install无法执行,可以采用以下方法: 在终端中cd到项目所 ...

  2. 从scratch到python——猜数游戏

    ` 之前讲解了从scratch到python,基于python turtle库的实现,讲解了用scratch和python turtle绘图的实现,以及让小猫动起来和当角色被单击的例子. 本节课讲继续 ...

  3. 理解R语言gdistance包下的transition函数

    library(raster)library(gdistance)r <- raster(nrows=3, ncols=4, xmn=0, xmx=7, ymn=0, ymx=6, crs=&q ...

  4. PyQt5——隐藏控件并保留位置

    原文地址:https://blog.csdn.net/qq_38161040/article/details/86605798 ———————————————————————————————— 设置控 ...

  5. 4 - BFS & Topological Algorithm

    615. Course Schedule https://www.lintcode.com/problem/course-schedule/description?_from=ladder&& ...

  6. C# interface 的隐式与显示实现及适应范围源码演示

    把代码过程中经常用到的一些代码段做个记录,如下的资料是关于C# interface 的隐式与显示实现及适应范围演示的代码. interface IAnimal { void Dog(); } clas ...

  7. OO-第一单元总结

    经过了前三次作业和两次实验的引导,我的编程思路在逐步从面向过程转向面向对象.也对面向对象有了初步的理解.虽然第一次实验由于自己没有及时完成导致没有提交过有些遗憾,但是第二次实验还是提交了几次的(虽然由 ...

  8. PHP以xml形式获取POST数据

    <?php namespace Home\Controller; use Think\Controller; class UrlController extends Controller { / ...

  9. css选择器以及使用场景

    1.选择器以及使用场景 id选择器:#header{} 类选择器:.header{} 元素选择器:div{} 子选择器:ul > li{} 后代选择器:div p{} 伪类选择器:a:hover ...

  10. Element-ui框架checkbox复选框回显

    先看下效果是不是你需要的..... 然后废话不多说,上代码,希望能够帮助到你... <template> <div class=''> <el-form label-wi ...