1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# Ubuntu CI Engine
# Copyright 2013, 2014 Canonical Ltd.
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3, as
# published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import logging
import os
import sys
import ci_utils
LOG_STATUS_FILENAME = 'log_status.json'
STATUS_FILENAME = 'status.log'
def _setup_log_path(filename, directory=None):
log_dir = ci_utils.get_logs_dir()
if directory is not None:
log_dir = os.path.join(log_dir, directory)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
path = os.path.join(log_dir, filename)
return path
def _get_status_logfile():
return _setup_log_path(STATUS_FILENAME)
def _get_status_datafile(ticket_id):
return _setup_log_path(LOG_STATUS_FILENAME, directory=str(ticket_id))
def _init_logging():
# set up a root logger for everything
handler = logging.StreamHandler(stream=sys.stderr)
formatter = logging.Formatter(
'%(asctime)s %(levelname)-5s %(name)s: %(message)s',
datefmt='%H:%M:%S')
handler.setFormatter(formatter)
l = logging.getLogger('')
l.addHandler(handler)
l.setLevel(logging.DEBUG)
# set up a status logger for prodution testing
lander_status_logfile = _get_status_logfile()
if (lander_status_logfile and
os.path.exists(os.path.dirname(lander_status_logfile))):
l.info("Setting up status logger")
handler = logging.FileHandler(lander_status_logfile)
handler.setFormatter(formatter)
l = logging.getLogger('lander_status')
l.addHandler(handler)
l.setLevel(logging.DEBUG)
else:
l.info("Unable to set up status logger: %s", lander_status_logfile)
# the keystoneclient.httpclient debug logging leaks passwords. It enables
# this based on the setting of the "swiftclient" level
logging.getLogger('swiftclient').setLevel(logging.INFO)
return True
_init_logging.called = False
def get_logger(name):
if not _init_logging.called:
_init_logging.called = _init_logging()
return logging.getLogger(name)
def log_status(msg):
status_logger = get_logger('lander_status')
status_logger.info(msg)
def add_status(ticket_id, data):
data_file = _get_status_datafile(ticket_id)
with open(data_file, 'a') as fp:
fp.write(json.dumps(data))
fp.write('\n')
fp.flush()
|