Commit 9986e569 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Globalize logger usage

Unsolved bug: redudant info in logfile
parent a5077876
......@@ -40,6 +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
_help = False
_debug = False
......@@ -168,34 +169,24 @@ cmd_spec_locations = [
def _setup_logging(silent=False, debug=False, verbose=False, include=False):
"""handle logging for clients package"""
def add_handler(name, level, prefix=''):
h = logging.StreamHandler()
fmt = logging.Formatter(prefix + '%(message)s')
h.setFormatter(fmt)
logger = logging.getLogger(name)
logger.addHandler(h)
logger.setLevel(level)
if silent:
add_handler('', logging.CRITICAL)
add_stream_logger(__name__, logging.CRITICAL)
return
if debug:
add_handler('requests', logging.INFO, prefix='* ')
add_handler('clients.send', logging.DEBUG, prefix='> ')
add_handler('clients.recv', logging.DEBUG, prefix='< ')
add_handler('kamaki', logging.DEBUG, prefix='(debug): ')
add_stream_logger('kamaki.clients.send', logging.DEBUG)
add_stream_logger('kamaki.clients.recv', logging.DEBUG)
add_stream_logger(__name__, logging.DEBUG)
elif verbose:
add_handler('requests', logging.INFO, prefix='* ')
add_handler('clients.send', logging.INFO, prefix='> ')
add_handler('clients.recv', logging.INFO, prefix='< ')
add_handler('kamaki', logging.INFO, prefix='(i): ')
add_stream_logger('kamaki.clients.send', logging.INFO)
add_stream_logger('kamaki.clients.recv', logging.INFO)
add_stream_logger(__name__, logging.INFO)
if include:
add_handler('data.send', logging.INFO, prefix='>[data]: ')
add_handler('data.recv', logging.INFO, prefix='<[data]: ')
add_handler('kamaki', logging.WARNING, prefix='(warning): ')
add_stream_logger('kamaki.clients.send', logging.INFO)
add_stream_logger('kamaki.clients.recv', logging.INFO)
add_stream_logger(__name__, logging.WARNING)
global kloger
kloger = logging.getLogger('kamaki')
kloger = get_logger(__name__)
def _init_session(arguments):
......
......@@ -34,7 +34,7 @@
from kamaki.cli.config import Config
from kamaki.cli.errors import CLISyntaxError, raiseCLIError
from kamaki.cli.utils import split_input
from logging import getLogger
from kamaki.logger import get_logger
from datetime import datetime as dtm
from time import mktime
......@@ -52,7 +52,7 @@ except ImportError:
# progress not installed - pls, pip install progress
pass
kloger = getLogger('kamaki')
log = get_logger('kamaki.cli')
class Argument(object):
......@@ -354,7 +354,7 @@ class ProgressBarArgument(FlagArgument):
try:
KamakiProgressBar
except NameError:
kloger.debug('WARNING: no progress bar functionality')
log.debug('WARNING: no progress bar functionality')
def clone(self):
"""Get a modifiable copy of this bar"""
......
......@@ -31,10 +31,9 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.command
from kamaki import logger
from kamaki.logger import get_logger
logger.add_file_logger('cli', __name__, filename=logger.get_log_filename())
sendlog = logger.get_logger('cli')
log = get_logger('kamaki.cli')
class _command_init(object):
......@@ -55,8 +54,8 @@ class _command_init(object):
self['config'].get('global', 'log_token') == 'on',
self['config'].get('global', 'log_data') == 'on')
except Exception as e:
sendlog.warning('Failed to read custom log settings: %s' % e)
sendlog.warning('\tdefaults for token and data logging are off')
log.warning('Failed to read custom log settings: %s' % e)
log.warning('\tdefaults for token and data logging are off')
pass
def _update_max_threads(self):
......@@ -65,8 +64,8 @@ class _command_init(object):
assert max_threads > 0
self.client.MAX_THREADS = max_threads
except Exception as e:
sendlog.warning('Failed to read custom thread settings: %s' % e)
sendlog.warning(
log.warning('Failed to read custom thread settings: %s' % e)
log.warning(
'\tdefault for max threads is %s' % self.client.MAX_THREADS)
pass
......
......@@ -32,18 +32,12 @@
# or implied, of GRNET S.A.command
from traceback import print_stack, print_exc
import logging
from kamaki.clients import ClientError
from kamaki.cli.errors import CLIError, raiseCLIError, CLISyntaxError
from kamaki.cli import _debug, kloger
from kamaki.cli.utils import format_size
sendlog = logging.getLogger('clients.send')
datasendlog = logging.getLogger('data.send')
recvlog = logging.getLogger('clients.recv')
datarecvlog = logging.getLogger('data.recv')
class generic(object):
......
......@@ -33,7 +33,6 @@
from sys import stdout
from time import localtime, strftime
from logging import getLogger
from os import path, makedirs, walk
from kamaki.cli import command
......@@ -49,9 +48,6 @@ from kamaki.cli.commands import _command_init, errors
from kamaki.clients.pithos import PithosClient, ClientError
from kamaki.clients.astakos import AstakosClient
kloger = getLogger('kamaki')
pithos_cmds = CommandTree('file', 'Pithos+/Storage API commands')
_commands = [pithos_cmds]
......
......@@ -31,10 +31,9 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
import logging
from kamaki.logger import get_logger
sendlog = logging.getLogger('clients.send')
recvlog = logging.getLogger('clients.recv')
log = get_logger('kamaki.cli')
class CLIError(Exception):
......@@ -101,7 +100,7 @@ def raiseCLIError(err, message='', importance=0, details=[]):
try:
stack = [e for e in stack if e != stack[1]]
except KeyError:
recvlog.debug('\n < '.join(stack))
log.debug('\n < '.join(stack))
details = ['%s' % details] if not isinstance(details, list)\
else list(details)
......
......@@ -31,14 +31,14 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.command
from logging import getLogger
from kamaki.logger import get_logger
from kamaki.cli import get_command_group, set_command_params
from kamaki.cli import print_subcommands_help, exec_cmd, update_parser_help
from kamaki.cli import _groups_help, _load_spec_module
kloger = getLogger('kamaki')
kloger = get_logger('kamaki.cli')
def _get_cmd_tree_from_spec(spec, cmd_tree_list):
......
......@@ -42,15 +42,15 @@ from random import random
from objpool.http import PooledHTTPConnection
from kamaki import logger
from kamaki.logger import add_file_logger, get_log_filename
TIMEOUT = 60.0 # seconds
HTTP_METHODS = ['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'COPY', 'MOVE']
log = logger.add_file_logger(__name__)
log.debug('Logging location: %s' % logger.get_log_filename())
sendlog = logger.add_file_logger('%s.send' % __name__)
recvlog = logger.add_file_logger('%s.recv' % __name__)
log = add_file_logger(__name__)
log.debug('Logging location: %s' % get_log_filename())
sendlog = add_file_logger('%s.send' % __name__)
recvlog = add_file_logger('%s.recv' % __name__)
def _encode(v):
......
......@@ -57,10 +57,11 @@ def set_log_filename(filename):
def _add_logger(name, level=None, filename=None, fmt=None):
log = logging.getLogger(name)
log = get_logger(name)
h = logging.FileHandler(filename) if (
filename) else logging.StreamHandler(filename)
h.setFormatter(logging.Formatter(fmt or '%(name)s %(message)s'))
filename) else logging.StreamHandler()
lfmt = logging.Formatter(fmt or '%(name)s\n %(message)s')
h.setFormatter(lfmt)
log.addHandler(h)
log.setLevel(level or logging.DEBUG)
return get_logger(name)
......@@ -77,7 +78,7 @@ def add_file_logger(name, level=None, filename=None):
def add_stream_logger(name, level=None):
try:
return _add_logger(name, level, fmt='%(name)s\n\t%(message)s')
return _add_logger(name, level)
except Exception:
return get_logger(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