Skip to content

Commit

Permalink
Allow to wryte simple messages to the console
Browse files Browse the repository at this point in the history
  • Loading branch information
nir0s committed Feb 19, 2018
1 parent 979dec9 commit 27bbe7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ $ wryte info my-message key1=value1 key2=value2 -j
```

### Wryting a simple log to the console

By default, Wryte will output `timestamp - level - logger_name - message` (and the provided key=value pairs) to the console. Many CLI applications log only the message (e.g. `pip`). You can configure Wryte to do so by either settings the `WRYTE_SIMPLE_CONSOLE` env var or by passing the `simple` flag when instantiating the logger:

```python
wryter = Wryte(simple=True)
wryter.info('My Message')
>>> 'My Message'
```


### Logging JSON strings

Expand Down
31 changes: 24 additions & 7 deletions wryte.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ def format(self, record):


class ConsoleFormatter(logging.Formatter):
def __init__(self, pretty=True, color=True):
def __init__(self, pretty=True, color=True, simple=False):
self.pretty = pretty
self.color = color
self.simple = simple or os.getenv('WRYTE_SIMPLE_CONSOLE')

@staticmethod
def _get_level_color(level):
Expand Down Expand Up @@ -104,7 +105,11 @@ def format(self, record):
timestamp = str(Fore.GREEN + timestamp + Style.RESET_ALL)
name = str(Fore.MAGENTA + name + Style.RESET_ALL)

msg = '{0} - {1} - {2} - {3}'.format(timestamp, name, level, message)
if self.simple:
msg = message
else:
msg = '{0} - {1} - {2} - {3}'.format(
timestamp, name, level, message)
if self.pretty:
for key, value in record.items():
msg += '\n {0}={1}'.format(key, value)
Expand All @@ -121,7 +126,8 @@ def __init__(self,
pretty=None,
bare=False,
jsonify=False,
color=True):
color=True,
simple=False):
"""Instantiate a logger instance.
Either a JSON or a Console handler will be added to the logger
Expand All @@ -137,6 +143,7 @@ def __init__(self,
self.logger = self._logger(name)

self.color = color
self.simple = simple
if not bare:
self._configure_handlers(level, jsonify)

Expand Down Expand Up @@ -204,7 +211,7 @@ def add_handler(self,
if COLOR_ENABLED:
colorama.init(autoreset=True)
pretty = True if self.pretty in (None, True) else False
_formatter = ConsoleFormatter(pretty, self.color)
_formatter = ConsoleFormatter(pretty, self.color, self.simple)
else:
_formatter = formatter
handler.setFormatter(_formatter)
Expand Down Expand Up @@ -413,9 +420,19 @@ class WryteError(Exception):
is_flag=True,
default=False,
help='Disable coloring in console formatter')
def main(level, message, objects, pretty, jsonify, name, no_color):
wryter = Wryte(name=name, pretty=pretty, level=level,
jsonify=jsonify, color=not no_color)
@click.option(
'--simple',
is_flag=True,
default=False,
help='Log only message to the console')
def main(level, message, objects, pretty, jsonify, name, no_color, simple):
wryter = Wryte(
name=name,
pretty=pretty,
level=level,
jsonify=jsonify,
color=not no_color,
simple=simple)
getattr(wryter, level.lower())(message, *objects)
else:
def main():
Expand Down

0 comments on commit 27bbe7b

Please sign in to comment.