@emartech/json-logger 4.0.1 → 5.0.2
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/README.md +1 -2
- package/dist/config.d.ts +10 -0
- package/dist/config.js +39 -0
- package/dist/enabled/enabled.d.ts +1 -0
- package/dist/enabled/enabled.js +33 -0
- package/dist/formatter/debug.d.ts +6 -0
- package/dist/formatter/debug.js +18 -0
- package/dist/formatter/index.d.ts +8 -0
- package/dist/formatter/index.js +11 -0
- package/dist/formatter/json.d.ts +1 -0
- package/dist/formatter/json.js +7 -0
- package/dist/formatter/logentries.d.ts +1 -0
- package/dist/formatter/logentries.js +24 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +26 -0
- package/dist/logger/logger.d.ts +55 -0
- package/dist/logger/logger.js +121 -0
- package/dist/output/color-name/color-name.d.ts +11 -0
- package/dist/output/color-name/color-name.js +27 -0
- package/dist/output/console.d.ts +1 -0
- package/dist/output/console.js +7 -0
- package/dist/output/format-body/format-body.d.ts +1 -0
- package/dist/output/format-body/format-body.js +23 -0
- package/dist/output/format-time/format-time.d.ts +5 -0
- package/dist/output/format-time/format-time.js +21 -0
- package/dist/output/stringify-level/stringify-level.d.ts +1 -0
- package/dist/output/stringify-level/stringify-level.js +8 -0
- package/dist/timer/timer.d.ts +15 -0
- package/dist/timer/timer.js +38 -0
- package/package.json +11 -5
- package/tsconfig.json +105 -0
- package/index.js +0 -29
- package/src/config.js +0 -39
- package/src/enabled/enabled.js +0 -37
- package/src/formatter/debug.js +0 -15
- package/src/formatter/index.js +0 -7
- package/src/formatter/json.js +0 -5
- package/src/formatter/logentries.js +0 -24
- package/src/logger/logger.js +0 -139
- package/src/output/color-name/color-name.js +0 -27
- package/src/output/console.js +0 -5
- package/src/output/format-body/format-body.js +0 -25
- package/src/output/format-time/format-time.js +0 -21
- package/src/output/stringify-level/stringify-level.js +0 -7
- package/src/timer/timer.js +0 -48
package/README.md
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
# @emartech/json-logger
|
|
2
2
|
|
|
3
3
|
A tiny and fast logging library that outputs logs in JSON format.
|
|
4
|
-
It has the same namespace based enabling/disabling mechanism as [debug]
|
|
5
|
-
and has the same log levels as [bunyan].
|
|
4
|
+
It has the same namespace based enabling/disabling mechanism as [debug].
|
|
6
5
|
|
|
7
6
|
### Installation
|
|
8
7
|
|
package/dist/config.d.ts
ADDED
package/dist/config.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.config = void 0;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
const levels = {
|
|
9
|
+
trace: {
|
|
10
|
+
number: 10,
|
|
11
|
+
name: 'TRACE'
|
|
12
|
+
},
|
|
13
|
+
debug: {
|
|
14
|
+
number: 20,
|
|
15
|
+
name: 'DEBUG'
|
|
16
|
+
},
|
|
17
|
+
info: {
|
|
18
|
+
number: 30,
|
|
19
|
+
name: 'INFO'
|
|
20
|
+
},
|
|
21
|
+
warn: {
|
|
22
|
+
number: 40,
|
|
23
|
+
name: chalk_1.default.yellow('WARN')
|
|
24
|
+
},
|
|
25
|
+
error: {
|
|
26
|
+
number: 50,
|
|
27
|
+
name: chalk_1.default.red('ERROR')
|
|
28
|
+
},
|
|
29
|
+
fatal: {
|
|
30
|
+
number: 60,
|
|
31
|
+
name: chalk_1.default.red('FATAL')
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
const availableLevels = Object.keys(levels);
|
|
35
|
+
const coloredNames = {};
|
|
36
|
+
availableLevels.forEach((levelName) => {
|
|
37
|
+
coloredNames[levels[levelName].number] = levels[levelName].name;
|
|
38
|
+
});
|
|
39
|
+
exports.config = { levels, availableLevels, coloredNames };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isNamespaceEnabled(availableNamespace: string, namespace: string): boolean;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isNamespaceEnabled = void 0;
|
|
4
|
+
function isNamespaceEnabled(availableNamespace, namespace) {
|
|
5
|
+
const availableNamespaces = availableNamespace.split(/[\s,]+/);
|
|
6
|
+
let enabled = false;
|
|
7
|
+
const adds = [];
|
|
8
|
+
const skips = [];
|
|
9
|
+
availableNamespaces.forEach(function (name) {
|
|
10
|
+
if (!name) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
name = name.replace(/\*/g, '.*?');
|
|
14
|
+
if (name[0] === '-') {
|
|
15
|
+
skips.push(new RegExp('^' + name.substr(1) + '$'));
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
adds.push(new RegExp('^' + name + '$'));
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
adds.forEach(function (addRegexp) {
|
|
22
|
+
if (addRegexp.test(namespace)) {
|
|
23
|
+
enabled = true;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
skips.forEach(function (addRegexp) {
|
|
27
|
+
if (addRegexp.test(namespace)) {
|
|
28
|
+
enabled = false;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
return enabled;
|
|
32
|
+
}
|
|
33
|
+
exports.isNamespaceEnabled = isNamespaceEnabled;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.debugFormatter = void 0;
|
|
4
|
+
const color_name_1 = require("../output/color-name/color-name");
|
|
5
|
+
const stringify_level_1 = require("../output/stringify-level/stringify-level");
|
|
6
|
+
const format_time_1 = require("../output/format-time/format-time");
|
|
7
|
+
const format_body_1 = require("../output/format-body/format-body");
|
|
8
|
+
const formatTime = new format_time_1.FormatTime();
|
|
9
|
+
function debugFormatter(log) {
|
|
10
|
+
return [
|
|
11
|
+
color_name_1.ColorName.addColor(log.name),
|
|
12
|
+
(0, stringify_level_1.stringifyLevel)(log.level),
|
|
13
|
+
formatTime.elapsedTime(),
|
|
14
|
+
(0, format_body_1.formatBody)(log)
|
|
15
|
+
].join(' ');
|
|
16
|
+
}
|
|
17
|
+
exports.debugFormatter = debugFormatter;
|
|
18
|
+
;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { jsonFormatter } from './json';
|
|
2
|
+
import { logentriesFormatter } from './logentries';
|
|
3
|
+
import { debugFormatter } from './debug';
|
|
4
|
+
export declare const formatter: {
|
|
5
|
+
json: typeof jsonFormatter;
|
|
6
|
+
debug: typeof debugFormatter;
|
|
7
|
+
logentries: typeof logentriesFormatter;
|
|
8
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatter = void 0;
|
|
4
|
+
const json_1 = require("./json");
|
|
5
|
+
const logentries_1 = require("./logentries");
|
|
6
|
+
const debug_1 = require("./debug");
|
|
7
|
+
exports.formatter = {
|
|
8
|
+
json: json_1.jsonFormatter,
|
|
9
|
+
debug: debug_1.debugFormatter,
|
|
10
|
+
logentries: logentries_1.logentriesFormatter
|
|
11
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function jsonFormatter(log: unknown): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function logentriesFormatter(data: Record<string, unknown>): string;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logentriesFormatter = void 0;
|
|
4
|
+
const isNumeric = (value) => !isNaN(parseFloat(value)) && isFinite(value);
|
|
5
|
+
const isString = (value) => typeof value === 'string' || value instanceof String;
|
|
6
|
+
const convertToTag = (value, key) => {
|
|
7
|
+
if (isString(value)) {
|
|
8
|
+
value = JSON.stringify(value);
|
|
9
|
+
}
|
|
10
|
+
else if (isNumeric(value)) {
|
|
11
|
+
value = value.toString();
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
value = '"' + JSON.stringify(value) + '"';
|
|
15
|
+
}
|
|
16
|
+
return key + '=' + value;
|
|
17
|
+
};
|
|
18
|
+
function logentriesFormatter(data) {
|
|
19
|
+
return Object
|
|
20
|
+
.keys(data)
|
|
21
|
+
.map(key => convertToTag(data[key], key))
|
|
22
|
+
.join(' ');
|
|
23
|
+
}
|
|
24
|
+
exports.logentriesFormatter = logentriesFormatter;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Logger, LoggerConfig } from './logger/logger';
|
|
2
|
+
export { Logger, LoggerConfig } from './logger/logger';
|
|
3
|
+
export { Timer } from './timer/timer';
|
|
4
|
+
export declare function logFactory(namespace: string): Logger;
|
|
5
|
+
export declare namespace logFactory {
|
|
6
|
+
var Logger: typeof import("./logger/logger").Logger;
|
|
7
|
+
var Timer: typeof import("./timer/timer").Timer;
|
|
8
|
+
var getNamespaces: () => string;
|
|
9
|
+
var configure: (options: LoggerConfig) => void;
|
|
10
|
+
var formatter: {
|
|
11
|
+
json: typeof import("./formatter/json").jsonFormatter;
|
|
12
|
+
debug: typeof import("./formatter/debug").debugFormatter;
|
|
13
|
+
logentries: typeof import("./formatter/logentries").logentriesFormatter;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export default logFactory;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logFactory = exports.Timer = exports.Logger = void 0;
|
|
4
|
+
const logger_1 = require("./logger/logger");
|
|
5
|
+
var logger_2 = require("./logger/logger");
|
|
6
|
+
Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return logger_2.Logger; } });
|
|
7
|
+
const timer_1 = require("./timer/timer");
|
|
8
|
+
var timer_2 = require("./timer/timer");
|
|
9
|
+
Object.defineProperty(exports, "Timer", { enumerable: true, get: function () { return timer_2.Timer; } });
|
|
10
|
+
const enabled_1 = require("./enabled/enabled");
|
|
11
|
+
const formatter_1 = require("./formatter");
|
|
12
|
+
function logFactory(namespace) {
|
|
13
|
+
return new logger_1.Logger(namespace, (0, enabled_1.isNamespaceEnabled)(logFactory.getNamespaces(), namespace));
|
|
14
|
+
}
|
|
15
|
+
exports.logFactory = logFactory;
|
|
16
|
+
logFactory.Logger = logger_1.Logger;
|
|
17
|
+
logFactory.Timer = timer_1.Timer;
|
|
18
|
+
logFactory.getNamespaces = function () {
|
|
19
|
+
return process.env.DEBUG || '';
|
|
20
|
+
};
|
|
21
|
+
logFactory.configure = function (options) {
|
|
22
|
+
logger_1.Logger.configure(options);
|
|
23
|
+
};
|
|
24
|
+
logFactory.formatter = formatter_1.formatter;
|
|
25
|
+
exports.default = logFactory;
|
|
26
|
+
module.exports = logFactory;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Timer } from '../timer/timer';
|
|
2
|
+
interface AxiosError extends Error {
|
|
3
|
+
isAxiosError: boolean;
|
|
4
|
+
config: {
|
|
5
|
+
method: string;
|
|
6
|
+
url: string;
|
|
7
|
+
};
|
|
8
|
+
response?: {
|
|
9
|
+
status: number;
|
|
10
|
+
statusText: string;
|
|
11
|
+
data: string;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export interface LoggerConfig {
|
|
15
|
+
formatter: Function;
|
|
16
|
+
output: Function;
|
|
17
|
+
transformers: Function[];
|
|
18
|
+
}
|
|
19
|
+
export declare class Logger {
|
|
20
|
+
_namespace: string;
|
|
21
|
+
_enabled: boolean;
|
|
22
|
+
constructor(namespace: string, enabled: boolean);
|
|
23
|
+
static configure(options: LoggerConfig): void;
|
|
24
|
+
static _validate(options: LoggerConfig): void;
|
|
25
|
+
static config: LoggerConfig;
|
|
26
|
+
isEnabled(): boolean;
|
|
27
|
+
trace(action: string, data?: unknown): void;
|
|
28
|
+
debug(action: string, data?: unknown): void;
|
|
29
|
+
info(action: string, data?: unknown): void;
|
|
30
|
+
warn(action: string, data?: unknown): void;
|
|
31
|
+
error(action: string, data?: unknown): void;
|
|
32
|
+
fatal(action: string, data?: unknown): void;
|
|
33
|
+
_log(level: string, action: string, data: unknown): void;
|
|
34
|
+
customError(severity: string, action: string, error: Error, data?: unknown): void;
|
|
35
|
+
fromError(action: string, error: unknown, data?: unknown): void;
|
|
36
|
+
warnFromError(action: string, error: unknown, data?: unknown): void;
|
|
37
|
+
timer(): Timer;
|
|
38
|
+
_shortenStackTrace(stack: string): string | undefined;
|
|
39
|
+
_shortenData(data: unknown): string | undefined;
|
|
40
|
+
_getErrorDetails(error: Error): {};
|
|
41
|
+
_getAxiosErrorDetails(error: AxiosError): {
|
|
42
|
+
request_method?: undefined;
|
|
43
|
+
request_url?: undefined;
|
|
44
|
+
response_status?: undefined;
|
|
45
|
+
response_status_text?: undefined;
|
|
46
|
+
response_data?: undefined;
|
|
47
|
+
} | {
|
|
48
|
+
request_method: string;
|
|
49
|
+
request_url: string;
|
|
50
|
+
response_status: number | undefined;
|
|
51
|
+
response_status_text: string | undefined;
|
|
52
|
+
response_data: string | undefined;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export {};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Logger = void 0;
|
|
4
|
+
const config_1 = require("../config");
|
|
5
|
+
const STACK_TRACE_LIMIT = 3000;
|
|
6
|
+
const DATA_LIMIT = 3000;
|
|
7
|
+
const timer_1 = require("../timer/timer");
|
|
8
|
+
const json_1 = require("../formatter/json");
|
|
9
|
+
const console_1 = require("../output/console");
|
|
10
|
+
const allowedKeys = ['output', 'formatter', 'transformers'];
|
|
11
|
+
class Logger {
|
|
12
|
+
constructor(namespace, enabled) {
|
|
13
|
+
this._namespace = namespace;
|
|
14
|
+
this._enabled = enabled;
|
|
15
|
+
}
|
|
16
|
+
static configure(options) {
|
|
17
|
+
this._validate(options);
|
|
18
|
+
Object.assign(Logger.config, options);
|
|
19
|
+
}
|
|
20
|
+
static _validate(options) {
|
|
21
|
+
Object.keys(options).forEach(key => {
|
|
22
|
+
if (!allowedKeys.includes(key)) {
|
|
23
|
+
throw new Error('Only the following keys are allowed: formatter, output');
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
isEnabled() {
|
|
28
|
+
return this._enabled;
|
|
29
|
+
}
|
|
30
|
+
trace(action, data = {}) {
|
|
31
|
+
this._log('trace', action, data);
|
|
32
|
+
}
|
|
33
|
+
debug(action, data = {}) {
|
|
34
|
+
this._log('debug', action, data);
|
|
35
|
+
}
|
|
36
|
+
info(action, data = {}) {
|
|
37
|
+
this._log('info', action, data);
|
|
38
|
+
}
|
|
39
|
+
warn(action, data = {}) {
|
|
40
|
+
this._log('warn', action, data);
|
|
41
|
+
}
|
|
42
|
+
error(action, data = {}) {
|
|
43
|
+
this._log('error', action, data);
|
|
44
|
+
}
|
|
45
|
+
fatal(action, data = {}) {
|
|
46
|
+
this._log('fatal', action, data);
|
|
47
|
+
}
|
|
48
|
+
_log(level, action, data) {
|
|
49
|
+
if (!this._enabled) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
let dataToLog = Object.assign({
|
|
53
|
+
name: this._namespace,
|
|
54
|
+
action: action,
|
|
55
|
+
level: config_1.config.levels[level].number,
|
|
56
|
+
time: new Date().toISOString()
|
|
57
|
+
}, data);
|
|
58
|
+
Logger.config.transformers.forEach((transform) => {
|
|
59
|
+
dataToLog = transform(dataToLog);
|
|
60
|
+
});
|
|
61
|
+
Logger.config.output(Logger.config.formatter(dataToLog));
|
|
62
|
+
}
|
|
63
|
+
customError(severity, action, error, data = {}) {
|
|
64
|
+
this._log(severity, action, Object.assign(this._getErrorDetails(error), data));
|
|
65
|
+
}
|
|
66
|
+
fromError(action, error, data = {}) {
|
|
67
|
+
this.customError('error', action, error, data);
|
|
68
|
+
}
|
|
69
|
+
warnFromError(action, error, data = {}) {
|
|
70
|
+
this.customError('warn', action, error, data);
|
|
71
|
+
}
|
|
72
|
+
timer() {
|
|
73
|
+
return new timer_1.Timer(this);
|
|
74
|
+
}
|
|
75
|
+
_shortenStackTrace(stack) {
|
|
76
|
+
if (!stack) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
return stack.length > STACK_TRACE_LIMIT
|
|
80
|
+
? stack.substring(0, STACK_TRACE_LIMIT) + ' ...'
|
|
81
|
+
: stack;
|
|
82
|
+
}
|
|
83
|
+
_shortenData(data) {
|
|
84
|
+
if (typeof data === 'undefined') {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const stringifiedData = typeof data === 'object' ? JSON.stringify(data) : data;
|
|
88
|
+
return stringifiedData.length > DATA_LIMIT
|
|
89
|
+
? stringifiedData.substring(0, DATA_LIMIT) + ' ...'
|
|
90
|
+
: stringifiedData;
|
|
91
|
+
}
|
|
92
|
+
_getErrorDetails(error) {
|
|
93
|
+
if (!(error instanceof Object)) {
|
|
94
|
+
return {};
|
|
95
|
+
}
|
|
96
|
+
const baseDetails = {
|
|
97
|
+
error_name: error.name,
|
|
98
|
+
error_stack: this._shortenStackTrace(error.stack || ''),
|
|
99
|
+
error_message: error.message,
|
|
100
|
+
error_data: this._shortenData(error.data)
|
|
101
|
+
};
|
|
102
|
+
return Object.assign(baseDetails, this._getAxiosErrorDetails(error));
|
|
103
|
+
}
|
|
104
|
+
_getAxiosErrorDetails(error) {
|
|
105
|
+
if (!error.isAxiosError)
|
|
106
|
+
return {};
|
|
107
|
+
return {
|
|
108
|
+
request_method: error.config.method,
|
|
109
|
+
request_url: error.config.url,
|
|
110
|
+
response_status: error.response ? error.response.status : undefined,
|
|
111
|
+
response_status_text: error.response ? error.response.statusText : undefined,
|
|
112
|
+
response_data: error.response ? this._shortenData(error.response.data) : undefined
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.Logger = Logger;
|
|
117
|
+
Logger.config = {
|
|
118
|
+
formatter: json_1.jsonFormatter,
|
|
119
|
+
output: console_1.consoleOutput,
|
|
120
|
+
transformers: []
|
|
121
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ColorName = void 0;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
const colors = ['cyan', 'magenta', 'grey', 'blue', 'green', 'yellow', 'white', 'red'];
|
|
9
|
+
class ColorName {
|
|
10
|
+
static addColor(name) {
|
|
11
|
+
if (!this.names[name]) {
|
|
12
|
+
this.names[name] = { color: this.counter % colors.length };
|
|
13
|
+
this.counter++;
|
|
14
|
+
}
|
|
15
|
+
const color = colors[this.names[name].color];
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
return chalk_1.default[color](name);
|
|
18
|
+
}
|
|
19
|
+
static reset() {
|
|
20
|
+
this.counter = 0;
|
|
21
|
+
this.names = {};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.ColorName = ColorName;
|
|
25
|
+
ColorName.counter = 0;
|
|
26
|
+
ColorName.colors = colors;
|
|
27
|
+
ColorName.names = {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function consoleOutput(formattedLog: unknown): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function formatBody(logBody: any): string;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatBody = void 0;
|
|
4
|
+
function formatBody(logBody) {
|
|
5
|
+
const log = Object.assign({}, logBody);
|
|
6
|
+
delete log.name;
|
|
7
|
+
delete log.level;
|
|
8
|
+
delete log.v;
|
|
9
|
+
delete log.pid;
|
|
10
|
+
delete log.hostname;
|
|
11
|
+
delete log.time;
|
|
12
|
+
if (!log.msg) {
|
|
13
|
+
delete log.msg;
|
|
14
|
+
}
|
|
15
|
+
const keys = Object.keys(log);
|
|
16
|
+
return keys
|
|
17
|
+
.sort()
|
|
18
|
+
.map(function (key) {
|
|
19
|
+
return key + '=' + JSON.stringify(log[key]);
|
|
20
|
+
})
|
|
21
|
+
.join(' ');
|
|
22
|
+
}
|
|
23
|
+
exports.formatBody = formatBody;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FormatTime = void 0;
|
|
4
|
+
class FormatTime {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.lastLog = 0;
|
|
7
|
+
}
|
|
8
|
+
elapsedTime() {
|
|
9
|
+
const current = this.getCurrentTime();
|
|
10
|
+
let elapsed = 0;
|
|
11
|
+
if (this.lastLog) {
|
|
12
|
+
elapsed = current - this.lastLog;
|
|
13
|
+
}
|
|
14
|
+
this.lastLog = current;
|
|
15
|
+
return '+' + elapsed + 'ms';
|
|
16
|
+
}
|
|
17
|
+
getCurrentTime() {
|
|
18
|
+
return new Date().getTime();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.FormatTime = FormatTime;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function stringifyLevel(level: string): string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stringifyLevel = void 0;
|
|
4
|
+
const config_1 = require("../../config");
|
|
5
|
+
function stringifyLevel(level) {
|
|
6
|
+
return config_1.config.coloredNames[level];
|
|
7
|
+
}
|
|
8
|
+
exports.stringifyLevel = stringifyLevel;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Logger } from '../logger/logger';
|
|
2
|
+
export declare class Timer {
|
|
3
|
+
_logger: Logger;
|
|
4
|
+
_start: number;
|
|
5
|
+
constructor(logger: Logger);
|
|
6
|
+
trace(action: string, data?: unknown): void;
|
|
7
|
+
debug(action: string, data?: unknown): void;
|
|
8
|
+
info(action: string, data?: unknown): void;
|
|
9
|
+
warn(action: string, data?: unknown): void;
|
|
10
|
+
error(action: string, data?: unknown): void;
|
|
11
|
+
fatal(action: string, data?: unknown): void;
|
|
12
|
+
fromError(action: string, error: unknown, data?: unknown): void;
|
|
13
|
+
warnFromError(action: string, error: unknown, data?: unknown): void;
|
|
14
|
+
_duration(): number;
|
|
15
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Timer = void 0;
|
|
4
|
+
class Timer {
|
|
5
|
+
constructor(logger) {
|
|
6
|
+
this._logger = logger;
|
|
7
|
+
this._start = new Date().getTime();
|
|
8
|
+
}
|
|
9
|
+
trace(action, data = {}) {
|
|
10
|
+
this._logger.trace(action, Object.assign({ duration: this._duration() }, data));
|
|
11
|
+
}
|
|
12
|
+
debug(action, data = {}) {
|
|
13
|
+
this._logger.debug(action, Object.assign({ duration: this._duration() }, data));
|
|
14
|
+
}
|
|
15
|
+
info(action, data = {}) {
|
|
16
|
+
this._logger.info(action, Object.assign({ duration: this._duration() }, data));
|
|
17
|
+
}
|
|
18
|
+
warn(action, data = {}) {
|
|
19
|
+
this._logger.warn(action, Object.assign({ duration: this._duration() }, data));
|
|
20
|
+
}
|
|
21
|
+
error(action, data = {}) {
|
|
22
|
+
this._logger.error(action, Object.assign({ duration: this._duration() }, data));
|
|
23
|
+
}
|
|
24
|
+
fatal(action, data = {}) {
|
|
25
|
+
this._logger.fatal(action, Object.assign({ duration: this._duration() }, data));
|
|
26
|
+
}
|
|
27
|
+
fromError(action, error, data = {}) {
|
|
28
|
+
this._logger.fromError(action, error, Object.assign({ duration: this._duration() }, data));
|
|
29
|
+
}
|
|
30
|
+
warnFromError(action, error, data = {}) {
|
|
31
|
+
this._logger.warnFromError(action, error, Object.assign({ duration: this._duration() }, data));
|
|
32
|
+
}
|
|
33
|
+
_duration() {
|
|
34
|
+
const end = new Date().getTime();
|
|
35
|
+
return end - this._start;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.Timer = Timer;
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@emartech/json-logger",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.2",
|
|
4
4
|
"description": "Tiny and fast json logger with namespace support",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "mocha ./src/ --recursive",
|
|
8
|
-
"test:watch": "mocha ./src/ --recursive --watch",
|
|
7
|
+
"test": "mocha --require ts-node/register ./src/ --recursive",
|
|
8
|
+
"test:watch": "mocha --require ts-node/register ./src/ --recursive --watch",
|
|
9
|
+
"build": "rm -rf dist && tsc --project ./tsconfig.json",
|
|
9
10
|
"semantic-release": "CI=true semantic-release",
|
|
11
|
+
"example-js": "DEBUG=* node examples/index-js.js",
|
|
12
|
+
"example-ts": "DEBUG=* ts-node examples/index-ts.ts",
|
|
10
13
|
"koa-example": "DEBUG=* node examples/koa.js",
|
|
11
14
|
"express-example": "DEBUG=* node examples/express.js"
|
|
12
15
|
},
|
|
@@ -29,13 +32,16 @@
|
|
|
29
32
|
},
|
|
30
33
|
"devDependencies": {
|
|
31
34
|
"@emartech/cls-adapter": "1.3.0",
|
|
35
|
+
"@types/node": "18.7.2",
|
|
32
36
|
"chai": "4.3.6",
|
|
33
37
|
"express": "4.18.1",
|
|
34
38
|
"koa": "2.13.4",
|
|
35
39
|
"mocha": "10.0.0",
|
|
36
40
|
"semantic-release": "15.5.0",
|
|
37
41
|
"sinon": "14.0.0",
|
|
38
|
-
"sinon-chai": "3.7.0"
|
|
42
|
+
"sinon-chai": "3.7.0",
|
|
43
|
+
"ts-node": "10.9.1",
|
|
44
|
+
"typescript": "4.7.4"
|
|
39
45
|
},
|
|
40
46
|
"repository": {
|
|
41
47
|
"type": "git",
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
{
|
|
2
|
+
"exclude": ["dist", "node_modules", "src/**/*.js"],
|
|
3
|
+
"include": ["src/**/*.ts", "src/**/*.js"],
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
6
|
+
|
|
7
|
+
/* Projects */
|
|
8
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
9
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
10
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
11
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
12
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
13
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
14
|
+
|
|
15
|
+
/* Language and Environment */
|
|
16
|
+
"target": "es2020", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
17
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
18
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
19
|
+
"experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
|
20
|
+
"emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
21
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
22
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
23
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
24
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
25
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
26
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
27
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
28
|
+
|
|
29
|
+
/* Modules */
|
|
30
|
+
"module": "commonjs", /* Specify what module code is generated. */
|
|
31
|
+
"rootDir": "./src", /* Specify the root folder within your source files. */
|
|
32
|
+
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
33
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
34
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
35
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
36
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
37
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
38
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
39
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
40
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
41
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
42
|
+
|
|
43
|
+
/* JavaScript Support */
|
|
44
|
+
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
45
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
46
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
47
|
+
|
|
48
|
+
/* Emit */
|
|
49
|
+
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
50
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
51
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
52
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
53
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
54
|
+
"outDir": "./dist", /* Specify an output folder for all emitted files. */
|
|
55
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
56
|
+
"noEmit": false, /* Disable emitting files from a compilation. */
|
|
57
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
58
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
|
59
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
60
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
61
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
62
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
63
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
64
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
65
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
66
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
67
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
68
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
69
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
70
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
71
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
72
|
+
|
|
73
|
+
/* Interop Constraints */
|
|
74
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
75
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
76
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
|
77
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
78
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
79
|
+
|
|
80
|
+
/* Type Checking */
|
|
81
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
82
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
83
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
84
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
85
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
86
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
87
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
88
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
89
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
90
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
91
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
92
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
93
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
94
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
95
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
96
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
97
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
98
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
99
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
100
|
+
|
|
101
|
+
/* Completeness */
|
|
102
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
103
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
104
|
+
}
|
|
105
|
+
}
|
package/index.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const Logger = require('./src/logger/logger');
|
|
4
|
-
const Timer = require('./src/timer/timer');
|
|
5
|
-
const isNamespaceEnabled = require('./src/enabled/enabled');
|
|
6
|
-
const formatter = require('./src/formatter');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @param namespace
|
|
10
|
-
* @param options
|
|
11
|
-
* @returns {Logger}
|
|
12
|
-
*/
|
|
13
|
-
function logFactory(namespace, options) {
|
|
14
|
-
return new Logger(namespace, isNamespaceEnabled(
|
|
15
|
-
logFactory.getNamespaces(), namespace
|
|
16
|
-
), options);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
logFactory.Logger = Logger;
|
|
20
|
-
logFactory.Timer = Timer;
|
|
21
|
-
logFactory.getNamespaces = function() {
|
|
22
|
-
return process.env.DEBUG || '';
|
|
23
|
-
};
|
|
24
|
-
logFactory.configure = function(options) {
|
|
25
|
-
Logger.configure(options);
|
|
26
|
-
};
|
|
27
|
-
logFactory.formatter = formatter;
|
|
28
|
-
|
|
29
|
-
module.exports = logFactory;
|
package/src/config.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const chalk = require('chalk');
|
|
4
|
-
|
|
5
|
-
const levels = {
|
|
6
|
-
trace: {
|
|
7
|
-
number: 10,
|
|
8
|
-
name: 'TRACE'
|
|
9
|
-
},
|
|
10
|
-
debug: {
|
|
11
|
-
number: 20,
|
|
12
|
-
name: 'DEBUG'
|
|
13
|
-
},
|
|
14
|
-
info: {
|
|
15
|
-
number: 30,
|
|
16
|
-
name: 'INFO'
|
|
17
|
-
},
|
|
18
|
-
warn: {
|
|
19
|
-
number: 40,
|
|
20
|
-
name: chalk.yellow('WARN')
|
|
21
|
-
},
|
|
22
|
-
error: {
|
|
23
|
-
number: 50,
|
|
24
|
-
name: chalk.red('ERROR')
|
|
25
|
-
},
|
|
26
|
-
fatal: {
|
|
27
|
-
number: 60,
|
|
28
|
-
name: chalk.red('FATAL')
|
|
29
|
-
},
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const availableLevels = Object.keys(levels);
|
|
33
|
-
|
|
34
|
-
const coloredNames = {};
|
|
35
|
-
availableLevels.forEach((levelName) => {
|
|
36
|
-
coloredNames[levels[levelName].number] = levels[levelName].name;
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
module.exports = { levels, availableLevels, coloredNames };
|
package/src/enabled/enabled.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
function isNamespaceEnabled(availableNamespaces, namespace) {
|
|
4
|
-
availableNamespaces = availableNamespaces.split(/[\s,]+/);
|
|
5
|
-
let enabled = false;
|
|
6
|
-
const adds = [];
|
|
7
|
-
const skips = [];
|
|
8
|
-
|
|
9
|
-
availableNamespaces.forEach(function(name) {
|
|
10
|
-
if (!name) {
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
name = name.replace(/\*/g, '.*?');
|
|
15
|
-
if (name[0] === '-') {
|
|
16
|
-
skips.push(new RegExp('^' + name.substr(1) + '$'));
|
|
17
|
-
} else {
|
|
18
|
-
adds.push(new RegExp('^' + name + '$'));
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
adds.forEach(function(addRegexp) {
|
|
23
|
-
if (addRegexp.test(namespace)) {
|
|
24
|
-
enabled = true;
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
skips.forEach(function(addRegexp) {
|
|
29
|
-
if (addRegexp.test(namespace)) {
|
|
30
|
-
enabled = false;
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
return enabled;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
module.exports = isNamespaceEnabled;
|
package/src/formatter/debug.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const ColorName = require('../output/color-name/color-name');
|
|
4
|
-
const stringifyLevel = require('../output/stringify-level/stringify-level');
|
|
5
|
-
const formatTime = require('../output/format-time/format-time');
|
|
6
|
-
const formatBody = require('../output/format-body/format-body');
|
|
7
|
-
|
|
8
|
-
module.exports = function(log) {
|
|
9
|
-
return [
|
|
10
|
-
ColorName.addColor(log.name),
|
|
11
|
-
stringifyLevel(log.level),
|
|
12
|
-
formatTime.elapsedTime(log.time),
|
|
13
|
-
formatBody(log)
|
|
14
|
-
].join(' ');
|
|
15
|
-
};
|
package/src/formatter/index.js
DELETED
package/src/formatter/json.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const isNumeric = (value) => !isNaN(parseFloat(value)) && isFinite(value);
|
|
4
|
-
|
|
5
|
-
const isString = (value) => typeof value === 'string' || value instanceof String;
|
|
6
|
-
|
|
7
|
-
const convertToTag = (value, key) => {
|
|
8
|
-
if (isString(value)) {
|
|
9
|
-
value = JSON.stringify(value);
|
|
10
|
-
} else if(isNumeric(value)) {
|
|
11
|
-
value = value.toString();
|
|
12
|
-
} else {
|
|
13
|
-
value = '"' + JSON.stringify(value) + '"';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return key + '=' + value;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
module.exports = function(data) {
|
|
20
|
-
return Object
|
|
21
|
-
.keys(data)
|
|
22
|
-
.map(key => convertToTag(data[key], key))
|
|
23
|
-
.join(' ');
|
|
24
|
-
};
|
package/src/logger/logger.js
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const config = require('../config');
|
|
4
|
-
const STACK_TRACE_LIMIT = 3000;
|
|
5
|
-
const DATA_LIMIT = 3000;
|
|
6
|
-
const Timer = require('../timer/timer');
|
|
7
|
-
const jsonFormatter = require('../formatter/json');
|
|
8
|
-
const consoleOutput = require('../output/console');
|
|
9
|
-
const allowedKeys = ['output', 'formatter', 'transformers'];
|
|
10
|
-
|
|
11
|
-
class Logger {
|
|
12
|
-
constructor(namespace, enabled) {
|
|
13
|
-
this._namespace = namespace;
|
|
14
|
-
this._enabled = enabled;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
static configure(options = {}) {
|
|
18
|
-
this._validate(options);
|
|
19
|
-
Object.assign(Logger.config, options);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
static _validate(options) {
|
|
23
|
-
Object.keys(options).forEach(key => {
|
|
24
|
-
if (!allowedKeys.includes(key)) {
|
|
25
|
-
throw new Error('Only the following keys are allowed: formatter, output');
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
isEnabled() {
|
|
31
|
-
return this._enabled;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
customError(severity, action, error, data = {}) {
|
|
35
|
-
logMethodFactory(severity).bind(this)(action, Object.assign(this._getErrorDetails(error), data));
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
fromError(action, error, data = {}) {
|
|
39
|
-
this.customError('error', action, error, data);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
warnFromError(action, error, data = {}) {
|
|
43
|
-
this.customError('warn', action, error, data);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
timer() {
|
|
47
|
-
return new Timer(this);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
_shortenStackTrace(stack) {
|
|
51
|
-
if (typeof stack === 'undefined') {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return stack.length > STACK_TRACE_LIMIT
|
|
56
|
-
? stack.substring(0, STACK_TRACE_LIMIT) + ' ...'
|
|
57
|
-
: stack;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
_shortenData(data) {
|
|
61
|
-
if (typeof data === 'undefined') {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const stringifiedData = typeof data === 'object' ? JSON.stringify(data) : data;
|
|
66
|
-
|
|
67
|
-
return stringifiedData.length > DATA_LIMIT
|
|
68
|
-
? stringifiedData.substring(0, DATA_LIMIT) + ' ...'
|
|
69
|
-
: stringifiedData;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
_getErrorDetails(error) {
|
|
73
|
-
if (!(error instanceof Object)) {
|
|
74
|
-
return {};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const baseDetails = {
|
|
78
|
-
error_name: error.name,
|
|
79
|
-
error_stack: this._shortenStackTrace(error.stack),
|
|
80
|
-
error_message: error.message,
|
|
81
|
-
error_data: this._shortenData(error.data)
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
return Object.assign(baseDetails, this._getAxiosErrorDetails(error));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
_getAxiosErrorDetails(error) {
|
|
88
|
-
if (!error.isAxiosError) return {};
|
|
89
|
-
|
|
90
|
-
return {
|
|
91
|
-
request_method: error.config.method,
|
|
92
|
-
request_url: error.config.url,
|
|
93
|
-
response_status: error.response ? error.response.status : undefined,
|
|
94
|
-
response_status_text: error.response ? error.response.statusText : undefined,
|
|
95
|
-
response_data: error.response ? this._shortenData(error.response.data) : undefined
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
Logger.config = {
|
|
101
|
-
formatter: jsonFormatter,
|
|
102
|
-
output: consoleOutput,
|
|
103
|
-
transformers: []
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
const logMethodFactory = function(level) {
|
|
107
|
-
return function(action, data = {}) {
|
|
108
|
-
if (!this._enabled) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
let dataToLog = Object.assign(
|
|
113
|
-
{
|
|
114
|
-
name: this._namespace,
|
|
115
|
-
action: action,
|
|
116
|
-
level: config.levels[level].number,
|
|
117
|
-
time: new Date().toISOString()
|
|
118
|
-
},
|
|
119
|
-
data
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
Logger.config.transformers.forEach((transform) => {
|
|
123
|
-
dataToLog = transform(dataToLog);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
Logger.config.output(
|
|
127
|
-
Logger.config.formatter(dataToLog)
|
|
128
|
-
);
|
|
129
|
-
};
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
Logger.prototype.trace = logMethodFactory('trace');
|
|
133
|
-
Logger.prototype.debug = logMethodFactory('debug');
|
|
134
|
-
Logger.prototype.info = logMethodFactory('info');
|
|
135
|
-
Logger.prototype.warn = logMethodFactory('warn');
|
|
136
|
-
Logger.prototype.error = logMethodFactory('error');
|
|
137
|
-
Logger.prototype.fatal = logMethodFactory('fatal');
|
|
138
|
-
|
|
139
|
-
module.exports = Logger;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const chalk = require('chalk');
|
|
4
|
-
const colors = ['cyan', 'magenta', 'grey', 'blue', 'green', 'yellow', 'white', 'red'];
|
|
5
|
-
|
|
6
|
-
class ColorName {
|
|
7
|
-
static addColor(name) {
|
|
8
|
-
if (!this.names[name]) {
|
|
9
|
-
this.names[name] = { color: this.counter % colors.length };
|
|
10
|
-
this.counter++;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const color = colors[this.names[name].color];
|
|
14
|
-
return chalk[color](name);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
static reset() {
|
|
18
|
-
this.counter = 0;
|
|
19
|
-
this.names = {};
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
ColorName.counter = 0;
|
|
24
|
-
ColorName.names = {};
|
|
25
|
-
ColorName.colors = colors;
|
|
26
|
-
|
|
27
|
-
module.exports = ColorName;
|
package/src/output/console.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
module.exports = function(logBody) {
|
|
4
|
-
const log = Object.assign({}, logBody);
|
|
5
|
-
|
|
6
|
-
delete log.name;
|
|
7
|
-
delete log.level;
|
|
8
|
-
delete log.v;
|
|
9
|
-
delete log.pid;
|
|
10
|
-
delete log.hostname;
|
|
11
|
-
delete log.time;
|
|
12
|
-
|
|
13
|
-
if (!log.msg) {
|
|
14
|
-
delete log.msg;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const keys = Object.keys(log);
|
|
18
|
-
|
|
19
|
-
return keys
|
|
20
|
-
.sort()
|
|
21
|
-
.map(function(key) {
|
|
22
|
-
return key + '=' + JSON.stringify(log[key]);
|
|
23
|
-
})
|
|
24
|
-
.join(' ');
|
|
25
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
module.exports = {
|
|
4
|
-
lastLog: null,
|
|
5
|
-
|
|
6
|
-
elapsedTime: function() {
|
|
7
|
-
const current = this.getCurrentTime();
|
|
8
|
-
let elapsed = 0;
|
|
9
|
-
|
|
10
|
-
if (this.lastLog) {
|
|
11
|
-
elapsed = current - this.lastLog;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
this.lastLog = current;
|
|
15
|
-
return '+' + elapsed + 'ms';
|
|
16
|
-
},
|
|
17
|
-
|
|
18
|
-
getCurrentTime: function() {
|
|
19
|
-
return new Date().getTime();
|
|
20
|
-
}
|
|
21
|
-
};
|
package/src/timer/timer.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const logMethodFactory = function(level) {
|
|
4
|
-
return function(action, data) {
|
|
5
|
-
this._logger[level](
|
|
6
|
-
action,
|
|
7
|
-
Object.assign({ duration: this._duration() }, data)
|
|
8
|
-
);
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
class Timer {
|
|
13
|
-
constructor(logger) {
|
|
14
|
-
this._logger = logger;
|
|
15
|
-
this._start = new Date().getTime();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
fromError(action, error, data = {}) {
|
|
19
|
-
this._logger.fromError(
|
|
20
|
-
action,
|
|
21
|
-
error,
|
|
22
|
-
Object.assign({ duration: this._duration() }, data)
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
warnFromError(action, error, data = {}) {
|
|
27
|
-
this._logger.warnFromError(
|
|
28
|
-
action,
|
|
29
|
-
error,
|
|
30
|
-
Object.assign({ duration: this._duration() }, data)
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
_duration() {
|
|
35
|
-
const end = new Date().getTime();
|
|
36
|
-
|
|
37
|
-
return end - this._start;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
Timer.prototype.trace = logMethodFactory('trace');
|
|
42
|
-
Timer.prototype.debug = logMethodFactory('debug');
|
|
43
|
-
Timer.prototype.info = logMethodFactory('info');
|
|
44
|
-
Timer.prototype.warn = logMethodFactory('warn');
|
|
45
|
-
Timer.prototype.error = logMethodFactory('error');
|
|
46
|
-
Timer.prototype.fatal = logMethodFactory('fatal');
|
|
47
|
-
|
|
48
|
-
module.exports = Timer;
|