forked from KeplerGO/pyke
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a first pass at issue KeplerGO#88, converting pyke to more Pythonic logging.
- Loading branch information
1 parent
6a3fcc0
commit 3112719
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |