@athenna/logger 1.0.8 → 1.1.1
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/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +1 -1
- package/src/Contracts/LevelTypes.d.ts +1 -0
- package/src/Contracts/LevelTypes.js +2 -0
- package/src/Drivers/ConsoleDriver.js +3 -3
- package/src/Drivers/DebugDriver.js +3 -3
- package/src/Drivers/FileDriver.js +3 -3
- package/src/Factories/FormatterFactory.js +6 -6
- package/src/Formatters/{LogFormatter.d.ts → CliFormatter.d.ts} +6 -5
- package/src/Formatters/CliFormatter.js +29 -0
- package/src/Formatters/JsonFormatter.d.ts +2 -2
- package/src/Formatters/JsonFormatter.js +1 -2
- package/src/Formatters/{ContextFormatter.d.ts → NestFormatter.d.ts} +3 -3
- package/src/Formatters/{ContextFormatter.js → NestFormatter.js} +10 -11
- package/src/Formatters/{DebugFormatter.d.ts → SimpleFormatter.d.ts} +7 -6
- package/src/Formatters/{LogFormatter.js → SimpleFormatter.js} +17 -10
- package/src/Logger.js +5 -5
- package/src/Formatters/DebugFormatter.js +0 -32
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -19,5 +19,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
19
19
|
};
|
|
20
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
21
|
__exportStar(require("./src/Logger"), exports);
|
|
22
|
+
__exportStar(require("./src/Utils/Color"), exports);
|
|
22
23
|
__exportStar(require("./src/Contracts/DriverContract"), exports);
|
|
23
24
|
__exportStar(require("./src/Contracts/FormatterContract"), exports);
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare type LevelTypes = 'info' | 'INFO' | 'debug' | 'DEBUG' | 'warn' | 'WARN' | 'error' | 'ERROR' | 'success' | 'SUCCESS';
|
|
@@ -16,15 +16,15 @@ class ConsoleDriver {
|
|
|
16
16
|
const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
|
|
17
17
|
this._formatter = configs.formatter || channelConfig.formatter;
|
|
18
18
|
this._streamType = configs.streamType || channelConfig.streamType;
|
|
19
|
-
this._formatterConfig =
|
|
20
|
-
configs.formatterConfig || channelConfig.formatterConfig;
|
|
19
|
+
this._formatterConfig = Object.assign({}, channelConfig.formatterConfig, configs.formatterConfig);
|
|
21
20
|
}
|
|
22
21
|
transport(message, options) {
|
|
23
22
|
options = Object.assign({}, {
|
|
24
23
|
formatter: this._formatter,
|
|
25
24
|
streamType: this._streamType,
|
|
26
25
|
}, options);
|
|
27
|
-
|
|
26
|
+
const formatterOptions = Object.assign({}, this._formatterConfig, options.formatterConfig);
|
|
27
|
+
message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, formatterOptions);
|
|
28
28
|
process[options.streamType].write(`${message}\n`);
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -17,15 +17,15 @@ class DebugDriver {
|
|
|
17
17
|
const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
|
|
18
18
|
this._formatter = configs.formatter || channelConfig.formatter;
|
|
19
19
|
this._namespace = configs.namespace || channelConfig.namespace;
|
|
20
|
-
this._formatterConfig =
|
|
21
|
-
configs.formatterConfig || channelConfig.formatterConfig;
|
|
20
|
+
this._formatterConfig = Object.assign({}, channelConfig.formatterConfig, configs.formatterConfig);
|
|
22
21
|
}
|
|
23
22
|
transport(message, options) {
|
|
24
23
|
options = Object.assign({}, {
|
|
25
24
|
formatter: this._formatter,
|
|
26
25
|
namespace: this._namespace,
|
|
27
26
|
}, options);
|
|
28
|
-
|
|
27
|
+
const formatterOptions = Object.assign({}, this._formatterConfig, options.formatterConfig);
|
|
28
|
+
message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, formatterOptions);
|
|
29
29
|
debug_1.debug(options.namespace)(message);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
@@ -19,8 +19,7 @@ class FileDriver {
|
|
|
19
19
|
const channelConfig = utils_1.Config.get(`logging.channels.${channel}`);
|
|
20
20
|
this._filePath = configs.filePath || channelConfig.filePath;
|
|
21
21
|
this._formatter = configs.formatter || channelConfig.formatter;
|
|
22
|
-
this._formatterConfig =
|
|
23
|
-
configs.formatterConfig || channelConfig.formatterConfig;
|
|
22
|
+
this._formatterConfig = Object.assign({}, channelConfig.formatterConfig, configs.formatterConfig);
|
|
24
23
|
}
|
|
25
24
|
async transport(message, options) {
|
|
26
25
|
options = Object.assign({}, {
|
|
@@ -32,7 +31,8 @@ class FileDriver {
|
|
|
32
31
|
if (!fs_1.existsSync(dir)) {
|
|
33
32
|
fs_1.mkdirSync(dir, { recursive: true });
|
|
34
33
|
}
|
|
35
|
-
|
|
34
|
+
const formatterOptions = Object.assign({}, this._formatterConfig, options.formatterConfig);
|
|
35
|
+
message = FormatterFactory_1.FormatterFactory.fabricate(options.formatter).format(message, formatterOptions);
|
|
36
36
|
return new Promise((resolve, reject) => {
|
|
37
37
|
const stream = fs_1.createWriteStream(filePath, { flags: 'a' });
|
|
38
38
|
stream.write(`${Color_1.Color.removeColors(message)}` + '\n');
|
|
@@ -9,12 +9,12 @@
|
|
|
9
9
|
*/
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
11
|
exports.FormatterFactory = void 0;
|
|
12
|
-
const LogFormatter_1 = require("../Formatters/LogFormatter");
|
|
13
12
|
const JsonFormatter_1 = require("../Formatters/JsonFormatter");
|
|
14
|
-
const
|
|
15
|
-
const
|
|
13
|
+
const NestFormatter_1 = require("../Formatters/NestFormatter");
|
|
14
|
+
const SimpleFormatter_1 = require("../Formatters/SimpleFormatter");
|
|
16
15
|
const NotFoundFormatterException_1 = require("../Exceptions/NotFoundFormatterException");
|
|
17
16
|
const FormatterAlreadyExistException_1 = require("../Exceptions/FormatterAlreadyExistException");
|
|
17
|
+
const CliFormatter_1 = require("../Formatters/CliFormatter");
|
|
18
18
|
class FormatterFactory {
|
|
19
19
|
static availableFormatters() {
|
|
20
20
|
const availableFormatters = [];
|
|
@@ -39,7 +39,7 @@ class FormatterFactory {
|
|
|
39
39
|
}
|
|
40
40
|
exports.FormatterFactory = FormatterFactory;
|
|
41
41
|
FormatterFactory.formatters = new Map()
|
|
42
|
-
.set('
|
|
43
|
-
.set('
|
|
42
|
+
.set('cli', { Formatter: CliFormatter_1.CliFormatter })
|
|
43
|
+
.set('nest', { Formatter: NestFormatter_1.NestFormatter })
|
|
44
44
|
.set('json', { Formatter: JsonFormatter_1.JsonFormatter })
|
|
45
|
-
.set('
|
|
45
|
+
.set('simple', { Formatter: SimpleFormatter_1.SimpleFormatter });
|
|
@@ -7,12 +7,13 @@
|
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
9
|
import { Chalk } from 'chalk';
|
|
10
|
+
import { LevelTypes } from '../Contracts/LevelTypes';
|
|
10
11
|
import { FormatterContract } from '../Contracts/FormatterContract';
|
|
11
|
-
export interface
|
|
12
|
-
|
|
13
|
-
level:
|
|
12
|
+
export interface CliFormatterOptions {
|
|
13
|
+
chalk: Chalk;
|
|
14
|
+
level: LevelTypes;
|
|
14
15
|
}
|
|
15
|
-
export declare class
|
|
16
|
-
format(message: string, options?: LogFormatterOptions): string;
|
|
16
|
+
export declare class CliFormatter implements FormatterContract {
|
|
17
17
|
private static paintByLevel;
|
|
18
|
+
format(message: string, options: CliFormatterOptions): string;
|
|
18
19
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @athenna/logger
|
|
4
|
+
*
|
|
5
|
+
* (c) João Lenon <lenon@athenna.io>
|
|
6
|
+
*
|
|
7
|
+
* For the full copyright and license information, please view the LICENSE
|
|
8
|
+
* file that was distributed with this source code.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.CliFormatter = void 0;
|
|
12
|
+
const Color_1 = require("../Utils/Color");
|
|
13
|
+
class CliFormatter {
|
|
14
|
+
static paintByLevel(level) {
|
|
15
|
+
const levelColors = {
|
|
16
|
+
info: Color_1.Color.info,
|
|
17
|
+
debug: Color_1.Color.debug,
|
|
18
|
+
warn: Color_1.Color.warning,
|
|
19
|
+
error: Color_1.Color.error,
|
|
20
|
+
success: Color_1.Color.log,
|
|
21
|
+
};
|
|
22
|
+
return levelColors[level.toLowerCase()](`[ ${level.toLowerCase()} ]`);
|
|
23
|
+
}
|
|
24
|
+
format(message, options) {
|
|
25
|
+
const level = CliFormatter.paintByLevel(options.level);
|
|
26
|
+
return `${level} ${message}`;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.CliFormatter = CliFormatter;
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
import { Chalk } from 'chalk';
|
|
10
10
|
import { FormatterContract } from '../Contracts/FormatterContract';
|
|
11
11
|
export interface JsonFormatterOptions {
|
|
12
|
-
|
|
12
|
+
chalk: Chalk;
|
|
13
13
|
}
|
|
14
14
|
export declare class JsonFormatter implements FormatterContract {
|
|
15
|
-
format(message: Record<any, unknown>, options
|
|
15
|
+
format(message: Record<any, unknown>, options: JsonFormatterOptions): string;
|
|
16
16
|
}
|
|
@@ -12,9 +12,8 @@ exports.JsonFormatter = void 0;
|
|
|
12
12
|
const Color_1 = require("../Utils/Color");
|
|
13
13
|
class JsonFormatter {
|
|
14
14
|
format(message, options) {
|
|
15
|
-
options = Object.assign({}, { color: Color_1.Color.green }, options);
|
|
16
15
|
const pid = Color_1.Color.yellow(`[Athenna] - PID: ${process.pid}`);
|
|
17
|
-
return `${pid} - ${Color_1.Color.bold('JSON:')} ${options.
|
|
16
|
+
return `${pid} - ${Color_1.Color.bold('JSON:')} ${options.chalk(JSON.stringify(message, null, 2))}`;
|
|
18
17
|
}
|
|
19
18
|
}
|
|
20
19
|
exports.JsonFormatter = JsonFormatter;
|
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
import { Chalk } from 'chalk';
|
|
10
10
|
import { FormatterContract } from '../Contracts/FormatterContract';
|
|
11
11
|
export interface ContextFormatterOptions {
|
|
12
|
-
|
|
12
|
+
chalk: Chalk;
|
|
13
13
|
context: string;
|
|
14
14
|
}
|
|
15
|
-
export declare class
|
|
16
|
-
format(message: string, options?: ContextFormatterOptions): string;
|
|
15
|
+
export declare class NestFormatter implements FormatterContract {
|
|
17
16
|
private static lastTimestamp?;
|
|
18
17
|
private static getTimestampDiff;
|
|
18
|
+
format(message: string, options: ContextFormatterOptions): string;
|
|
19
19
|
}
|
|
@@ -8,18 +8,10 @@
|
|
|
8
8
|
* file that was distributed with this source code.
|
|
9
9
|
*/
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.
|
|
11
|
+
exports.NestFormatter = void 0;
|
|
12
12
|
const Color_1 = require("../Utils/Color");
|
|
13
13
|
const getTimestamp_1 = require("../Utils/getTimestamp");
|
|
14
|
-
class
|
|
15
|
-
format(message, options) {
|
|
16
|
-
options = Object.assign({}, { color: Color_1.Color.green, context: 'Logger' }, options);
|
|
17
|
-
const pid = Color_1.Color.yellow(`[Athenna] - PID: ${process.pid}`);
|
|
18
|
-
const timestamp = getTimestamp_1.getTimestamp();
|
|
19
|
-
const messageCtx = Color_1.Color.yellow(`[${options.context}] `);
|
|
20
|
-
const timestampDiff = ContextFormatter.getTimestampDiff();
|
|
21
|
-
return `${pid} - ${timestamp} ${messageCtx}${options.color(message)}${timestampDiff}`;
|
|
22
|
-
}
|
|
14
|
+
class NestFormatter {
|
|
23
15
|
static getTimestampDiff() {
|
|
24
16
|
let result = '';
|
|
25
17
|
if (this.lastTimestamp) {
|
|
@@ -28,5 +20,12 @@ class ContextFormatter {
|
|
|
28
20
|
this.lastTimestamp = Date.now();
|
|
29
21
|
return result;
|
|
30
22
|
}
|
|
23
|
+
format(message, options) {
|
|
24
|
+
const pid = Color_1.Color.yellow(`[Athenna] - PID: ${process.pid}`);
|
|
25
|
+
const timestamp = getTimestamp_1.getTimestamp();
|
|
26
|
+
const messageCtx = Color_1.Color.yellow(`[${options.context}] `);
|
|
27
|
+
const timestampDiff = NestFormatter.getTimestampDiff();
|
|
28
|
+
return `${pid} - ${timestamp} ${messageCtx}${options.chalk(message)}${timestampDiff}`;
|
|
29
|
+
}
|
|
31
30
|
}
|
|
32
|
-
exports.
|
|
31
|
+
exports.NestFormatter = NestFormatter;
|
|
@@ -7,14 +7,15 @@
|
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
9
|
import { Chalk } from 'chalk';
|
|
10
|
+
import { LevelTypes } from '../Contracts/LevelTypes';
|
|
10
11
|
import { FormatterContract } from '../Contracts/FormatterContract';
|
|
11
|
-
export interface
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
namespace: string;
|
|
12
|
+
export interface LogFormatterOptions {
|
|
13
|
+
chalk: Chalk;
|
|
14
|
+
level: LevelTypes;
|
|
15
15
|
}
|
|
16
|
-
export declare class
|
|
17
|
-
format(message: string, options?: DebugFormatterOptions): string;
|
|
16
|
+
export declare class SimpleFormatter implements FormatterContract {
|
|
18
17
|
private static lastTimestamp?;
|
|
19
18
|
private static getTimestampDiff;
|
|
19
|
+
private static paintByLevel;
|
|
20
|
+
format(message: string, options: LogFormatterOptions): string;
|
|
20
21
|
}
|
|
@@ -8,16 +8,17 @@
|
|
|
8
8
|
* file that was distributed with this source code.
|
|
9
9
|
*/
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.
|
|
11
|
+
exports.SimpleFormatter = void 0;
|
|
12
12
|
const Color_1 = require("../Utils/Color");
|
|
13
13
|
const getTimestamp_1 = require("../Utils/getTimestamp");
|
|
14
|
-
class
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
class SimpleFormatter {
|
|
15
|
+
static getTimestampDiff() {
|
|
16
|
+
let result = '';
|
|
17
|
+
if (this.lastTimestamp) {
|
|
18
|
+
result = Color_1.Color.yellow(` +${Date.now() - this.lastTimestamp}ms`);
|
|
19
|
+
}
|
|
20
|
+
this.lastTimestamp = Date.now();
|
|
21
|
+
return result;
|
|
21
22
|
}
|
|
22
23
|
static paintByLevel(level) {
|
|
23
24
|
const levelColors = {
|
|
@@ -27,7 +28,13 @@ class LogFormatter {
|
|
|
27
28
|
error: Color_1.Color.error,
|
|
28
29
|
success: Color_1.Color.log,
|
|
29
30
|
};
|
|
30
|
-
return levelColors[level.toLowerCase()](`[${level}]`);
|
|
31
|
+
return levelColors[level.toLowerCase()](`[${level.toUpperCase()}]`);
|
|
32
|
+
}
|
|
33
|
+
format(message, options) {
|
|
34
|
+
const timestamp = getTimestamp_1.getTimestamp();
|
|
35
|
+
const timestampDiff = SimpleFormatter.getTimestampDiff();
|
|
36
|
+
const level = SimpleFormatter.paintByLevel(options.level);
|
|
37
|
+
return `${level} - ${timestamp} ${options.chalk(message)}${timestampDiff}`;
|
|
31
38
|
}
|
|
32
39
|
}
|
|
33
|
-
exports.
|
|
40
|
+
exports.SimpleFormatter = SimpleFormatter;
|
package/src/Logger.js
CHANGED
|
@@ -68,7 +68,7 @@ class Logger {
|
|
|
68
68
|
streamType: 'stdout',
|
|
69
69
|
formatterConfig: {
|
|
70
70
|
level: 'INFO',
|
|
71
|
-
|
|
71
|
+
chalk: Color_1.Color.cyan,
|
|
72
72
|
},
|
|
73
73
|
});
|
|
74
74
|
message = Logger.applyLogEngine(message);
|
|
@@ -79,7 +79,7 @@ class Logger {
|
|
|
79
79
|
streamType: 'stdout',
|
|
80
80
|
formatterConfig: {
|
|
81
81
|
level: 'WARN',
|
|
82
|
-
|
|
82
|
+
chalk: Color_1.Color.orange,
|
|
83
83
|
},
|
|
84
84
|
});
|
|
85
85
|
message = Logger.applyLogEngine(message);
|
|
@@ -90,7 +90,7 @@ class Logger {
|
|
|
90
90
|
streamType: 'stdout',
|
|
91
91
|
formatterConfig: {
|
|
92
92
|
level: 'ERROR',
|
|
93
|
-
|
|
93
|
+
chalk: Color_1.Color.red,
|
|
94
94
|
},
|
|
95
95
|
});
|
|
96
96
|
message = Logger.applyLogEngine(message);
|
|
@@ -101,7 +101,7 @@ class Logger {
|
|
|
101
101
|
streamType: 'stdout',
|
|
102
102
|
formatterConfig: {
|
|
103
103
|
level: 'DEBUG',
|
|
104
|
-
|
|
104
|
+
chalk: Color_1.Color.purple,
|
|
105
105
|
},
|
|
106
106
|
});
|
|
107
107
|
message = Logger.applyLogEngine(message);
|
|
@@ -112,7 +112,7 @@ class Logger {
|
|
|
112
112
|
streamType: 'stdout',
|
|
113
113
|
formatterConfig: {
|
|
114
114
|
level: 'SUCCESS',
|
|
115
|
-
|
|
115
|
+
chalk: Color_1.Color.green,
|
|
116
116
|
},
|
|
117
117
|
});
|
|
118
118
|
message = Logger.applyLogEngine(message);
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @athenna/logger
|
|
4
|
-
*
|
|
5
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
6
|
-
*
|
|
7
|
-
* For the full copyright and license information, please view the LICENSE
|
|
8
|
-
* file that was distributed with this source code.
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.DebugFormatter = void 0;
|
|
12
|
-
const Color_1 = require("../Utils/Color");
|
|
13
|
-
const getTimestamp_1 = require("../Utils/getTimestamp");
|
|
14
|
-
class DebugFormatter {
|
|
15
|
-
format(message, options) {
|
|
16
|
-
options = Object.assign({}, { color: Color_1.Color.green, context: 'Debugger', namespace: 'api:main' }, options);
|
|
17
|
-
const pid = Color_1.Color.yellow(`[Athenna Debugger] - PID: ${process.pid}`);
|
|
18
|
-
const timestamp = Color_1.Color.white(getTimestamp_1.getTimestamp());
|
|
19
|
-
const messageCtx = Color_1.Color.yellow(`[${options.context}] `);
|
|
20
|
-
const timestampDiff = DebugFormatter.getTimestampDiff();
|
|
21
|
-
return `${pid} - ${timestamp} ${messageCtx}${options.color(message)}${timestampDiff}`;
|
|
22
|
-
}
|
|
23
|
-
static getTimestampDiff() {
|
|
24
|
-
let result = '';
|
|
25
|
-
if (this.lastTimestamp) {
|
|
26
|
-
result = Color_1.Color.yellow(` +${Date.now() - this.lastTimestamp}ms`);
|
|
27
|
-
}
|
|
28
|
-
this.lastTimestamp = Date.now();
|
|
29
|
-
return result;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
exports.DebugFormatter = DebugFormatter;
|