Skip to content

Commit

Permalink
Merge pull request #96 from deep-compute/feature-minimal-logger
Browse files Browse the repository at this point in the history
Feature minimal logger
  • Loading branch information
jaswanth098 authored Sep 16, 2019
2 parents 6a45865 + f495c50 commit 0319d90
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ deploy:
- basescript/utils.py
- examples/adder.py
- examples/helloworld.py
name: basescript-0.3.2
tag_name: 0.3.2
name: basescript-0.3.3
tag_name: 0.3.3
true:
repo: deep-compute/basescript
- provider: pypi
Expand Down
7 changes: 7 additions & 0 deletions basescript/basescript.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, args=None):
pre_hooks=self.define_log_pre_format_hooks(),
post_hooks=self.define_log_post_format_hooks(),
metric_grouping_interval=self.args.metric_grouping_interval,
minimal=self.args.minimal,
)

self._flush_metrics_q = log._force_flush_q
Expand Down Expand Up @@ -169,6 +170,12 @@ def define_baseargs(self, parser):
action="store_true",
help="To run the code in debug mode",
)
parser.add_argument(
"--minimal",
default=False,
action="store_true",
help="Hide log keys such as id, host",
)

def define_args(self, parser):
"""
Expand Down
20 changes: 18 additions & 2 deletions basescript/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ def _structlog_default_keys_processor(logger_class, log_method, event):
return event


def _structlog_minimal_processor(logger_class, log_method, event):
for key in ("host", "id", "type"):
if key in event:
event.pop(key)

return event


@keeprunning()
def dump_metrics(log, interval):
global METRICS_STATE
Expand Down Expand Up @@ -358,7 +366,9 @@ def define_log_processors():
]


def _configure_logger(fmt, quiet, level, fpath, processors, metric_grouping_interval):
def _configure_logger(
fmt, quiet, level, fpath, processors, metric_grouping_interval, minimal
):
"""
configures a logger when required write to stderr or a file
"""
Expand All @@ -376,6 +386,9 @@ def _configure_logger(fmt, quiet, level, fpath, processors, metric_grouping_inte
if metric_grouping_interval:
_processors.append(metrics_grouping_processor)

if minimal:
_processors.append(_structlog_minimal_processor)

streams = []

if fpath:
Expand Down Expand Up @@ -418,6 +431,7 @@ def init_logger(
fpath=None,
processors=None,
metric_grouping_interval=None,
minimal=False,
):
"""
fmt=pretty/json controls only stderr; file always gets json.
Expand All @@ -434,7 +448,9 @@ def init_logger(
if not fmt and not quiet:
fmt = "pretty" if sys.stderr.isatty() else "json"

_configure_logger(fmt, quiet, level, fpath, processors, metric_grouping_interval)
_configure_logger(
fmt, quiet, level, fpath, processors, metric_grouping_interval, minimal
)

log = structlog.get_logger()
log._force_flush_q = queue.Queue(maxsize=FORCE_FLUSH_Q_SIZE)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def get_long_description():

long_description = get_long_description()

version = "0.3.2"
version = "0.3.3"
setup(
name="basescript",
version=version,
Expand Down

0 comments on commit 0319d90

Please sign in to comment.