root / kamaki / cli / logger.py @ 5576a4eb
History | View | Annotate | Download (3.4 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 |
ALL = 0
|
41 |
|
42 |
_blacklist = {} |
43 |
|
44 |
|
45 |
def deactivate(name): |
46 |
"""Deactivate a logger. Can be restored"""
|
47 |
xlogger = logging.getLogger(name) |
48 |
_blacklist[name] = xlogger.level |
49 |
xlogger.setLevel(logging.CRITICAL) |
50 |
|
51 |
|
52 |
def activate(name): |
53 |
"""Restore a loggers settings"""
|
54 |
old_logger = logging.getLogger(name) |
55 |
old_logger.setLevel(_blacklist.pop(name, old_logger.level)) |
56 |
|
57 |
|
58 |
def if_logger_enabled(foo): |
59 |
def wrap(name, *args, **kwargs): |
60 |
if name in _blacklist: |
61 |
return logging.getLogger(name)
|
62 |
return foo(name, *args, **kwargs)
|
63 |
return wrap
|
64 |
|
65 |
|
66 |
def get_log_filename(): |
67 |
for logfile in LOG_FILE: |
68 |
try:
|
69 |
with open(logfile, 'a+') as f: |
70 |
f.seek(0)
|
71 |
chmod(logfile, 0600)
|
72 |
except IOError: |
73 |
continue
|
74 |
return logfile
|
75 |
print('Failed to open any logging locations, file-logging aborted')
|
76 |
|
77 |
|
78 |
def set_log_filename(filename): |
79 |
global LOG_FILE
|
80 |
LOG_FILE[0] = filename
|
81 |
|
82 |
|
83 |
def _add_logger(name, level=None, filename=None, fmt=None): |
84 |
log = get_logger(name) |
85 |
h = logging.FileHandler(filename) if (
|
86 |
filename) else logging.StreamHandler()
|
87 |
lfmt = logging.Formatter(fmt or '%(name)s\n %(message)s') |
88 |
h.setFormatter(lfmt) |
89 |
log.addHandler(h) |
90 |
log.setLevel(level or logging.DEBUG)
|
91 |
return log
|
92 |
|
93 |
|
94 |
@if_logger_enabled
|
95 |
def add_file_logger(name, level=None, filename=None): |
96 |
try:
|
97 |
return _add_logger(
|
98 |
name, level, filename or get_log_filename(),
|
99 |
fmt='%(name)s(%(levelname)s) %(asctime)s\n\t%(message)s')
|
100 |
except Exception: |
101 |
return get_logger(name)
|
102 |
|
103 |
|
104 |
@if_logger_enabled
|
105 |
def add_stream_logger(name, level=None, fmt=None): |
106 |
try:
|
107 |
return _add_logger(name, level, fmt=fmt)
|
108 |
except Exception: |
109 |
return get_logger(name)
|
110 |
|
111 |
|
112 |
@if_logger_enabled
|
113 |
def get_logger(name): |
114 |
return logging.getLogger(name)
|