-
Notifications
You must be signed in to change notification settings - Fork 69
/
simple-logger.ts
112 lines (101 loc) · 3.16 KB
/
simple-logger.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// tslint:disable:no-console
import { Log as LogTypes } from '@requestnetwork/types';
// The default log level to use if none is used at the constructor.
const DEFAULT_LOG_LEVEL = LogTypes.LogLevel.INFO;
/**
* Simple logger that outputs content to the console.
*/
export default class SimpleLogger implements LogTypes.ILogger {
/**
* maxLogLevel, the maximum log level to display
*/
public maxLogLevel: LogTypes.LogLevel;
/**
* The output console to use for logging
*/
public output = console;
/**
* Creates an instance of SimpleLogger
*
* @param [maxLogLevel=DEFAULT_LOG_LEVEL] The maximum log level for this logger
*/
constructor(maxLogLevel: LogTypes.LogLevel = DEFAULT_LOG_LEVEL) {
this.maxLogLevel = maxLogLevel;
}
/**
* Logs an error message
*
* @param message The error message to log
* @param [tags] The array of tags concerning this message
*/
public error(message: string, tags?: string[]): void {
if (this.maxLogLevel >= LogTypes.LogLevel.ERROR) {
this.output.error(this.formatLog(LogTypes.LogLevel.ERROR, message, tags));
}
}
/**
* Logs a warning message
*
* @param message The warning message to log
* @param [tags] The array of tags concerning this message
*/
public warn(message: string, tags?: string[]): void {
if (this.maxLogLevel >= LogTypes.LogLevel.WARN) {
this.output.warn(this.formatLog(LogTypes.LogLevel.WARN, message, tags));
}
}
/**
* Logs an info message
*
* @param message The info message to log
* @param [tags] The array of tags concerning this message
*/
public info(message: string, tags?: string[]): void {
if (this.maxLogLevel >= LogTypes.LogLevel.INFO) {
this.output.info(this.formatLog(LogTypes.LogLevel.INFO, message, tags));
}
}
/**
* Logs a debug message
*
* @param message The debug message to log
* @param [tags] The array of tags concerning this message
*/
public debug(message: string, tags?: string[]): void {
if (this.maxLogLevel >= LogTypes.LogLevel.DEBUG) {
this.output.debug(this.formatLog(LogTypes.LogLevel.DEBUG, message, tags));
}
}
/**
* Formats the message on the log format
* The format is: ISO date|level|message|tag1,tag2,...,tagM
*
* @private
* @param level The log level of the message
* @param message The log message
* @param [tags] The log tags
* @returns A string with the formatted log message
*/
private formatLog(level: LogTypes.LogLevel, message: string, tags?: string[]): string {
const now = new Date();
let log = `${now.toISOString()}|${LogTypes.LogLevel[level]}|${message}`;
if (tags && tags.length) {
this.checkForSeparator(tags, '|');
const tagList = tags.join(',');
log += `|${tagList}`;
}
return log;
}
/**
* Throws if tags contain the log separator character
*
* @private
* @param tags The list of tags to check
* @param separator The log separator character
*/
private checkForSeparator(tags: string[], separator: string): void {
if (tags.some(tag => tag.includes(separator))) {
throw new Error(`Log tags can't can't contain ${separator} character`);
}
}
}