-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ya-Liang Chang (Allen)
committed
Dec 15, 2018
1 parent
70e03d8
commit 06ab997
Showing
1 changed file
with
40 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,40 @@ | ||
import os | ||
import sys | ||
from time import localtime, strftime | ||
import logging | ||
|
||
# Clean existing handlers | ||
for handler in logging.root.handlers[:]: | ||
logging.root.removeHandler(handler) | ||
|
||
# Create log dir | ||
LOG_DIR = 'logs' | ||
created_time = strftime("%Y%m%d_%H%M%S", localtime()) | ||
if not os.path.exists(LOG_DIR): | ||
os.makedirs(LOG_DIR) | ||
|
||
# Set up handlers | ||
LOGGING_LEVEL = logging.INFO | ||
stream_handler = logging.StreamHandler(sys.stdout) | ||
file_handler = logging.FileHandler(f"{LOG_DIR}/{created_time}.log") | ||
format_ = ('[%(asctime)s] {%(filename)s:%(lineno)d} ' | ||
'%(levelname)s - %(message)s') | ||
|
||
# Try to use colored formatter from coloredlogs | ||
try: | ||
import coloredlogs | ||
formatter = coloredlogs.ColoredFormatter(fmt=format_) | ||
stream_handler.setFormatter(formatter) | ||
except Exception as err: | ||
print(f"{err}") | ||
|
||
handlers = [ | ||
file_handler, | ||
stream_handler | ||
] | ||
logging.basicConfig( | ||
format=format_, | ||
level=LOGGING_LEVEL, | ||
handlers=handlers | ||
) | ||
logger = logging.getLogger(__name__) |