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
import logging | |
import os | |
LOGGER_NAME = 'my_logger' | |
def init_logger(log_level_for_console: str = 'info', log_level_for_file: str = 'debug', | |
save_dir: str = None): | |
logger = logging.getLogger(LOGGER_NAME) | |
logger.setLevel(level=logging.DEBUG) | |
logger.propagate = False |
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
from logging_utils import init_logger | |
init_logger(save_dir='/path/to/somewhere') |
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
from logging_utils import get_logger | |
logger = get_logger() | |
logger.info("Let's start!") | |
>>> 2020-04-12 14:49:47 [INFO] my_module.py 17 - Let's start! |
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
from logging_utils import get_logger | |
logger = get_logger() | |
logger.info("TEST") | |
>>> | |
logger.warning("TEST") | |
>>> TEST |
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
from logging_utils import get_logger, init_logger | |
logger = get_logger() | |
logger.info("TEST") | |
>>> | |
logger.warning("TEST") | |
>>> TEST | |
init_logger() |
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
import os | |
from datetime import datetime | |
start_time_str = datetime.today().strftime('%d_%m_%Y__%H_%M_%S') | |
log_dir = f"log_{start_time_str}" | |
os.mkdir(log_dir) | |
# This will generate a folder named something like log_14_04_2020__19_46_06 |
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
import os | |
import json | |
# Here we assume: params = MyParamsClass() | |
sorted_params_dict = {k: params.__dict__[k] for k | |
in sorted(params.__dict__.keys())} | |
params_filepath = os.path.join(log_dir, 'params.json') | |
json.dump(sorted_params_dict, open(params_filepath, 'w'), indent=4) |
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
import os | |
from time import time | |
class RuntimeLogger: | |
def __init__(self, log_dir: str): | |
self.runtime_filepath = os.path.join(log_dir, "runtime.json") | |
self.runtime_dict = {} | |
self.global_start_time = None | |
def start(self): |
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
from time import time | |
import RuntimeLogger | |
class MyFancyAlgorithm: | |
def __init__(self, log_dir: str): | |
self.runtime_logger = RuntimeLogger(log_dir) | |
def run(self): | |
self.runtime_logger.start() | |
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
path/to/project$ git rev-parse --abbrev-ref HEAD | |
my_branch | |
path/to/project$ git describe --always | |
ed7a91b | |
path/to/project$ git diff | |
<...> |
OlderNewer