Skip to content

Commit

Permalink
Send messages to appropriate streams
Browse files Browse the repository at this point in the history
INFO goes to stdout, everything else goes to stderr

Fixes #144
  • Loading branch information
goldmann committed Mar 14, 2017
1 parent 4c5a61d commit 8f6282a
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions docker_squash/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
from docker_squash.version import version


# Source: http://stackoverflow.com/questions/1383254/logging-streamhandler-and-standard-streams
class SingleLevelFilter(logging.Filter):
def __init__(self, passlevel, reject):
self.passlevel = passlevel
self.reject = reject

def filter(self, record):
if self.reject:
return (record.levelno != self.passlevel)
else:
return (record.levelno == self.passlevel)


class MyParser(argparse.ArgumentParser):

def error(self, message):
Expand All @@ -19,12 +32,21 @@ def error(self, message):
class CLI(object):

def __init__(self):
handler_out = logging.StreamHandler(sys.stdout)
handler_err = logging.StreamHandler(sys.stderr)

handler_out.addFilter(SingleLevelFilter(logging.INFO, False))
handler_err.addFilter(SingleLevelFilter(logging.INFO, True))

self.log = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
self.log.addHandler(handler)

handler_out.setFormatter(formatter)
handler_err.setFormatter(formatter)

self.log.addHandler(handler_out)
self.log.addHandler(handler_err)

def run(self):
parser = MyParser(
Expand Down

0 comments on commit 8f6282a

Please sign in to comment.