Revision 42115b51

b/kamaki/clients/__init__.py
36 36
from json import dumps, loads
37 37
from time import time
38 38

  
39
from kamaki.clients.utils import get_logger, add_file_logger, get_log_filename
39
from kamaki.clients.utils import logger
40 40
from kamaki.clients.connection.kamakicon import KamakiHTTPConnection
41 41
from kamaki.clients.connection.errors import KamakiConnectionError
42 42
from kamaki.clients.connection.errors import KamakiResponseError
43 43

  
44 44
LOG_TOKEN = False
45
DEBUG_LOG = get_log_filename()
45
DEBUG_LOG = logger.get_log_filename()
46 46

  
47
add_file_logger('clients.send', __name__, filename=DEBUG_LOG)
48
sendlog = get_logger('clients.send')
47
logger.add_file_logger('clients.send', __name__, filename=DEBUG_LOG)
48
sendlog = logger.get_logger('clients.send')
49 49
sendlog.debug('Logging location: %s' % DEBUG_LOG)
50
add_file_logger('data.send', __name__, filename=DEBUG_LOG)
51
datasendlog = get_logger('data.send')
52
add_file_logger('clients.recv', __name__, filename=DEBUG_LOG)
53
recvlog = get_logger('clients.recv')
54
add_file_logger('data.recv', __name__, filename=DEBUG_LOG)
55
datarecvlog = get_logger('data.recv')
50
logger.add_file_logger('data.send', __name__, filename=DEBUG_LOG)
51
datasendlog = logger.get_logger('data.send')
52
logger.add_file_logger('clients.recv', __name__, filename=DEBUG_LOG)
53
recvlog = logger.get_logger('clients.recv')
54
logger.add_file_logger('data.recv', __name__, filename=DEBUG_LOG)
55
datarecvlog = logger.get_logger('data.recv')
56 56

  
57 57

  
58 58
class ClientError(Exception):
b/kamaki/clients/utils/__init__.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
import logging
35

  
36

  
37
def get_log_filename(filename=(
38
        '/var/log/kamaki.log',
39
        '/var/log/kamaki/clients.log',
40
        '/tmp/kamaki.log',
41
        'kamaki.log')):
42
    if not (isinstance(filename, list) or isinstance(filename, tuple)):
43
        filename = (filename,)
44
    for logfile in filename:
45
        try:
46
            with open(logfile) as f:
47
                f.seek(0)
48
        except IOError:
49
            continue
50
        return logfile
51
    print('Failed to open any logging locations')
52

  
53

  
54
def add_file_logger(
55
        name, caller,
56
        level=logging.DEBUG, prefix='', filename='/tmp/kamaki.log'):
57
    try:
58
        assert caller and filename
59
        logger = logging.getLogger(name)
60
        h = logging.FileHandler(filename)
61
        fmt = logging.Formatter(
62
            '%(asctime)s ' + caller + ' %(name)s-%(levelname)s: %(message)s')
63
        h.setFormatter(fmt)
64
        logger.addHandler(h)
65
        logger.setLevel(level)
66
    except Exception:
67
        pass
68

  
69

  
70
def get_logger(name):
71
    return logging.getLogger(name)
72

  
73 34

  
74 35
def _matches(val1, val2, exactMath=True):
75 36
    """Case Insensitive match"""
b/kamaki/clients/utils/logger.py
1
# Copyright 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

  
34
import logging
35

  
36

  
37
def get_log_filename(filename=(
38
        '/var/log/kamaki.log',
39
        '/var/log/kamaki/clients.log',
40
        '/tmp/kamaki.log',
41
        'kamaki.log')):
42
    if not (isinstance(filename, list) or isinstance(filename, tuple)):
43
        filename = (filename,)
44
    for logfile in filename:
45
        try:
46
            with open(logfile) as f:
47
                f.seek(0)
48
        except IOError:
49
            continue
50
        return logfile
51
    print('Failed to open any logging locations, file-logging aborted')
52

  
53

  
54
def add_file_logger(
55
        name, caller,
56
        level=logging.DEBUG, prefix='', filename='/tmp/kamaki.log'):
57
    try:
58
        assert caller and filename
59
        logger = logging.getLogger(name)
60
        h = logging.FileHandler(filename)
61
        fmt = logging.Formatter(
62
            '%(asctime)s ' + caller + ' %(name)s-%(levelname)s: %(message)s')
63
        h.setFormatter(fmt)
64
        logger.addHandler(h)
65
        logger.setLevel(level)
66
    except Exception:
67
        pass
68

  
69

  
70
def get_logger(name):
71
    return logging.getLogger(name)

Also available in: Unified diff