Commit f85f0f2e authored by Martin Matějek's avatar Martin Matějek

Remove logging to file, use stderr/out instead

parent 3e4cc661
......@@ -25,6 +25,9 @@ COLORS = {
'default': '\033[39m',
}
# TODO: get these levels programatically somehow?
LOG_LEVELS = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
root_logger = logging.getLogger()
logger = logging.getLogger('cliapp')
......@@ -33,7 +36,7 @@ def create_argparser():
"""Create new argument parser"""
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", help="Specify config file")
parser.add_argument("-l", "--logfile", help="Specify log file", nargs="?", default="notifylib-cli.log")
parser.add_argument("--log-level", help="Specify log level", nargs="?", choices=LOG_LEVELS, default="INFO")
subparsers = parser.add_subparsers(help="sub-command help", dest='command')
subparsers.required = True
......@@ -97,22 +100,20 @@ def print_notification(notification):
# work-in-progress!
def setup_logging(logfile):
root_logger.setLevel(logging.DEBUG)
def setup_logging(loglevel):
root_logger.setLevel(loglevel)
# TODO: use parametric logfile
fh = logging.FileHandler(logfile)
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(message)s')
fh_formatter = logging.Formatter('%(asctime)s %(filename)s:%(lineno)s - %(levelname)s - %(funcName)s - %(message)s')
fh.setFormatter(fh_formatter)
handler_lib_stderr = logging.StreamHandler(sys.stderr)
handler_lib_stderr.setFormatter(formatter)
# TODO: use loglevel here?
handler_lib_stderr.setLevel(logging.WARNING)
root_logger.addHandler(fh)
root_logger.addHandler(handler_lib_stderr)
# CLI app logger
# Level Info and below go to stdout, others go to stderr
formatter = logging.Formatter('%(message)s')
handler_stdout = logging.StreamHandler(sys.stdout)
handler_stdout.setFormatter(formatter)
handler_stdout.setLevel(logging.INFO)
......@@ -127,7 +128,7 @@ def setup_logging(logfile):
def process_args(parser, args):
"""Call module interface based on args"""
setup_logging(args.logfile)
setup_logging(args.log_level)
if args.config:
api = Api(os.path.abspath(args.config))
......@@ -156,7 +157,7 @@ def process_args(parser, args):
if ret:
logger.info("Succesfully created notification '%s'", format(ret))
else:
logger.error("Failed to create notification. Please see the log for more details.")
logger.error("Failed to create notification.")
sys.exit(1)
elif args.command == 'list':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment