/usr/lib/python2.7/dist-packages/apptools/logger/filtering_handler.py is in python-apptools 4.3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | #------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Enthought, Inc.
# Description: <Enthought pyface package component>
#------------------------------------------------------------------------------
""" A log handler that allows filtering of messages by origin. """
# Standard library imports.
import logging, inspect, os
# Local imports.
#
# fixme: This module was just copied over from 'envisage.core' (so
# that we don't rely on Envisage here!). Where should this module go?
from util import get_module_name
class FilteringHandler(logging.Handler):
""" A log handler that allows filtering of messages by origin.
Example
-------
::
from apptools.logger.api import DebugHandler, logger
handler = FilteringHandler(
include = {
'envisage.core' : True
},
exclude = {
'envisage.core.application' : False
}
)
logger.addHandler(handler)
Notes
-----
The boolean value specified for each name in the include and exclude
dictionaries indicates whether or not the include or exclude should pertain
to any sub-packages and modules.
The above example includes all log messages from anything contained in, or
under the 'envisage.core' package, EXCEPT for any log messages
from the 'envisage.core.application' module.
"""
###########################################################################
# 'object' interface.
###########################################################################
def __init__(self, include=None, exclude=None):
""" Creates a new handler. """
# Base class constructor.
logging.Handler.__init__(self)
# Packages or modules to include.
self.include = include is not None and include or {}
# Packages or modules to exclude.
self.exclude = exclude is not None and exclude or {}
return
###########################################################################
# 'Handler' interface.
###########################################################################
def emit(self, record):
""" Emits a log record. """
# Get the name of the module that the logger was called from.
module_name = self._get_module_name()
if len(self.include) == 0 or self._include(module_name):
if len(self.exclude) == 0 or not self._exclude(module_name):
self.filtered_emit(record)
return
###########################################################################
# 'Handler' interface.
###########################################################################
def filtered_emit(self, record):
""" Emits a log record if it has not been filtered. """
print record.getMessage()
return
###########################################################################
# Private interface.
###########################################################################
def _get_module_name(self):
""" Returns the module that the logger was actually called from. """
# fixem: Ahem.... what can I say... this gets us to the actual caller!
frame = inspect.currentframe()
frame = frame.f_back.f_back.f_back.f_back.f_back.f_back.f_back
# This returns a tuple containing the last 5 elements of the frame
# record which are:-
#
# - the filename
# - the line number
# - the function name
# - the list of lines of context from the source code
# - the index of the current line within that list
filename, lineno, funcname, source, index = inspect.getframeinfo(frame)
# The plugin definition's location is the directory containing the
# module that it is defined in.
self.location = os.path.dirname(filename)
# We can't use 'inspect.getmodulename' here as it gets confused because
# of our import hook in Envisage 8^(
#
# e.g. for the core plugin definition:-
#
# using inspect -> 'core_plugin_definition'
# using ours -> 'envisage.core.core_plugin_definition'
return get_module_name(filename)
def _include(self, module_name):
""" Is the module name in the include set? """
for item, include_children in self.include.items():
if item == module_name:
include = True
break
elif include_children and self._is_child_of(item, module_name):
include = True
break
else:
include = False
return include
def _exclude(self, module_name):
""" Is the module name in the exclude set? """
for item, exclude_children in self.exclude.items():
if item == module_name:
exclude = True
break
elif exclude_children and self._is_child_of(item, module_name):
exclude = True
break
else:
exclude = False
return exclude
def _is_child_of(self, x, y):
""" Is 'y' a child symbol of 'x'?
e.g.
'foo.bar.baz' (y) is a child of 'foo.bar' (x)
"""
if y.startswith(x):
x_atoms = x.split('.')
y_atoms = y.split('.')
is_child_of = y_atoms[:len(x_atoms)] == x_atoms
else:
is_child_of = False
return is_child_of
#### EOF ######################################################################
|