commit 9986e56967a110a4718bf433177716b0589d7d5b
Author: Stavros Sachtouris <saxtouri@admin.grnet.gr>
Date:   Thu May 9 19:06:48 2013 +0300

    Globalize logger usage
    
    Unsolved bug: redudant info in logfile

diff --git a/kamaki/cli/__init__.py b/kamaki/cli/__init__.py
index 3309d0c..4576ee7 100644
--- a/kamaki/cli/__init__.py
+++ b/kamaki/cli/__init__.py
@@ -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):
diff --git a/kamaki/cli/argument.py b/kamaki/cli/argument.py
index 9d31459..6618110 100644
--- a/kamaki/cli/argument.py
+++ b/kamaki/cli/argument.py
@@ -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"""
diff --git a/kamaki/cli/commands/__init__.py b/kamaki/cli/commands/__init__.py
index 7f25fa3..13e13ba 100644
--- a/kamaki/cli/commands/__init__.py
+++ b/kamaki/cli/commands/__init__.py
@@ -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
 
diff --git a/kamaki/cli/commands/errors.py b/kamaki/cli/commands/errors.py
index c31c890..0553b7e 100644
--- a/kamaki/cli/commands/errors.py
+++ b/kamaki/cli/commands/errors.py
@@ -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):
 
diff --git a/kamaki/cli/commands/pithos.py b/kamaki/cli/commands/pithos.py
index a2bd17a..f5309b7 100644
--- a/kamaki/cli/commands/pithos.py
+++ b/kamaki/cli/commands/pithos.py
@@ -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]
 
diff --git a/kamaki/cli/errors.py b/kamaki/cli/errors.py
index 4d6b1a8..e15bf32 100644
--- a/kamaki/cli/errors.py
+++ b/kamaki/cli/errors.py
@@ -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)
diff --git a/kamaki/cli/one_command.py b/kamaki/cli/one_command.py
index dcc20b2..9638004 100644
--- a/kamaki/cli/one_command.py
+++ b/kamaki/cli/one_command.py
@@ -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):
diff --git a/kamaki/clients/__init__.py b/kamaki/clients/__init__.py
index aee61a8..8fa94c9 100644
--- a/kamaki/clients/__init__.py
+++ b/kamaki/clients/__init__.py
@@ -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):
diff --git a/kamaki/logger.py b/kamaki/logger.py
index 68b4830..9648a58 100644
--- a/kamaki/logger.py
+++ b/kamaki/logger.py
@@ -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)
 
