mirror of
https://gitlab.com/apparmor/apparmor.git
synced 2025-03-04 08:24:42 +01:00
Merge branch 'feature/debuglogger-stdout' into 'master'
Extend common DebugLogger with option to log to stderr See merge request apparmor/apparmor!325 Acked-by: Eric Chiang <ericchiang@google.com> Acked-by: Christian Boltz <apparmor@cboltz.de>
This commit is contained in:
commit
3b4d1ed0e4
1 changed files with 21 additions and 4 deletions
|
@ -255,12 +255,20 @@ def type_is_str(var):
|
|||
else:
|
||||
return False
|
||||
|
||||
|
||||
class DebugLogger(object):
|
||||
'''Unified debug facility. Logs to file or stderr.
|
||||
|
||||
Does not log anything by default. Will only log if environment variable
|
||||
LOGPROF_DEBUG is set to a number between 1 and 3 or if method activateStderr
|
||||
is run.
|
||||
'''
|
||||
def __init__(self, module_name=__name__):
|
||||
self.debugging = False
|
||||
self.logfile = '/var/log/apparmor/logprof.log'
|
||||
self.debug_level = logging.DEBUG
|
||||
|
||||
if os.getenv('LOGPROF_DEBUG', False):
|
||||
self.logfile = '/var/log/apparmor/logprof.log'
|
||||
self.debugging = os.getenv('LOGPROF_DEBUG')
|
||||
try:
|
||||
self.debugging = int(self.debugging)
|
||||
|
@ -272,11 +280,11 @@ class DebugLogger(object):
|
|||
if self.debugging == 0: # debugging disabled, don't need to setup logging
|
||||
return
|
||||
if self.debugging == 1:
|
||||
self.debug_level = logging.ERROR
|
||||
self.debug_level = logging.ERROR # 40
|
||||
elif self.debugging == 2:
|
||||
self.debug_level = logging.INFO
|
||||
self.debug_level = logging.INFO # 20
|
||||
elif self.debugging == 3:
|
||||
self.debug_level = logging.DEBUG
|
||||
self.debug_level = logging.DEBUG # 10
|
||||
|
||||
try:
|
||||
logging.basicConfig(filename=self.logfile, level=self.debug_level,
|
||||
|
@ -292,6 +300,15 @@ class DebugLogger(object):
|
|||
|
||||
self.logger = logging.getLogger(module_name)
|
||||
|
||||
def activateStderr(self):
|
||||
self.debugging = True
|
||||
logging.basicConfig(
|
||||
level=self.debug_level,
|
||||
format='%(levelname)s: %(message)s',
|
||||
stream=sys.stderr,
|
||||
)
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
def error(self, message):
|
||||
if self.debugging:
|
||||
self.logger.error(message)
|
||||
|
|
Loading…
Add table
Reference in a new issue