Move logger operations to a seperate file/pkg
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Tue, 26 Mar 2013 10:23:57 +0000 (12:23 +0200)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Tue, 26 Mar 2013 10:23:57 +0000 (12:23 +0200)
kamaki/clients/__init__.py
kamaki/clients/utils/__init__.py
kamaki/clients/utils/logger.py [new file with mode: 0644]

index 6d6e9c5..28377ef 100644 (file)
@@ -36,23 +36,23 @@ from threading import Thread
 from json import dumps, loads
 from time import time
 
-from kamaki.clients.utils import get_logger, add_file_logger, get_log_filename
+from kamaki.clients.utils import logger
 from kamaki.clients.connection.kamakicon import KamakiHTTPConnection
 from kamaki.clients.connection.errors import KamakiConnectionError
 from kamaki.clients.connection.errors import KamakiResponseError
 
 LOG_TOKEN = False
-DEBUG_LOG = get_log_filename()
+DEBUG_LOG = logger.get_log_filename()
 
-add_file_logger('clients.send', __name__, filename=DEBUG_LOG)
-sendlog = get_logger('clients.send')
+logger.add_file_logger('clients.send', __name__, filename=DEBUG_LOG)
+sendlog = logger.get_logger('clients.send')
 sendlog.debug('Logging location: %s' % DEBUG_LOG)
-add_file_logger('data.send', __name__, filename=DEBUG_LOG)
-datasendlog = get_logger('data.send')
-add_file_logger('clients.recv', __name__, filename=DEBUG_LOG)
-recvlog = get_logger('clients.recv')
-add_file_logger('data.recv', __name__, filename=DEBUG_LOG)
-datarecvlog = get_logger('data.recv')
+logger.add_file_logger('data.send', __name__, filename=DEBUG_LOG)
+datasendlog = logger.get_logger('data.send')
+logger.add_file_logger('clients.recv', __name__, filename=DEBUG_LOG)
+recvlog = logger.get_logger('clients.recv')
+logger.add_file_logger('data.recv', __name__, filename=DEBUG_LOG)
+datarecvlog = logger.get_logger('data.recv')
 
 
 class ClientError(Exception):
index a574e4b..bb9dc72 100644 (file)
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-import logging
-
-
-def get_log_filename(filename=(
-        '/var/log/kamaki.log',
-        '/var/log/kamaki/clients.log',
-        '/tmp/kamaki.log',
-        'kamaki.log')):
-    if not (isinstance(filename, list) or isinstance(filename, tuple)):
-        filename = (filename,)
-    for logfile in filename:
-        try:
-            with open(logfile) as f:
-                f.seek(0)
-        except IOError:
-            continue
-        return logfile
-    print('Failed to open any logging locations')
-
-
-def add_file_logger(
-        name, caller,
-        level=logging.DEBUG, prefix='', filename='/tmp/kamaki.log'):
-    try:
-        assert caller and filename
-        logger = logging.getLogger(name)
-        h = logging.FileHandler(filename)
-        fmt = logging.Formatter(
-            '%(asctime)s ' + caller + ' %(name)s-%(levelname)s: %(message)s')
-        h.setFormatter(fmt)
-        logger.addHandler(h)
-        logger.setLevel(level)
-    except Exception:
-        pass
-
-
-def get_logger(name):
-    return logging.getLogger(name)
-
 
 def _matches(val1, val2, exactMath=True):
     """Case Insensitive match"""
diff --git a/kamaki/clients/utils/logger.py b/kamaki/clients/utils/logger.py
new file mode 100644 (file)
index 0000000..cc36889
--- /dev/null
@@ -0,0 +1,71 @@
+# Copyright 2013 GRNET S.A. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or
+# without modification, are permitted provided that the following
+# conditions are met:
+#
+#   1. Redistributions of source code must retain the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials
+#      provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and
+# documentation are those of the authors and should not be
+# interpreted as representing official policies, either expressed
+# or implied, of GRNET S.A.
+
+import logging
+
+
+def get_log_filename(filename=(
+        '/var/log/kamaki.log',
+        '/var/log/kamaki/clients.log',
+        '/tmp/kamaki.log',
+        'kamaki.log')):
+    if not (isinstance(filename, list) or isinstance(filename, tuple)):
+        filename = (filename,)
+    for logfile in filename:
+        try:
+            with open(logfile) as f:
+                f.seek(0)
+        except IOError:
+            continue
+        return logfile
+    print('Failed to open any logging locations, file-logging aborted')
+
+
+def add_file_logger(
+        name, caller,
+        level=logging.DEBUG, prefix='', filename='/tmp/kamaki.log'):
+    try:
+        assert caller and filename
+        logger = logging.getLogger(name)
+        h = logging.FileHandler(filename)
+        fmt = logging.Formatter(
+            '%(asctime)s ' + caller + ' %(name)s-%(levelname)s: %(message)s')
+        h.setFormatter(fmt)
+        logger.addHandler(h)
+        logger.setLevel(level)
+    except Exception:
+        pass
+
+
+def get_logger(name):
+    return logging.getLogger(name)