-
Notifications
You must be signed in to change notification settings - Fork 1
/
chain_of_responsibility_pattern.py
60 lines (42 loc) · 1.64 KB
/
chain_of_responsibility_pattern.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class AbstractLogger:
INFO = 1
DEBUG = 2
ERROR = 3
def __init__(self):
self.next_logger = None
self.lever = 0
def set_next_logger(self, next_logger: 'AbstractLogger'):
self.next_logger = next_logger
def log_message(self, level: 'int', message: 'str'):
if self.lever <= level:
self.write(message)
if self.next_logger is not None:
self.next_logger.log_message(level, message)
def write(self, message: 'str'):
pass
class ConsoleLogger(AbstractLogger):
def __init__(self, level: 'int'):
AbstractLogger.__init__(self)
self.lever = level
def write(self, message: 'str'):
print("standard console::logger: " + message)
class ErrorLogger(AbstractLogger):
def __init__(self, level: 'int'):
AbstractLogger.__init__(self)
self.lever = level
def write(self, message: 'str'):
print("error console::logger: " + message)
class FileLogger(AbstractLogger):
def __init__(self, level: 'int'):
AbstractLogger.__init__(self)
self.lever = level
def write(self, message: 'str'):
print("file console::logger: " + message)
error_logger = ErrorLogger(AbstractLogger.ERROR)
file_logger = FileLogger(AbstractLogger.DEBUG)
console_logger = ConsoleLogger(AbstractLogger.INFO)
error_logger.set_next_logger(file_logger)
file_logger.set_next_logger(console_logger)
error_logger.log_message(AbstractLogger.INFO, "this is an information.")
error_logger.log_message(AbstractLogger.DEBUG, "this is an debug information.")
error_logger.log_message(AbstractLogger.ERROR, "this is an error information.")