Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / logger.py @ bb50c4ec

History | View | Annotate | Download (3.4 kB)

1 42115b51 Stavros Sachtouris
# Copyright 2013 GRNET S.A. All rights reserved.
2 42115b51 Stavros Sachtouris
#
3 42115b51 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 42115b51 Stavros Sachtouris
# without modification, are permitted provided that the following
5 42115b51 Stavros Sachtouris
# conditions are met:
6 42115b51 Stavros Sachtouris
#
7 42115b51 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 42115b51 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 42115b51 Stavros Sachtouris
#      disclaimer.
10 42115b51 Stavros Sachtouris
#
11 42115b51 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 42115b51 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 42115b51 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 42115b51 Stavros Sachtouris
#      provided with the distribution.
15 42115b51 Stavros Sachtouris
#
16 42115b51 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 42115b51 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 42115b51 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 42115b51 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 42115b51 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 42115b51 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 42115b51 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 42115b51 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 42115b51 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 42115b51 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 42115b51 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 42115b51 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 42115b51 Stavros Sachtouris
#
29 42115b51 Stavros Sachtouris
# The views and conclusions contained in the software and
30 42115b51 Stavros Sachtouris
# documentation are those of the authors and should not be
31 42115b51 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 42115b51 Stavros Sachtouris
# or implied, of GRNET S.A.
33 42115b51 Stavros Sachtouris
34 edf00ab3 Stavros Sachtouris
from os import chmod
35 9e87e4bb Stavros Sachtouris
from os.path import expanduser
36 42115b51 Stavros Sachtouris
import logging
37 42115b51 Stavros Sachtouris
38 42115b51 Stavros Sachtouris
39 9e87e4bb Stavros Sachtouris
LOG_FILE = [expanduser('~/.kamaki.log')]
40 9dc724e5 Stavros Sachtouris
ALL = 0
41 9dc724e5 Stavros Sachtouris
42 9dc724e5 Stavros Sachtouris
_blacklist = {}
43 9dc724e5 Stavros Sachtouris
44 9dc724e5 Stavros Sachtouris
45 d761839f Stavros Sachtouris
def deactivate(name):
46 9dc724e5 Stavros Sachtouris
    """Deactivate a logger. Can be restored"""
47 9dc724e5 Stavros Sachtouris
    xlogger = logging.getLogger(name)
48 9dc724e5 Stavros Sachtouris
    _blacklist[name] = xlogger.level
49 9dc724e5 Stavros Sachtouris
    xlogger.setLevel(logging.CRITICAL)
50 9dc724e5 Stavros Sachtouris
51 9dc724e5 Stavros Sachtouris
52 9dc724e5 Stavros Sachtouris
def activate(name):
53 9dc724e5 Stavros Sachtouris
    """Restore a loggers settings"""
54 9dc724e5 Stavros Sachtouris
    old_logger = logging.getLogger(name)
55 9dc724e5 Stavros Sachtouris
    old_logger.setLevel(_blacklist.pop(name, old_logger.level))
56 9dc724e5 Stavros Sachtouris
57 9dc724e5 Stavros Sachtouris
58 392d902d Stavros Sachtouris
def if_logger_enabled(func):
59 9dc724e5 Stavros Sachtouris
    def wrap(name, *args, **kwargs):
60 9dc724e5 Stavros Sachtouris
        if name in _blacklist:
61 9dc724e5 Stavros Sachtouris
            return logging.getLogger(name)
62 392d902d Stavros Sachtouris
        return func(name, *args, **kwargs)
63 9dc724e5 Stavros Sachtouris
    return wrap
64 f47417e7 Stavros Sachtouris
65 f47417e7 Stavros Sachtouris
66 f47417e7 Stavros Sachtouris
def get_log_filename():
67 f47417e7 Stavros Sachtouris
    for logfile in LOG_FILE:
68 42115b51 Stavros Sachtouris
        try:
69 31a30991 Stavros Sachtouris
            with open(logfile, 'a+') as f:
70 42115b51 Stavros Sachtouris
                f.seek(0)
71 edf00ab3 Stavros Sachtouris
            chmod(logfile, 0600)
72 42115b51 Stavros Sachtouris
        except IOError:
73 42115b51 Stavros Sachtouris
            continue
74 42115b51 Stavros Sachtouris
        return logfile
75 42115b51 Stavros Sachtouris
    print('Failed to open any logging locations, file-logging aborted')
76 42115b51 Stavros Sachtouris
77 42115b51 Stavros Sachtouris
78 f47417e7 Stavros Sachtouris
def set_log_filename(filename):
79 f47417e7 Stavros Sachtouris
    global LOG_FILE
80 9dc724e5 Stavros Sachtouris
    LOG_FILE[0] = filename
81 f47417e7 Stavros Sachtouris
82 f47417e7 Stavros Sachtouris
83 e9db8806 Stavros Sachtouris
def _add_logger(name, level=None, filename=None, fmt=None):
84 9986e569 Stavros Sachtouris
    log = get_logger(name)
85 e9db8806 Stavros Sachtouris
    h = logging.FileHandler(filename) if (
86 9986e569 Stavros Sachtouris
        filename) else logging.StreamHandler()
87 9986e569 Stavros Sachtouris
    lfmt = logging.Formatter(fmt or '%(name)s\n %(message)s')
88 9986e569 Stavros Sachtouris
    h.setFormatter(lfmt)
89 e9db8806 Stavros Sachtouris
    log.addHandler(h)
90 e9db8806 Stavros Sachtouris
    log.setLevel(level or logging.DEBUG)
91 c4563114 Stavros Sachtouris
    return log
92 e9db8806 Stavros Sachtouris
93 e9db8806 Stavros Sachtouris
94 9dc724e5 Stavros Sachtouris
@if_logger_enabled
95 a5077876 Stavros Sachtouris
def add_file_logger(name, level=None, filename=None):
96 e9db8806 Stavros Sachtouris
    try:
97 e9db8806 Stavros Sachtouris
        return _add_logger(
98 e9db8806 Stavros Sachtouris
            name, level, filename or get_log_filename(),
99 e9db8806 Stavros Sachtouris
            fmt='%(name)s(%(levelname)s) %(asctime)s\n\t%(message)s')
100 e9db8806 Stavros Sachtouris
    except Exception:
101 e9db8806 Stavros Sachtouris
        return get_logger(name)
102 e9db8806 Stavros Sachtouris
103 e9db8806 Stavros Sachtouris
104 9dc724e5 Stavros Sachtouris
@if_logger_enabled
105 6e1f863b Stavros Sachtouris
def add_stream_logger(name, level=None, fmt=None):
106 42115b51 Stavros Sachtouris
    try:
107 6e1f863b Stavros Sachtouris
        return _add_logger(name, level, fmt=fmt)
108 42115b51 Stavros Sachtouris
    except Exception:
109 e9db8806 Stavros Sachtouris
        return get_logger(name)
110 42115b51 Stavros Sachtouris
111 42115b51 Stavros Sachtouris
112 9dc724e5 Stavros Sachtouris
@if_logger_enabled
113 42115b51 Stavros Sachtouris
def get_logger(name):
114 42115b51 Stavros Sachtouris
    return logging.getLogger(name)