UNPKG

3.31 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2018, OpenCensus Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.logger = exports.ConsoleLogger = void 0;
19const util = require("util");
20const logDriver = require('log-driver');
21/**
22 * This class implements a console logger.
23 */
24class ConsoleLogger {
25 /**
26 * Constructs a new ConsoleLogger instance
27 * @param options A logger configuration object.
28 */
29 constructor(options) {
30 let opt = {};
31 if (typeof options === 'number') {
32 if (options < 0) {
33 options = 0;
34 }
35 else if (options > ConsoleLogger.LEVELS.length) {
36 options = ConsoleLogger.LEVELS.length - 1;
37 }
38 opt = { level: ConsoleLogger.LEVELS[options] };
39 }
40 else if (typeof options === 'string') {
41 opt = { level: options };
42 }
43 else {
44 opt = options || {};
45 }
46 if (opt.level)
47 this.level = opt.level;
48 this.logger = logDriver({
49 levels: ConsoleLogger.LEVELS,
50 level: opt.level || 'silent',
51 });
52 }
53 /**
54 * Logger error function.
55 * @param message message error to log in console
56 * @param args arguments to log in console
57 */
58 // tslint:disable-next-line:no-any
59 error(message, ...args) {
60 this.logger.error(util.format(message, ...args));
61 }
62 /**
63 * Logger warning function.
64 * @param message message warning to log in console
65 * @param args arguments to log in console
66 */
67 // tslint:disable-next-line:no-any
68 warn(message, ...args) {
69 this.logger.warn(util.format(message, ...args));
70 }
71 /**
72 * Logger info function.
73 * @param message message info to log in console
74 * @param args arguments to log in console
75 */
76 // tslint:disable-next-line:no-any
77 info(message, ...args) {
78 this.logger.info(util.format(message, ...args));
79 }
80 /**
81 * Logger debug function.
82 * @param message message debug to log in console
83 * @param args arguments to log in console
84 */
85 // tslint:disable-next-line:no-any
86 debug(message, ...args) {
87 this.logger.debug(util.format(message, ...args));
88 }
89}
90exports.ConsoleLogger = ConsoleLogger;
91ConsoleLogger.LEVELS = ['silent', 'error', 'warn', 'info', 'debug'];
92/**
93 * Function logger exported to others classes. Inspired by:
94 * https://github.com/cainus/logdriver/blob/bba1761737ca72f04d6b445629848538d038484a/index.js#L50
95 * @param options A logger options or strig to logger in console
96 */
97const logger = (options) => {
98 return new ConsoleLogger(options);
99};
100exports.logger = logger;
101//# sourceMappingURL=console-logger.js.map
\No newline at end of file