1
/*
2
 * Copyright 2017-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5
 * the License. A copy of the License is located at
6
 *
7
 *     http://aws.amazon.com/apache2.0/
8
 *
9
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
 * and limitations under the License.
12
 */
13

14
import { Logger } from './logger-interface';
15

16 1
const LOG_LEVELS = {
17
	VERBOSE: 1,
18
	DEBUG: 2,
19
	INFO: 3,
20
	WARN: 4,
21
	ERROR: 5,
22
};
23

24
/**
25
 * Write logs
26
 * @class Logger
27
 */
28 1
export class ConsoleLogger implements Logger {
29
	name: string;
30
	level: string;
31

32
	/**
33
	 * @constructor
34
	 * @param {string} name - Name of the logger
35
	 */
36 1
	constructor(name, level = 'WARN') {
37 1
		this.name = name;
38 1
		this.level = level;
39
	}
40

41 1
	static LOG_LEVEL = null;
42

43 1
	_padding(n) {
44 1
		return n < 10 ? '0' + n : '' + n;
45
	}
46

47 1
	_ts() {
48 1
		const dt = new Date();
49 1
		return (
50
			[this._padding(dt.getMinutes()), this._padding(dt.getSeconds())].join(
51
				':'
52
			) +
53
			'.' +
54
			dt.getMilliseconds()
55
		);
56
	}
57

58
	/**
59
	 * Write log
60
	 * @method
61
	 * @memeberof Logger
62
	 * @param {string} type - log type, default INFO
63
	 * @param {string|object} msg - Logging message or object
64
	 */
65 1
	_log(type: string, ...msg) {
66 1
		let logger_level_name = this.level;
67 1
		if (ConsoleLogger.LOG_LEVEL) {
68 1
			logger_level_name = ConsoleLogger.LOG_LEVEL;
69
		}
70 1
		if (typeof (<any>window) !== 'undefined' && (<any>window).LOG_LEVEL) {
71 0
			logger_level_name = (<any>window).LOG_LEVEL;
72
		}
73 1
		const logger_level = LOG_LEVELS[logger_level_name];
74 1
		const type_level = LOG_LEVELS[type];
75 1
		if (!(type_level >= logger_level)) {
76
			// Do nothing if type is not greater than or equal to logger level (handle undefined)
77 1
			return;
78
		}
79

80 1
		let log = console.log.bind(console);
81 1
		if (type === 'ERROR' && console.error) {
82 0
			log = console.error.bind(console);
83
		}
84 1
		if (type === 'WARN' && console.warn) {
85 1
			log = console.warn.bind(console);
86
		}
87

88 1
		const prefix = `[${type}] ${this._ts()} ${this.name}`;
89

90 1
		if (msg.length === 1 && typeof msg[0] === 'string') {
91 1
			log(`${prefix} - ${msg[0]}`);
92 1
		} else if (msg.length === 1) {
93 0
			log(prefix, msg[0]);
94 1
		} else if (typeof msg[0] === 'string') {
95 1
			let obj = msg.slice(1);
96 1
			if (obj.length === 1) {
97 1
				obj = obj[0];
98
			}
99 1
			log(`${prefix} - ${msg[0]}`, obj);
100
		} else {
101 0
			log(prefix, msg);
102
		}
103
	}
104

105
	/**
106
	 * Write General log. Default to INFO
107
	 * @method
108
	 * @memeberof Logger
109
	 * @param {string|object} msg - Logging message or object
110
	 */
111 1
	log(...msg) {
112 0
		this._log('INFO', ...msg);
113
	}
114

115
	/**
116
	 * Write INFO log
117
	 * @method
118
	 * @memeberof Logger
119
	 * @param {string|object} msg - Logging message or object
120
	 */
121 1
	info(...msg) {
122 0
		this._log('INFO', ...msg);
123
	}
124

125
	/**
126
	 * Write WARN log
127
	 * @method
128
	 * @memeberof Logger
129
	 * @param {string|object} msg - Logging message or object
130
	 */
131 1
	warn(...msg) {
132 1
		this._log('WARN', ...msg);
133
	}
134

135
	/**
136
	 * Write ERROR log
137
	 * @method
138
	 * @memeberof Logger
139
	 * @param {string|object} msg - Logging message or object
140
	 */
141 1
	error(...msg) {
142 0
		this._log('ERROR', ...msg);
143
	}
144

145
	/**
146
	 * Write DEBUG log
147
	 * @method
148
	 * @memeberof Logger
149
	 * @param {string|object} msg - Logging message or object
150
	 */
151 1
	debug(...msg) {
152 1
		this._log('DEBUG', ...msg);
153
	}
154

155
	/**
156
	 * Write VERBOSE log
157
	 * @method
158
	 * @memeberof Logger
159
	 * @param {string|object} msg - Logging message or object
160
	 */
161 1
	verbose(...msg) {
162 0
		this._log('VERBOSE', ...msg);
163
	}
164 1
}

Read our documentation on viewing source code .

Loading