Statistics
| Branch: | Tag: | Revision:

root / kamaki / logger.py @ 6e1f863b

History | View | Annotate | Download (2.8 kB)

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
from os import chmod
35
from os.path import expanduser
36
import logging
37

    
38

    
39
LOG_FILE = [expanduser('~/.kamaki.log')]
40

    
41

    
42
def get_log_filename():
43
    for logfile in LOG_FILE:
44
        try:
45
            with open(logfile, 'a+') as f:
46
                f.seek(0)
47
            chmod(logfile, 0600)
48
        except IOError:
49
            continue
50
        return logfile
51
    print('Failed to open any logging locations, file-logging aborted')
52

    
53

    
54
def set_log_filename(filename):
55
    global LOG_FILE
56
    LOG_FILE = [filename] + LOG_FILE
57

    
58

    
59
def _add_logger(name, level=None, filename=None, fmt=None):
60
    log = get_logger(name)
61
    h = logging.FileHandler(filename) if (
62
        filename) else logging.StreamHandler()
63
    lfmt = logging.Formatter(fmt or '%(name)s\n %(message)s')
64
    h.setFormatter(lfmt)
65
    log.addHandler(h)
66
    log.setLevel(level or logging.DEBUG)
67
    return get_logger(name)
68

    
69

    
70
def add_file_logger(name, level=None, filename=None):
71
    try:
72
        return _add_logger(
73
            name, level, filename or get_log_filename(),
74
            fmt='%(name)s(%(levelname)s) %(asctime)s\n\t%(message)s')
75
    except Exception:
76
        return get_logger(name)
77

    
78

    
79
def add_stream_logger(name, level=None, fmt=None):
80
    try:
81
        return _add_logger(name, level, fmt=fmt)
82
    except Exception:
83
        return get_logger(name)
84

    
85

    
86
def get_logger(name):
87
    return logging.getLogger(name)