-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChainOfResponsibility.ts
More file actions
85 lines (68 loc) · 1.69 KB
/
Copy pathChainOfResponsibility.ts
File metadata and controls
85 lines (68 loc) · 1.69 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
enum LogLevel {
None = 0,
Info,
Debug,
Warning,
Error,
FunctionalMessage,
FunctionalError,
All
}
abstract class Logger {
protected logMask: LogLevel;
/**
* Next logger in chain.
*/
protected next: Logger;
constructor(mask: LogLevel) {
this.logMask = mask;
}
/**
*
* @param nextLogger Next logger in the chain of loggers.
*/
setNext(nextLogger: Logger): Logger {
let lastLogger: Logger = this;
while (lastLogger.next != null) {
lastLogger = lastLogger.next;
}
lastLogger.next = nextLogger;
return this;
}
message(msg: string, severity: LogLevel): void {
if ((severity & this.logMask) != 0) {
this.writeMessage(msg);
}
if (this.next != null) {
this.next.message(msg, severity);
}
}
protected abstract writeMessage(msg: string): void;
}
class ConsoleLogger extends Logger {
constructor(mask: LogLevel) {
super(mask);
}
protected writeMessage(msg: string): void {
console.log('Writing to console: ' + msg);
}
}
class EmailLogger extends Logger {
constructor(mask: LogLevel) {
super(mask);
}
protected writeMessage(msg: string): void {
console.log('Writing to email: ' + msg);
}
}
class FileLogger extends Logger {
constructor(mask: LogLevel) {
super(mask);
}
protected writeMessage(msg: string): void {
console.log('Writing to file: ' + msg);
}
}
let logger = new ConsoleLogger(LogLevel.All);
logger.setNext(new EmailLogger(LogLevel.FunctionalMessage));
logger.setNext(new FileLogger(LogLevel.Warning))