Skip to content

Commit 9b72b74

Browse files
author
Matthew Fisher
committed
feat(client): add colorized logging
1 parent de7ed01 commit 9b72b74

5 files changed

Lines changed: 26 additions & 18 deletions

File tree

client/deis.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
from docopt import docopt
6868
from docopt import DocoptExit
6969
import requests
70+
from termcolor import colored
7071
import yaml
7172

7273
__version__ = '0.12.0'
@@ -363,16 +364,6 @@ class ResponseError(Exception):
363364
pass
364365

365366

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-
376367
class DeisClient(object):
377368
"""
378369
A client which interacts with a Deis controller.
@@ -613,7 +604,22 @@ def apps_logs(self, args):
613604
response = self._dispatch('post',
614605
"/api/apps/{}/logs".format(app))
615606
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)))
617623
else:
618624
raise ResponseError(response)
619625

@@ -2148,15 +2154,15 @@ def _dispatch_cmd(method, args):
21482154

21492155

21502156
def _init_logger():
2157+
logger = logging.getLogger(__name__)
21512158
stdoutHandler = logging.StreamHandler(sys.stdout)
21522159
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)
21552163
stderrHandler.setLevel(logging.WARNING)
2156-
logger = logging.getLogger(__name__)
21572164
logger.addHandler(stdoutHandler)
21582165
logger.addHandler(stderrHandler)
2159-
logger.setLevel(logging.DEBUG)
21602166

21612167

21622168
def main():

client/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
long_description=LONG_DESCRIPTION,
5959
install_requires=[
6060
'docopt==0.6.2', 'python-dateutil==2.2',
61-
'PyYAML==3.11', 'requests==2.3.0'
61+
'PyYAML==3.11', 'requests==2.3.0',
62+
'termcolor==1.1.0'
6263
],
6364
zip_safe=True,
6465
**KWARGS)

docs/docs_requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ docopt==0.6.2
2828
python-dateutil==2.2
2929
#PyYAML==3.11
3030
requests==2.3.0
31+
termcolor==1.1.0
3132

3233
# Deis documentation requirements
3334
Sphinx>=1.2.2

tests/bin/build-deis-cli.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
virtualenv --system-site-packages venv
66
. venv/bin/activate
7-
pip install docopt==0.6.2 python-dateutil==2.2 PyYAML==3.11 requests==2.3.0 pyinstaller==2.1
7+
pip install docopt==0.6.2 python-dateutil==2.2 PyYAML==3.11 requests==2.3.0 pyinstaller==2.1 termcolor==1.1.0
88
make -C client/ client

tests/bin/test-integration.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ set -e
3131
# Build updated Deis CLI and use it for testing
3232
virtualenv --system-site-packages venv
3333
. venv/bin/activate
34-
pip install docopt==0.6.2 python-dateutil==2.2 PyYAML==3.11 requests==2.3.0 pyinstaller==2.1
34+
pip install docopt==0.6.2 python-dateutil==2.2 PyYAML==3.11 requests==2.3.0 pyinstaller==2.1 termcolor==1.1.0
3535
make -C client/ client
3636
chmod +x client/dist/deis
3737
export PATH=`pwd`/client/dist:$PATH

0 commit comments

Comments
 (0)