| 3 | import colorlog |
| 4 | |
| 5 | def init_logger(dunder_name, show_debug=False) -> logging.Logger: |
| 6 | log_format = ( |
| 7 | '%(asctime)s - ' |
| 8 | '%(name)s - ' |
| 9 | '%(funcName)s - ' |
| 10 | '%(levelname)s - ' |
| 11 | '%(message)s' |
| 12 | ) |
| 13 | bold_seq = '\033[1m' |
| 14 | colorlog_format = ( |
| 15 | f'{bold_seq} ' |
| 16 | '%(log_color)s ' |
| 17 | f'{log_format}' |
| 18 | ) |
| 19 | colorlog.basicConfig(format=colorlog_format) |
| 20 | logging.getLogger('tensorflow').disabled = True |
| 21 | logger = logging.getLogger(dunder_name) |
| 22 | |
| 23 | if show_debug: |
| 24 | logger.setLevel(logging.DEBUG) |
| 25 | else: |
| 26 | logger.setLevel(logging.INFO) |
| 27 | |
| 28 | # Note: these file outputs are left in place as examples |
| 29 | # Feel free to uncomment and use the outputs as you like |
| 30 | |
| 31 | # Output full log |
| 32 | # fh = logging.FileHandler(os.path.join('data', log', 'trading.log') |
| 33 | # fh.setLevel(logging.DEBUG) |
| 34 | # formatter = logging.Formatter(log_format) |
| 35 | # fh.setFormatter(formatter) |
| 36 | # logger.addHandler(fh) |
| 37 | |
| 38 | # # Output warning log |
| 39 | # fh = logging.FileHandler(os.path.join('data', log', 'trading.warning.log') |
| 40 | # fh.setLevel(logging.WARNING) |
| 41 | # formatter = logging.Formatter(log_format) |
| 42 | # fh.setFormatter(formatter) |
| 43 | # logger.addHandler(fh) |
| 44 | |
| 45 | # # Output error log |
| 46 | # fh = logging.FileHandler(os.path.join('data', log', 'trading.error.log') |
| 47 | # fh.setLevel(logging.ERROR) |
| 48 | # formatter = logging.Formatter(log_format) |
| 49 | # fh.setFormatter(formatter) |
| 50 | # logger.addHandler(fh) |
| 51 | |
| 52 | return logger |