Skip to content

Commit

Permalink
[88] Add keplog.py
Browse files Browse the repository at this point in the history
This is a first pass at issue KeplerGO#88, converting pyke to more Pythonic
logging.
  • Loading branch information
ecalifornica committed Feb 18, 2018
1 parent 6a3fcc0 commit 3112719
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions pyke/keplog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import logging

_logger = logging.getLogger('pyke')
_logger.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s %(levelname)s - %(message)s',
'%Y-%m-%d %H:%M:%S')

console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)

_logger.addHandler(console_handler)


def debug(*args, **kwargs):
_logger.debug(*args, **kwargs)


def info(*args, **kwargs):
_logger.info(*args, **kwargs)


def warn(*args, **kwargs):
_logger.warn(*args, **kwargs)


def error(*args, **kwargs):
_logger.error(*args, **kwargs)


def exception(*args, **kwargs):
_logger.exception(*args, **kwargs)


def set_level(level):
try:
_logger.setLevel(getattr(logging, level))
except TypeError:
_logger.setLevel(level)


def set_logfile(log_filepath):
fh = logging.FileHandler(log_filepath)
fh.setFormatter(formatter)
_logger.addHandler(fh)


def setup_args(parser):
g = parser.add_mutually_exclusive_group()
g.add_argument('--silent',
action='store_true',
help='Set log level to ERROR or above')
g.add_argument('--debug',
action='store_true',
help='Set log level to DEBUG or above')


def handle_args(args):
if args.silent:
_logger.setLevel(logging.ERROR)
if args.debug:
_logger.setLevel(logging.DEBUG)

if args.logfile is not None:
set_logfile(args.logfile)

0 comments on commit 3112719

Please sign in to comment.