Skip to content

Commit

Permalink
fixes #33, no-op logger when user silences logs
Browse files Browse the repository at this point in the history
when the user passes --quiet without specifying a --log-file,
there is no need to ever process log statements.
a no-op dummy object acts as a log.
  • Loading branch information
algrebe committed Feb 16, 2017
1 parent 9bac4e4 commit 510483d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
14 changes: 10 additions & 4 deletions basescript/basescript.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from .log import LevelLoggerFactory, BoundLevelLogger, StdlibStructlogHandler, StderrConsoleRenderer, Stream
from .stats import Stats
from .utils import Dummy

class BaseScript(object):
DESC = 'Base script abstraction'
Expand Down Expand Up @@ -133,6 +134,10 @@ def define_log_post_format_hooks(self):
return []

def _configure_logger(self):
"""
configures a logger when required write to stderr or a file
"""

# NOTE not thread safe. Multiple BaseScripts cannot be instantiated concurrently.
level = getattr(logging, self.args.log_level.upper())

Expand Down Expand Up @@ -189,10 +194,7 @@ def processor(logger, method_name, event_dict):
# TODO set mode and encoding appropriately
streams.append(open(self.args.log_file, 'a'))

if len(streams) == 0:
# no logging configured at all
# TODO what do we do in such cases ?
return
assert len(streams) != 0, "cannot configure logger for 0 streams"

stream = streams[0] if len(streams) == 1 else Stream(*streams)
atexit.register(stream.close)
Expand All @@ -214,6 +216,10 @@ def processor(logger, method_name, event_dict):
self._GLOBAL_LOG_CONFIGURED = True

def init_logger(self):
if self.args.quiet and self.args.log_file is None:
# no need for a log - return a dummy
return Dummy()

self._configure_logger()

# TODO bind relevant things to the basescript here ? name / hostname etc ?
Expand Down
9 changes: 9 additions & 0 deletions basescript/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Dummy(object):
def __init__(self, *args, **kwargs):
pass

def __call__(self, *args, **kwargs):
return self

def __getattr__(self, *args, **kwargs):
return self
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_long_description():

long_description = get_long_description()

version = '0.1.9'
version = '0.1.10'
setup(
name="basescript",
version=version,
Expand Down

0 comments on commit 510483d

Please sign in to comment.