Skip to content
This repository was archived by the owner on Nov 30, 2019. It is now read-only.

Commit c302b42

Browse files
committed
feat(Logger): Added Loglevel and Logger
The logger provides several methods for logging messages You can log with the following log levels: - INFO - DEBUG - WARN - ERROR - DUMP Notice: You can dump objects which will be stringified to JSON You can also provide a message which will be displayed before the stringified object. The "error" method also provides an extra Argument with which you can log errors with a simple message
1 parent 78c9c68 commit c302b42

File tree

2 files changed

+242
-0
lines changed

2 files changed

+242
-0
lines changed

src/logger/LogLevel.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* An enumeration of log levels
3+
*
4+
* @export
5+
* @enum {number}
6+
* @since 0.0.1
7+
* @version 0.0.1
8+
* @author Yannick Fricke <yannickfricke@googlemail.com>
9+
* @license MIT
10+
* @copyright MedjaiBot https://github.com/MedjaiBot/server
11+
*/
12+
export enum LogLevel {
13+
/**
14+
* The info log level
15+
*/
16+
INFO = 1 << 0,
17+
18+
/**
19+
* The debug log level. Will be checked with the dump log level
20+
*/
21+
DEBUG = 1 << 1,
22+
23+
/**
24+
* The warn log level
25+
*/
26+
WARN = 1 << 2,
27+
28+
/**
29+
* The error log level
30+
*/
31+
ERROR = 1 << 3,
32+
33+
/**
34+
* The dump log level. Will be checked with the debug log level
35+
*/
36+
DUMP = 1 << 4,
37+
38+
/**
39+
* All loglevels togeather
40+
*/
41+
ALL = INFO | DEBUG | WARN | ERROR | DUMP,
42+
}

