UNPKG

1.79 kBJavaScriptView Raw
1'use strict';
2
3const pino = require('pino');
4
5class KellnerLoggerManager {
6 constructor({
7 name,
8 version,
9 logLevel,
10 logEnabled,
11 instant,
12 env,
13 prettyPrint,
14 }) {
15 this.name = name;
16 this.version = version;
17 this.logLevel = logLevel;
18 this.logEnabled = logEnabled;
19 this.prettyPrint = prettyPrint;
20 this.env = env;
21 this._instance = instant ? this._getInstance() : null;
22 }
23
24 getLogger() {
25 if (!this._instance) {
26 const pinoOptions = {
27 name: `${(this.name)} (${this.version})`,
28 level: this.logLevel,
29 enabled: this.logEnabled,
30 };
31 if (this.prettyPrint)
32 pinoOptions.prettyPrint = this.prettyPrint;
33
34 this._instance = pino(pinoOptions);
35 this._instance.fatal('fatal logging is [ON]');
36 this._instance.error('error logging is [ON]');
37 this._instance.warn('warn logging is [ON]');
38 this._instance.info('info logging is [ON]');
39 this._instance.debug('debug logging is [ON]');
40 this._instance.trace('trace logging is [ON]');
41 }
42
43 return this._instance;
44 }
45
46 getManager({
47 name,
48 version = this.version,
49 logLevel = this.logLevel,
50 logEnabled = this.logEnabled,
51 prettyPrint = this.prettyPrint,
52 env = this.env,
53 instant,
54 }) {
55 if (!name)
56 throw new Error('Logger muste have a name');
57
58 return new KellnerLoggerManager({
59 name: `${this.name}:${name}`,
60 version,
61 logLevel,
62 logEnabled,
63 instant,
64 prettyPrint,
65 env,
66 });
67 }
68
69 static getManager({
70 name,
71 version = '0.0.0',
72 logLevel = 'debug',
73 logEnabled = true,
74 instant = false,
75 prettyPrint = false,
76 env = 'debug',
77 }) {
78 if (!name)
79 throw new Error('Logger muste have a name');
80 return new KellnerLoggerManager({
81 name,
82 version,
83 logLevel,
84 logEnabled,
85 instant,
86 prettyPrint,
87 env,
88 });
89 }
90}
91
92module.exports = KellnerLoggerManager;