Commit 9dc724e5 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Allow activation/deactivation of specific loggers

refs: #3715
Logger settings are not aborted and will be restored at deactivation

Also, remove all logger creation functionality from kamaki.clients
A file or stream logger should be created an the kamaki.clients caller side
In this case, the caller is kamaki.cli
Kamaki.cli should handle where and how the loggers will output. Kamaki.clients
will log as __name__ and the two extras: kamaki.clients.send/recv

An external app can now import kamaki and create a logger named "kamaki" and
this will log whatever kamaki is logging. The external app will handle loggers
as they wish.
parent 6634e01c
......@@ -40,7 +40,7 @@ from kamaki.cli.argument import ArgumentParseManager
from kamaki.cli.history import History
from kamaki.cli.utils import print_dict, red, magenta, yellow
from kamaki.cli.errors import CLIError
from kamaki.logger import add_stream_logger, get_logger
from kamaki import logger
_help = False
_debug = False
......@@ -48,6 +48,7 @@ _include = False
_verbose = False
_colors = False
kloger = None
filelog = None
# command auxiliary methods
......@@ -170,24 +171,25 @@ def _setup_logging(silent=False, debug=False, verbose=False, include=False):
"""handle logging for clients package"""
if silent:
add_stream_logger(__name__, logging.CRITICAL)
logger.add_stream_logger(__name__, logging.CRITICAL)
return
sfmt, rfmt = '> %(message)s', '< %(message)s'
if debug:
add_stream_logger('kamaki.clients.send', logging.DEBUG, sfmt)
add_stream_logger('kamaki.clients.recv', logging.DEBUG, rfmt)
add_stream_logger(__name__, logging.DEBUG)
print('Logging location: %s' % logger.get_log_filename())
logger.add_stream_logger('kamaki.clients.send', logging.DEBUG, sfmt)
logger.add_stream_logger('kamaki.clients.recv', logging.DEBUG, rfmt)
logger.add_stream_logger(__name__, logging.DEBUG)
elif verbose:
add_stream_logger('kamaki.clients.send', logging.INFO, sfmt)
add_stream_logger('kamaki.clients.recv', logging.INFO, rfmt)
add_stream_logger(__name__, logging.INFO)
logger.add_stream_logger('kamaki.clients.send', logging.INFO, sfmt)
logger.add_stream_logger('kamaki.clients.recv', logging.INFO, rfmt)
logger.add_stream_logger(__name__, logging.INFO)
if include:
add_stream_logger('kamaki.clients.send', logging.INFO, sfmt)
add_stream_logger('kamaki.clients.recv', logging.INFO, rfmt)
add_stream_logger(__name__, logging.WARNING)
logger.add_stream_logger('kamaki.clients.send', logging.INFO, sfmt)
logger.add_stream_logger('kamaki.clients.recv', logging.INFO, rfmt)
logger.add_stream_logger(__name__, logging.WARNING)
global kloger
kloger = get_logger(__name__)
kloger = logger.get_logger(__name__)
def _init_session(arguments):
......@@ -385,6 +387,10 @@ def main():
from kamaki.logger import set_log_filename
set_log_filename(log_file)
global filelog
filelog = logger.add_file_logger(__name__.split('.')[0])
filelog.info('Logging location: %s' % logger.get_log_filename())
_init_session(parser.arguments)
from kamaki.cli.utils import suggest_missing
......
......@@ -42,13 +42,13 @@ from random import random
from objpool.http import PooledHTTPConnection
from kamaki.logger import add_file_logger, get_log_filename, get_logger
from kamaki.logger import get_logger
TIMEOUT = 60.0 # seconds
HTTP_METHODS = ['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'COPY', 'MOVE']
log = add_file_logger(__name__)
log.debug('Logging location: %s' % get_log_filename())
log = get_logger(__name__)
sendlog = get_logger('%s.send' % __name__)
recvlog = get_logger('%s.recv' % __name__)
......
......@@ -37,6 +37,30 @@ import logging
LOG_FILE = [expanduser('~/.kamaki.log')]
ALL = 0
_blacklist = {}
def diactivate(name):
"""Deactivate a logger. Can be restored"""
xlogger = logging.getLogger(name)
_blacklist[name] = xlogger.level
xlogger.setLevel(logging.CRITICAL)
def activate(name):
"""Restore a loggers settings"""
old_logger = logging.getLogger(name)
old_logger.setLevel(_blacklist.pop(name, old_logger.level))
def if_logger_enabled(foo):
def wrap(name, *args, **kwargs):
if name in _blacklist:
return logging.getLogger(name)
return foo(name, *args, **kwargs)
return wrap
def get_log_filename():
......@@ -53,7 +77,7 @@ def get_log_filename():
def set_log_filename(filename):
global LOG_FILE
LOG_FILE = [filename] + LOG_FILE
LOG_FILE[0] = filename
def _add_logger(name, level=None, filename=None, fmt=None):
......@@ -67,6 +91,7 @@ def _add_logger(name, level=None, filename=None, fmt=None):
return log
@if_logger_enabled
def add_file_logger(name, level=None, filename=None):
try:
return _add_logger(
......@@ -76,6 +101,7 @@ def add_file_logger(name, level=None, filename=None):
return get_logger(name)
@if_logger_enabled
def add_stream_logger(name, level=None, fmt=None):
try:
return _add_logger(name, level, fmt=fmt)
......@@ -83,5 +109,6 @@ def add_stream_logger(name, level=None, fmt=None):
return get_logger(name)
@if_logger_enabled
def get_logger(name):
return logging.getLogger(name)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment