UNPKG

2.19 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.LogLevel = void 0;
4const MethodFactory_1 = require("./MethodFactory");
5const PrefixFactory_1 = require("./PrefixFactory");
6const defaults = {
7 factory: void 0,
8 level: 'warn',
9 name: (+new Date()).toString(),
10 prefix: void 0
11};
12class LogLevel {
13 constructor(options) {
14 this.type = 'LogLevel';
15 this.options = Object.assign({}, defaults, options);
16 this.methodFactory = options.factory;
17 if (!this.methodFactory) {
18 const factory = options.prefix
19 ? new PrefixFactory_1.PrefixFactory(this, options.prefix)
20 : new MethodFactory_1.MethodFactory(this);
21 this.methodFactory = factory;
22 }
23 if (!this.methodFactory.logger) {
24 this.methodFactory.logger = this;
25 }
26 this.name = options.name || '<unknown>';
27 this.level = this.options.level ?? 'trace';
28 }
29 get factory() {
30 return this.methodFactory;
31 }
32 set factory(factory) {
33 factory.logger = this;
34 this.methodFactory = factory;
35 this.methodFactory.replaceMethods(this.level);
36 }
37 disable() {
38 this.level = this.levels.SILENT;
39 }
40 enable() {
41 this.level = this.levels.TRACE;
42 }
43 get level() {
44 return this.currentLevel;
45 }
46 set level(logLevel) {
47 const level = this.methodFactory.distillLevel(logLevel);
48 if (level === false || level == null) {
49 throw new RangeError(`loglevelnext: setLevel() called with invalid level: ${logLevel}`);
50 }
51 this.currentLevel = level;
52 this.methodFactory.replaceMethods(level);
53 if (typeof console === 'undefined') {
54 process.stdout.write('loglevelnext: console is undefined. The log will produce no output.\n');
55 }
56 else if (level < this.levels.SILENT) {
57 console.warn(`The log level has been set to a value less than 'silent'. The log will produce no output.`);
58 }
59 }
60 get levels() {
61 return this.methodFactory.levels;
62 }
63}
64exports.LogLevel = LogLevel;
65//# sourceMappingURL=LogLevel.js.map
\No newline at end of file