Revision 42115b51 kamaki/clients/utils/__init__.py

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"""

Also available in: Unified diff