-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_config.py
44 lines (32 loc) · 1.04 KB
/
logger_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-
"""
Filename: logger_config.py
Author: Iliya Vereshchagin
Copyright (c) 2023. All rights reserved.
Created: 25.08.2023
Last Modified: 17.10.2023
Description:
This file contains configuration for loggers.
"""
import logging
import sys
from utils.other import is_heroku_environment
def setup_logger(name: str, log_file: str, level=logging.DEBUG):
"""
Method to setup logger
:param name: (string) Name of the logger.
:param log_file: path to log_file
:param level: logging level. Default is logging.DEBUG
:returns: logger object
"""
formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(name)s: %(message)s")
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
if not is_heroku_environment():
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
return logger