~pwlars/uci-engine/tr-pass-no-tests

« back to all changes in this revision

Viewing changes to lander/lander/__init__.py

  • Committer: paul.larson at canonical
  • Date: 2014-08-12 23:12:36 UTC
  • mfrom: (740.1.5 uci-engine)
  • Revision ID: paul.larson@canonical.com-20140812231236-jaulj4b9xr0v6k8y
resolve merge conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
# You should have received a copy of the GNU Affero General Public License
14
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
15
 
 
16
import json
16
17
import logging
 
18
import os
17
19
import sys
18
20
 
 
21
import ci_utils
 
22
 
 
23
 
 
24
LOG_STATUS_FILENAME = 'log_status.json'
 
25
STATUS_FILENAME = 'status.log'
 
26
 
 
27
 
 
28
def _setup_log_path(filename, directory=None):
 
29
    log_dir = ci_utils.get_logs_dir()
 
30
 
 
31
    if directory is not None:
 
32
        log_dir = os.path.join(log_dir, directory)
 
33
 
 
34
    if not os.path.exists(log_dir):
 
35
        os.makedirs(log_dir)
 
36
 
 
37
    path = os.path.join(log_dir, filename)
 
38
 
 
39
    return path
 
40
 
 
41
 
 
42
def _get_status_logfile():
 
43
    return _setup_log_path(STATUS_FILENAME)
 
44
 
 
45
 
 
46
def _get_status_datafile(ticket_id):
 
47
    return _setup_log_path(LOG_STATUS_FILENAME, directory=str(ticket_id))
 
48
 
19
49
 
20
50
def _init_logging():
21
51
    # set up a root logger for everything
28
58
    l.addHandler(handler)
29
59
    l.setLevel(logging.DEBUG)
30
60
 
 
61
    # set up a status logger for prodution testing
 
62
    lander_status_logfile = _get_status_logfile()
 
63
    if (lander_status_logfile and
 
64
            os.path.exists(os.path.dirname(lander_status_logfile))):
 
65
        l.info("Setting up status logger")
 
66
        handler = logging.FileHandler(lander_status_logfile)
 
67
        handler.setFormatter(formatter)
 
68
        l = logging.getLogger('lander_status')
 
69
        l.addHandler(handler)
 
70
        l.setLevel(logging.DEBUG)
 
71
    else:
 
72
        l.info("Unable to set up status logger: %s", lander_status_logfile)
 
73
 
31
74
    # the keystoneclient.httpclient debug logging leaks passwords. It enables
32
75
    # this based on the setting of the "swiftclient" level
33
76
    logging.getLogger('swiftclient').setLevel(logging.INFO)
40
83
    if not _init_logging.called:
41
84
        _init_logging.called = _init_logging()
42
85
    return logging.getLogger(name)
 
86
 
 
87
 
 
88
def log_status(msg):
 
89
    status_logger = get_logger('lander_status')
 
90
    status_logger.info(msg)
 
91
 
 
92
 
 
93
def add_status(ticket_id, data):
 
94
    data_file = _get_status_datafile(ticket_id)
 
95
    with open(data_file, 'a') as fp:
 
96
        fp.write(json.dumps(data))
 
97
        fp.write('\n')
 
98
        fp.flush()