@athenna/logger 1.0.1 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/Drivers/ConsoleDriver.d.ts +2 -1
- package/src/Drivers/ConsoleDriver.js +8 -7
- package/src/Drivers/DebugDriver.d.ts +1 -1
- package/src/Drivers/DebugDriver.js +7 -6
- package/src/Drivers/FileDriver.d.ts +1 -1
- package/src/Drivers/FileDriver.js +13 -8
- package/src/Formatters/ContextFormatter.js +1 -1
- package/src/Formatters/DebugFormatter.js +1 -1
- package/src/Formatters/JsonFormatter.js +1 -1
- package/src/Logger.js +12 -12
package/package.json
CHANGED
|
@@ -12,6 +12,7 @@ export interface ConsoleDriverOpts {
|
|
|
12
12
|
color: Color;
|
|
13
13
|
level: string;
|
|
14
14
|
context: string;
|
|
15
|
+
formatter: string;
|
|
15
16
|
streamType: string;
|
|
16
17
|
}
|
|
17
18
|
export declare class ConsoleDriver implements DriverContract {
|
|
@@ -19,6 +20,6 @@ export declare class ConsoleDriver implements DriverContract {
|
|
|
19
20
|
private readonly _context;
|
|
20
21
|
private readonly _formatter;
|
|
21
22
|
private readonly _streamType;
|
|
22
|
-
constructor(channel: string);
|
|
23
|
+
constructor(channel: string, configs?: any);
|
|
23
24
|
transport(message: string, options?: ConsoleDriverOpts): void;
|
|
24
25
|
}
|
|
@@ -12,21 +12,22 @@ exports.ConsoleDriver = void 0;
|
|
|
12
12
|
const utils_1 = require("@secjs/utils");
|
|
13
13
|
const FormatterFactory_1 = require("../Factories/FormatterFactory");
|
|
14
14
|
class ConsoleDriver {
|
|
15
|
-
constructor(channel) {
|
|
15
|
+
constructor(channel, configs = {}) {
|
|
16
16
|
const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
|
|
17
|
-
this._level =
|
|
18
|
-
this._context =
|
|
19
|
-
this._formatter =
|
|
20
|
-
this._streamType =
|
|
17
|
+
this._level = configs.level || channelConfig.level;
|
|
18
|
+
this._context = configs.context || channelConfig.context;
|
|
19
|
+
this._formatter = configs.formatter || channelConfig.formatter;
|
|
20
|
+
this._streamType = configs.streamType || channelConfig.streamType;
|
|
21
21
|
}
|
|
22
22
|
transport(message, options) {
|
|
23
23
|
options = Object.assign({}, {
|
|
24
24
|
level: this._level,
|
|
25
25
|
context: this._context,
|
|
26
|
+
formatter: this._formatter,
|
|
26
27
|
streamType: this._streamType,
|
|
27
28
|
}, options);
|
|
28
|
-
message = FormatterFactory_1.FormatterFactory.fabricate(
|
|
29
|
-
process[
|
|
29
|
+
message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, options);
|
|
30
|
+
process[options.streamType].write(`${message}\n`);
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
33
|
exports.ConsoleDriver = ConsoleDriver;
|
|
@@ -20,6 +20,6 @@ export declare class DebugDriver implements DriverContract {
|
|
|
20
20
|
private readonly _context;
|
|
21
21
|
private readonly _formatter;
|
|
22
22
|
private readonly _namespace;
|
|
23
|
-
constructor(channel: string);
|
|
23
|
+
constructor(channel: string, configs?: any);
|
|
24
24
|
transport(message: string, options?: DebugDriverOpts): void;
|
|
25
25
|
}
|
|
@@ -13,20 +13,21 @@ const debug_1 = require("debug");
|
|
|
13
13
|
const utils_1 = require("@secjs/utils");
|
|
14
14
|
const FormatterFactory_1 = require("../Factories/FormatterFactory");
|
|
15
15
|
class DebugDriver {
|
|
16
|
-
constructor(channel) {
|
|
16
|
+
constructor(channel, configs = {}) {
|
|
17
17
|
const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
|
|
18
|
-
this._level =
|
|
19
|
-
this._context =
|
|
20
|
-
this._formatter =
|
|
21
|
-
this._namespace =
|
|
18
|
+
this._level = configs.level || channelConfig.level;
|
|
19
|
+
this._context = configs.context || channelConfig.context;
|
|
20
|
+
this._formatter = configs.formatter || channelConfig.formatter;
|
|
21
|
+
this._namespace = configs.namespace || channelConfig.namespace;
|
|
22
22
|
}
|
|
23
23
|
transport(message, options) {
|
|
24
24
|
options = Object.assign({}, {
|
|
25
25
|
level: this._level,
|
|
26
26
|
context: this._context,
|
|
27
|
+
formatter: this._formatter,
|
|
27
28
|
namespace: this._namespace,
|
|
28
29
|
}, options);
|
|
29
|
-
message = FormatterFactory_1.FormatterFactory.fabricate(
|
|
30
|
+
message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, options);
|
|
30
31
|
debug_1.debug(options.namespace)(message);
|
|
31
32
|
}
|
|
32
33
|
}
|
|
@@ -18,6 +18,6 @@ export declare class FileDriver implements DriverContract {
|
|
|
18
18
|
private readonly _context;
|
|
19
19
|
private readonly _filePath;
|
|
20
20
|
private readonly _formatter;
|
|
21
|
-
constructor(channel: string);
|
|
21
|
+
constructor(channel: string, configs?: any);
|
|
22
22
|
transport(message: string, options?: FileDriverOpts): Promise<void>;
|
|
23
23
|
}
|
|
@@ -10,26 +10,31 @@
|
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
11
|
exports.FileDriver = void 0;
|
|
12
12
|
const path_1 = require("path");
|
|
13
|
-
const Color_1 = require("../Utils/Color");
|
|
14
13
|
const utils_1 = require("@secjs/utils");
|
|
14
|
+
const Color_1 = require("../Utils/Color");
|
|
15
15
|
const fs_1 = require("fs");
|
|
16
16
|
const FormatterFactory_1 = require("../Factories/FormatterFactory");
|
|
17
17
|
class FileDriver {
|
|
18
|
-
constructor(channel) {
|
|
18
|
+
constructor(channel, configs = {}) {
|
|
19
19
|
const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
|
|
20
|
-
this._level =
|
|
21
|
-
this._context =
|
|
22
|
-
this._filePath =
|
|
23
|
-
this._formatter =
|
|
20
|
+
this._level = configs.level || channelConfig.level;
|
|
21
|
+
this._context = configs.context || channelConfig.context;
|
|
22
|
+
this._filePath = configs.filePath || channelConfig.filePath;
|
|
23
|
+
this._formatter = configs.formatter || channelConfig.formatter;
|
|
24
24
|
}
|
|
25
25
|
async transport(message, options) {
|
|
26
|
-
options = Object.assign({}, {
|
|
26
|
+
options = Object.assign({}, {
|
|
27
|
+
level: this._level,
|
|
28
|
+
context: this._context,
|
|
29
|
+
filePath: this._filePath,
|
|
30
|
+
formatter: this._formatter,
|
|
31
|
+
}, options);
|
|
27
32
|
const filePath = options.filePath;
|
|
28
33
|
const { dir } = path_1.parse(filePath);
|
|
29
34
|
if (!fs_1.existsSync(dir)) {
|
|
30
35
|
fs_1.mkdirSync(dir, { recursive: true });
|
|
31
36
|
}
|
|
32
|
-
message = FormatterFactory_1.FormatterFactory.fabricate(
|
|
37
|
+
message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, options);
|
|
33
38
|
return new Promise((resolve, reject) => {
|
|
34
39
|
const stream = fs_1.createWriteStream(filePath, { flags: 'a' });
|
|
35
40
|
stream.write(`${Color_1.Color.removeColors(message)}` + '\n');
|
|
@@ -14,7 +14,7 @@ const getTimestamp_1 = require("../Utils/getTimestamp");
|
|
|
14
14
|
class ContextFormatter {
|
|
15
15
|
format(message, options) {
|
|
16
16
|
options = Object.assign({}, { color: Color_1.Color.green, context: 'Logger' }, options);
|
|
17
|
-
const pid = Color_1.Color.
|
|
17
|
+
const pid = Color_1.Color.yellow(`[Athenna] - PID: ${process.pid}`);
|
|
18
18
|
const timestamp = getTimestamp_1.getTimestamp();
|
|
19
19
|
const messageCtx = Color_1.Color.yellow(`[${options.context}] `);
|
|
20
20
|
const timestampDiff = ContextFormatter.getTimestampDiff();
|
|
@@ -14,7 +14,7 @@ const getTimestamp_1 = require("../Utils/getTimestamp");
|
|
|
14
14
|
class DebugFormatter {
|
|
15
15
|
format(message, options) {
|
|
16
16
|
options = Object.assign({}, { color: Color_1.Color.green, context: 'Debugger', namespace: 'api:main' }, options);
|
|
17
|
-
const pid = Color_1.Color.
|
|
17
|
+
const pid = Color_1.Color.yellow(`[Athenna Debugger] - PID: ${process.pid}`);
|
|
18
18
|
const timestamp = Color_1.Color.white(getTimestamp_1.getTimestamp());
|
|
19
19
|
const messageCtx = Color_1.Color.yellow(`[${options.context}] `);
|
|
20
20
|
const timestampDiff = DebugFormatter.getTimestampDiff();
|
|
@@ -14,7 +14,7 @@ class JsonFormatter {
|
|
|
14
14
|
format(message, options) {
|
|
15
15
|
options = Object.assign({}, { color: Color_1.Color.green }, options);
|
|
16
16
|
const pid = Color_1.Color.yellow(`[Athenna] - PID: ${process.pid}`);
|
|
17
|
-
return `${pid} - ${Color_1.Color.bold('JSON:')} ${options.color(JSON.stringify(message))}`;
|
|
17
|
+
return `${pid} - ${Color_1.Color.bold('JSON:')} ${options.color(JSON.stringify(message, null, 2))}`;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
exports.JsonFormatter = JsonFormatter;
|
package/src/Logger.js
CHANGED
|
@@ -32,10 +32,10 @@ class Logger {
|
|
|
32
32
|
}
|
|
33
33
|
async log(message, options) {
|
|
34
34
|
options = Object.assign({}, { context: 'Logger' }, options);
|
|
35
|
-
if (this.runtimeConfig && this.runtimeConfig.
|
|
35
|
+
if (this.runtimeConfig && this.runtimeConfig.formatterConfig) {
|
|
36
36
|
options = {
|
|
37
37
|
...options,
|
|
38
|
-
...this.runtimeConfig.
|
|
38
|
+
...this.runtimeConfig.formatterConfig,
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
41
|
await this.driver.transport(message, options);
|
|
@@ -45,10 +45,10 @@ class Logger {
|
|
|
45
45
|
options.level = 'INFO';
|
|
46
46
|
options.color = Color_1.Color.cyan;
|
|
47
47
|
options.streamType = 'stdout';
|
|
48
|
-
if (this.runtimeConfig && this.runtimeConfig.
|
|
48
|
+
if (this.runtimeConfig && this.runtimeConfig.formatterConfig) {
|
|
49
49
|
options = {
|
|
50
50
|
...options,
|
|
51
|
-
...this.runtimeConfig.
|
|
51
|
+
...this.runtimeConfig.formatterConfig,
|
|
52
52
|
};
|
|
53
53
|
}
|
|
54
54
|
await this.driver.transport(message, options);
|
|
@@ -58,10 +58,10 @@ class Logger {
|
|
|
58
58
|
options.level = 'WARN';
|
|
59
59
|
options.color = Color_1.Color.orange;
|
|
60
60
|
options.streamType = 'stdout';
|
|
61
|
-
if (this.runtimeConfig && this.runtimeConfig.
|
|
61
|
+
if (this.runtimeConfig && this.runtimeConfig.formatterConfig) {
|
|
62
62
|
options = {
|
|
63
63
|
...options,
|
|
64
|
-
...this.runtimeConfig.
|
|
64
|
+
...this.runtimeConfig.formatterConfig,
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
67
|
await this.driver.transport(message, options);
|
|
@@ -71,10 +71,10 @@ class Logger {
|
|
|
71
71
|
options.level = 'ERROR';
|
|
72
72
|
options.color = Color_1.Color.red;
|
|
73
73
|
options.streamType = 'stderr';
|
|
74
|
-
if (this.runtimeConfig && this.runtimeConfig.
|
|
74
|
+
if (this.runtimeConfig && this.runtimeConfig.formatterConfig) {
|
|
75
75
|
options = {
|
|
76
76
|
...options,
|
|
77
|
-
...this.runtimeConfig.
|
|
77
|
+
...this.runtimeConfig.formatterConfig,
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
80
|
await this.driver.transport(message, options);
|
|
@@ -84,10 +84,10 @@ class Logger {
|
|
|
84
84
|
options.level = 'DEBUG';
|
|
85
85
|
options.color = Color_1.Color.purple;
|
|
86
86
|
options.streamType = 'stdout';
|
|
87
|
-
if (this.runtimeConfig && this.runtimeConfig.
|
|
87
|
+
if (this.runtimeConfig && this.runtimeConfig.formatterConfig) {
|
|
88
88
|
options = {
|
|
89
89
|
...options,
|
|
90
|
-
...this.runtimeConfig.
|
|
90
|
+
...this.runtimeConfig.formatterConfig,
|
|
91
91
|
};
|
|
92
92
|
}
|
|
93
93
|
await this.driver.transport(message, options);
|
|
@@ -97,10 +97,10 @@ class Logger {
|
|
|
97
97
|
options.level = 'SUCCESS';
|
|
98
98
|
options.color = Color_1.Color.green;
|
|
99
99
|
options.streamType = 'stdout';
|
|
100
|
-
if (this.runtimeConfig && this.runtimeConfig.
|
|
100
|
+
if (this.runtimeConfig && this.runtimeConfig.formatterConfig) {
|
|
101
101
|
options = {
|
|
102
102
|
...options,
|
|
103
|
-
...this.runtimeConfig.
|
|
103
|
+
...this.runtimeConfig.formatterConfig,
|
|
104
104
|
};
|
|
105
105
|
}
|
|
106
106
|
await this.driver.transport(message, options);
|