src/logger/Logger.ts

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
// tslint:disable:no-bitwise
2+
3+
import chalk from 'chalk';
4+
import { inject, injectable } from 'inversify';
5+
import { EOL } from 'os';
6+
import { LogLevel } from './LogLevel';
7+
8+
/**
9+
* A basic logger
10+
*
11+
* @export
12+
* @class Logger
13+
* @since 0.0.1
14+
* @version 0.0.1
15+
* @author Yannick Fricke <yannickfricke@googlemail.com>
16+
* @license MIT
17+
* @copyright MedjaiBot https://github.com/MedjaiBot/server
18+
*/
19+
@injectable()
20+
export class Logger {
21+
22+
public logLevel: number;
23+
/**
24+
* The stream where to write normal messages
25+
*
26+
* @private
27+
* @type {NodeJS.WriteStream}
28+
* @memberof Logger
29+
*/
30+
private outputStream: NodeJS.WriteStream;
31+
32+
/**
33+
* The stream where to write error messages
34+
*
35+
* @private
36+
* @type {NodeJS.WriteStream}
37+
* @memberof Logger
38+
*/
39+
private errorStream: NodeJS.WriteStream;
40+
41+
/**
42+
* Constructs a new Logger
43+
* @param outputStream The stream where to log normal messages to
44+
* @param errorStream The stream where to log error messages to
45+
* @param logLevel The log level of this logger. Default is info.
46+
*/
47+
constructor(
48+
@inject(Symbol.for('Logger_output_stream'))
49+
outputStream = process.stdout,
50+
@inject(Symbol.for('Logger_error_stream'))
51+
errorStream = process.stderr,
52+
@inject(Symbol.for('logger.loglevel'))
53+
logLevel = LogLevel.INFO,
54+
) {
55+
this.outputStream = outputStream;
56+
this.errorStream = errorStream;
57+
this.logLevel = logLevel;
58+
}
59+
60+
/**
61+
* Logs the given message with the INFO level
62+
*
63+
* @param message The message to log
64+
*/
65+
public info(message: string) {
66+
this.log(
67+
this.outputStream,
68+
LogLevel.INFO,
69+
message,
70+
);
71+
}
72+
73+
/**
74+
* Logs the given message with the DEBUG level
75+
* @param message The message to log
76+
*/
77+
public debug(message: string) {
78+
this.log(
79+
this.outputStream,
80+
LogLevel.DEBUG,
81+
message,
82+
);
83+
}
84+
85+
/**
86+
* Logs the given message with the WARN level
87+
*
88+
* @param {string} message The message to log
89+
* @param {string} error The error to log
90+
* @memberof Logger
91+
*/
92+
public warn(message: string) {
93+
this.log(
94+
this.outputStream,
95+
LogLevel.WARN,
96+
message,
97+
);
98+
}
99+
100+
/**
101+
* Logs the given message with the ERROR level
102+
*
103+
* @param {string} message The message to log
104+
* @param {string} error The error to log
105+
* @memberof Logger
106+
*/
107+
public error(message: string, error?: string) {
108+
this.log(
109+
this.errorStream,
110+
LogLevel.ERROR,
111+
error === undefined ? message : `${message} ${error}`,
112+
);
113+
}
114+
115+
/**
116+
* Dumps an object to the stream with the DUMP level
117+
* @param description The description for the object
118+
* @param obj The object to log
119+
*/
120+
public dumpObject(description: string, obj: object) {
121+
this.log(
122+
this.outputStream,
123+
LogLevel.DUMP,
124+
`${description}: ${JSON.stringify(obj, undefined, 4)}`,
125+
);
126+
}
127+
128+
/**
129+
* Prefixes a number with a zero when the value is less than ten
130+
*
131+
* @public
132+
* @param {number} number The number to format
133+
* @returns The formatted number
134+
* @memberof Logger
135+
*/
136+
public formatNumber(number: number, prefix: string = '0', length: number = 2) {
137+
const stringLength = number.toString().length;
138+
139+
if (stringLength === length) {
140+
return number.toString();
141+
}
142+
143+
if (stringLength > length) {
144+
return number.toString().substr(0, length);
145+
}
146+
147+
return number.toString().padStart(length, prefix);
148+
}
149+
150+
/**
151+
* Logs a message to the given stream with the given level
152+
*
153+
* @private
154+
* @param {NodeJS.WriteStream} stream The stream to write to
155+
* @param {string} level The log level of the message
156+
* @param {string} message The message to log
157+
* @memberof Logger
158+
*/
159+
private log(stream: NodeJS.WriteStream, level: number, message: string) {
160+
// The log level as a (colored) string
161+
let logLevel;
162+
163+
if ((this.logLevel & LogLevel.INFO) === level) {
164+
logLevel = chalk.supportsColor ? chalk.blue('INFO') : 'INFO';
165+
}
166+
if ((this.logLevel & LogLevel.DEBUG) === level) {
167+
logLevel = chalk.supportsColor ? chalk.cyanBright('DEBUG') : 'DEBUG';
168+
}
169+
if ((this.logLevel & LogLevel.WARN) === level) {
170+
logLevel = chalk.supportsColor ? chalk.yellow('WARN') : 'WARN';
171+
}
172+
if ((this.logLevel & LogLevel.ERROR) === level) {
173+
logLevel = chalk.supportsColor ? chalk.redBright('ERROR') : 'ERROR';
174+
}
175+
if ((this.logLevel & LogLevel.DUMP) === level) {
176+
logLevel = chalk.supportsColor ? chalk.cyanBright('DUMP') : 'DUMP';
177+
}
178+
179+
// Sets the timestamp variable to the current time and date
180+
const timestamp = new Date();
181+
182+
// The formatted date joined by a point
183+
const formattedDate = [
184+
this.formatNumber(timestamp.getDate()),
185+
this.formatNumber(timestamp.getMonth() + 1),
186+
timestamp.getFullYear(),
187+
].join('.');
188+
189+
// The formatted time joined by a semicolon
190+
const formattedTime = [
191+
this.formatNumber(timestamp.getHours()),
192+
this.formatNumber(timestamp.getMinutes()),
193+
this.formatNumber(timestamp.getSeconds()),
194+
this.formatNumber(timestamp.getMilliseconds(), '0', 3),
195+
].join(':');
196+
197+
// Writes the log messages to the given stream
198+
stream.write(`[${formattedDate} ${formattedTime}] [${logLevel}] ${message}${EOL}`);
199+
}
200+
}

0 commit comments

Comments
 (0)