-
Notifications
You must be signed in to change notification settings - Fork 18
/
logger.py
43 lines (33 loc) · 1.4 KB
/
logger.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
"""
ccextractor-web | logger.py
Author : Saurabh Shrivastava
Email : saurabh.shrivastava54+ccextractorweb[at]gmail.com
Link : https://github.com/saurabhshri
"""
import logging
import os
class Logger:
def __init__(self, log_level, dir, filename, format=None, console_level="", file_level=""):
if not console_level:
console_level = log_level
if not file_level:
file_level = log_level
self.log_level = logging.getLevelName(log_level)
self.console_level = logging.getLevelName(console_level)
self.file_level = logging.getLevelName(file_level)
if format is None:
format = '[%(asctime)s] [%(name)s] [%(levelname)s] [%(pathname)s#L%(lineno)d]\t| "%(message)s"'
format = logging.Formatter(format)
self.log_to_console = logging.StreamHandler()
self.log_to_console.setFormatter(format)
self.log_to_console.setLevel(logging.getLevelName(self.console_level))
path = os.path.join(dir, '{name}.log'.format(name=filename))
self.log_to_file = logging.FileHandler(path)
self.log_to_file.setFormatter(format)
self.log_to_file.setLevel(self.file_level)
def get_logger(self, name):
logger = logging.getLogger(name)
logger.setLevel(self.log_level)
logger.addHandler(self.log_to_console)
logger.addHandler(self.log_to_file)
return logger