|
67 | 67 | from docopt import docopt |
68 | 68 | from docopt import DocoptExit |
69 | 69 | import requests |
| 70 | +from termcolor import colored |
70 | 71 | import yaml |
71 | 72 |
|
72 | 73 | __version__ = '0.12.0' |
@@ -363,16 +364,6 @@ class ResponseError(Exception): |
363 | 364 | pass |
364 | 365 |
|
365 | 366 |
|
366 | | -class MaxLevelFilter(logging.Filter): |
367 | | - '''Filters (lets through) all messages with level < LEVEL''' |
368 | | - def __init__(self, level): |
369 | | - self.level = level |
370 | | - |
371 | | - def filter(self, record): |
372 | | - # since logger.setLevel is inclusive, this should be exclusive |
373 | | - return record.levelno < self.level |
374 | | - |
375 | | - |
376 | 367 | class DeisClient(object): |
377 | 368 | """ |
378 | 369 | A client which interacts with a Deis controller. |
@@ -613,7 +604,22 @@ def apps_logs(self, args): |
613 | 604 | response = self._dispatch('post', |
614 | 605 | "/api/apps/{}/logs".format(app)) |
615 | 606 | if response.status_code == requests.codes.ok: # @UndefinedVariable |
616 | | - sys.stdout.write(response.json()) |
| 607 | + # strip the last newline character |
| 608 | + for line in response.json().split('\n')[:-1]: |
| 609 | + # get the tag from the log |
| 610 | + log_tag = line.split(': ')[0].split(' ')[2] |
| 611 | + # colorize the log based on the tag |
| 612 | + color = sum([ord(ch) for ch in log_tag]) % 6 |
| 613 | + def f(x): |
| 614 | + return { |
| 615 | + 0: 'green', |
| 616 | + 1: 'cyan', |
| 617 | + 2: 'red', |
| 618 | + 3: 'yellow', |
| 619 | + 4: 'blue', |
| 620 | + 5: 'magenta', |
| 621 | + }.get(x, 'magenta') |
| 622 | + self._logger.info(colored(line, f(color))) |
617 | 623 | else: |
618 | 624 | raise ResponseError(response) |
619 | 625 |
|
@@ -2148,15 +2154,15 @@ def _dispatch_cmd(method, args): |
2148 | 2154 |
|
2149 | 2155 |
|
2150 | 2156 | def _init_logger(): |
| 2157 | + logger = logging.getLogger(__name__) |
2151 | 2158 | stdoutHandler = logging.StreamHandler(sys.stdout) |
2152 | 2159 | stderrHandler = logging.StreamHandler(sys.stderr) |
2153 | | - stdoutHandler.addFilter(MaxLevelFilter(logging.WARNING)) |
2154 | | - stdoutHandler.setLevel(logging.DEBUG) |
| 2160 | + # TODO: add a --debug flag |
| 2161 | + logger.setLevel(logging.INFO) |
| 2162 | + stdoutHandler.setLevel(logging.INFO) |
2155 | 2163 | stderrHandler.setLevel(logging.WARNING) |
2156 | | - logger = logging.getLogger(__name__) |
2157 | 2164 | logger.addHandler(stdoutHandler) |
2158 | 2165 | logger.addHandler(stderrHandler) |
2159 | | - logger.setLevel(logging.DEBUG) |
2160 | 2166 |
|
2161 | 2167 |
|
2162 | 2168 | def main(): |
|
0 commit comments