Skip to content

Commit 4561c14

Browse files
authored
Merge pull request #163 from botify-labs/enhancement/162/Logging-add-exception-information
Logging: add exception information
2 parents 0f69be3 + 2b5c220 commit 4561c14

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

simpleflow/command.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import swf.querysets
1919

2020
from simpleflow.history import History
21+
from simpleflow.settings import logging_formatter
22+
from simpleflow.settings.logging_formatter import ColorModes
2123
from simpleflow.swf.stats import pretty
2224
from simpleflow.swf import helpers
2325
from simpleflow.swf.process import decider
@@ -76,11 +78,14 @@ def comma_separated_list(value):
7678
@click.group()
7779
@click.option('--format')
7880
@click.option('--header/--no-header', default=False)
81+
@click.option('--color', type=click.Choice([ColorModes.AUTO, ColorModes.ALWAYS, ColorModes.NEVER]),
82+
default=ColorModes.AUTO)
7983
@click.version_option(version=__version__)
8084
@click.pass_context
81-
def cli(ctx, header, format):
85+
def cli(ctx, header, format, color):
8286
ctx.params['format'] = format
8387
ctx.params['header'] = header
88+
logging_formatter.color_mode = color
8489

8590

8691
def get_workflow_type(domain_name, workflow_class):

simpleflow/settings/logging_formatter.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@
1010
END = '\033[0m'
1111

1212

13+
class ColorModes(object):
14+
AUTO = 'auto'
15+
ALWAYS = 'always'
16+
NEVER = 'never'
17+
18+
color_mode = 'auto'
19+
20+
1321
def colorize(level, message):
1422
# if not in a tty, we're likely redirected or piped
15-
if not sys.stdout.isatty():
23+
if color_mode == ColorModes.NEVER or (color_mode == ColorModes.AUTO and not sys.stdout.isatty()):
1624
return message
1725

1826
# color mappings
@@ -60,4 +68,27 @@ def format(self, record):
6068
record.isodate = date.isoformat()
6169
record.message = record.msg % record.args
6270
record.coloredlevel = colorize(record.levelname, record.levelname)
63-
return "%(isodate)s %(coloredlevel)s [process=%(processName)s, pid=%(process)s]: %(message)s" % record.__dict__
71+
s = "%(isodate)s %(coloredlevel)s [process=%(processName)s, pid=%(process)s]: %(message)s" % record.__dict__
72+
73+
# C&P from logging.Formatter#format
74+
if record.exc_info:
75+
# Cache the traceback text to avoid converting it multiple times
76+
# (it's constant anyway)
77+
if not record.exc_text:
78+
record.exc_text = self.formatException(record.exc_info)
79+
if record.exc_text:
80+
if s[-1:] != "\n":
81+
s = s + "\n"
82+
try:
83+
s = s + record.exc_text
84+
except UnicodeError:
85+
# Sometimes filenames have non-ASCII chars, which can lead
86+
# to errors when s is Unicode and record.exc_text is str
87+
# See issue 8924.
88+
# We also use replace for when there are multiple
89+
# encodings, e.g. UTF-8 for the filesystem and latin-1
90+
# for a script. See issue 13232.
91+
s = s + record.exc_text.decode(sys.getfilesystemencoding(),
92+
'replace')
93+
94+
return s

0 commit comments

Comments
 (0)