@emartech/json-logger 5.0.0 → 6.0.0
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 +61 -14
- 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 +13 -0
- package/dist/index.js +21 -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 +1 -1
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
|
|
|
@@ -12,10 +11,13 @@ npm install @emartech/json-logger
|
|
|
12
11
|
|
|
13
12
|
### Usage
|
|
14
13
|
|
|
14
|
+
#### Script
|
|
15
|
+
|
|
15
16
|
```javascript
|
|
16
17
|
process.env.DEBUG = 'redis';
|
|
17
|
-
const
|
|
18
|
-
const
|
|
18
|
+
const { createLogger } = require('@emartech/json-logger');
|
|
19
|
+
const mongoLogger = createLogger('mongo');
|
|
20
|
+
const redisLogger = createLogger('redis');
|
|
19
21
|
|
|
20
22
|
redisLogger.info('connected', { domain: 'yahoo' });
|
|
21
23
|
// {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","domain":"yahoo"}
|
|
@@ -27,6 +29,47 @@ redisLogger.fromError('query', new Error('Unauthorized'), { problem: 'missmatch'
|
|
|
27
29
|
// {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"}
|
|
28
30
|
```
|
|
29
31
|
|
|
32
|
+
#### Class
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
const { createLogger } = require('@emartech/json-logger');
|
|
36
|
+
const logger = createLogger('Exporter');
|
|
37
|
+
|
|
38
|
+
class Exporter {
|
|
39
|
+
export() {
|
|
40
|
+
mongoLogger.info('export', { customer_id: 123 });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { createLogger } from '@emartech/json-logger';
|
|
47
|
+
const logger = createLogger('Exporter');
|
|
48
|
+
|
|
49
|
+
class Exporter {
|
|
50
|
+
export() {
|
|
51
|
+
mongoLogger.info('export', { customer_id: 123 });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
#### Tests
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
import { Logger } from '@emartech/json-logger';
|
|
60
|
+
|
|
61
|
+
describe('Exporter', () => {
|
|
62
|
+
it('should log', () => {
|
|
63
|
+
jest.spyOn(Logger.prototype, 'info').mockReturnValue();
|
|
64
|
+
const exporter = new Exporter();
|
|
65
|
+
|
|
66
|
+
exporter.export();
|
|
67
|
+
|
|
68
|
+
expect(Logger.prototype.info).toHaveBeenCalledWith('export', { customer_id: 123 });
|
|
69
|
+
})
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
30
73
|
More examples can be found in the `examples` directory.
|
|
31
74
|
|
|
32
75
|
### API
|
|
@@ -40,11 +83,12 @@ Disabled instances output no logs.
|
|
|
40
83
|
|
|
41
84
|
```javascript
|
|
42
85
|
process.env.DEBUG = 'redis,mysql';
|
|
86
|
+
const { createLogger } = require('@emartech/json-logger');
|
|
43
87
|
|
|
44
|
-
const mongoLogger =
|
|
88
|
+
const mongoLogger = createLogger('mongo');
|
|
45
89
|
// mongo instance will be disabled
|
|
46
90
|
|
|
47
|
-
const redisLogger =
|
|
91
|
+
const redisLogger = createLogger('redis');
|
|
48
92
|
// redis instance will be enabled
|
|
49
93
|
```
|
|
50
94
|
|
|
@@ -53,7 +97,8 @@ const redisLogger = require('@emartech/json-logger')('redis');
|
|
|
53
97
|
Prints the provided data to the console in JSON format.
|
|
54
98
|
|
|
55
99
|
```javascript
|
|
56
|
-
const
|
|
100
|
+
const { createLogger } = require('@emartech/json-logger');
|
|
101
|
+
const redisLogger = createLogger('redis');
|
|
57
102
|
|
|
58
103
|
redisLogger.info('connected', { domain: 'yahoo' });
|
|
59
104
|
// {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","domain":"yahoo"}
|
|
@@ -93,7 +138,8 @@ The displayed line contains the stack trace, the name and the message of the err
|
|
|
93
138
|
The log level defaults to error.
|
|
94
139
|
|
|
95
140
|
```javascript
|
|
96
|
-
const
|
|
141
|
+
const { createLogger } = require('@emartech/json-logger');
|
|
142
|
+
const redisLogger = createLogger('redis');
|
|
97
143
|
|
|
98
144
|
redisLogger.fromError('query', new Error('Unauthorized'), { problem: 'missmatch' });
|
|
99
145
|
// {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"}
|
|
@@ -110,7 +156,8 @@ but also logs the elapsed time in milliseconds from the creation of the instance
|
|
|
110
156
|
The elapsed time will be logged into the `duration` field.
|
|
111
157
|
|
|
112
158
|
```javascript
|
|
113
|
-
const
|
|
159
|
+
const { createLogger } = require('@emartech/json-logger');
|
|
160
|
+
const redisLogger = createLogger('redis');
|
|
114
161
|
|
|
115
162
|
const timer = redisLogger.timer();
|
|
116
163
|
|
|
@@ -128,9 +175,9 @@ With transformers we can alter the data to be logged before passing to the forma
|
|
|
128
175
|
It is a perfect place to add the name of the machine is running on or the request id associated with the current thread stored on a continuation local storage.
|
|
129
176
|
|
|
130
177
|
```javascript
|
|
131
|
-
const
|
|
178
|
+
const { createLogger } = require('@emartech/json-logger');
|
|
132
179
|
|
|
133
|
-
|
|
180
|
+
createLogger.configure({
|
|
134
181
|
formatter: JSON.stringify,
|
|
135
182
|
output: console.log,
|
|
136
183
|
transformers: []
|
|
@@ -161,11 +208,11 @@ For automating
|
|
|
161
208
|
|
|
162
209
|
```javascript
|
|
163
210
|
const Koa = require('koa');
|
|
164
|
-
const
|
|
211
|
+
const { createLogger } = require('@emartech/json-logger');
|
|
165
212
|
const clsAdapter = require('@emartech/cls-adapter');
|
|
166
|
-
const logger =
|
|
213
|
+
const logger = createLogger('redis');
|
|
167
214
|
|
|
168
|
-
|
|
215
|
+
createLogger.configure({
|
|
169
216
|
transformers: [
|
|
170
217
|
clsAdapter.addContextStorageToInput()
|
|
171
218
|
]
|
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,13 @@
|
|
|
1
|
+
import { Logger, LoggerConfig } from './logger/logger';
|
|
2
|
+
export { Logger, LoggerConfig } from './logger/logger';
|
|
3
|
+
export { Timer } from './timer/timer';
|
|
4
|
+
export declare function createLogger(namespace: string): Logger;
|
|
5
|
+
export declare namespace createLogger {
|
|
6
|
+
var getNamespaces: () => string;
|
|
7
|
+
var configure: (options: LoggerConfig) => void;
|
|
8
|
+
var formatter: {
|
|
9
|
+
json: typeof import("./formatter/json").jsonFormatter;
|
|
10
|
+
debug: typeof import("./formatter/debug").debugFormatter;
|
|
11
|
+
logentries: typeof import("./formatter/logentries").logentriesFormatter;
|
|
12
|
+
};
|
|
13
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createLogger = 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
|
+
var timer_1 = require("./timer/timer");
|
|
8
|
+
Object.defineProperty(exports, "Timer", { enumerable: true, get: function () { return timer_1.Timer; } });
|
|
9
|
+
const enabled_1 = require("./enabled/enabled");
|
|
10
|
+
const formatter_1 = require("./formatter");
|
|
11
|
+
function createLogger(namespace) {
|
|
12
|
+
return new logger_1.Logger(namespace, (0, enabled_1.isNamespaceEnabled)(createLogger.getNamespaces(), namespace));
|
|
13
|
+
}
|
|
14
|
+
exports.createLogger = createLogger;
|
|
15
|
+
createLogger.getNamespaces = function () {
|
|
16
|
+
return process.env.DEBUG || '';
|
|
17
|
+
};
|
|
18
|
+
createLogger.configure = function (options) {
|
|
19
|
+
logger_1.Logger.configure(options);
|
|
20
|
+
};
|
|
21
|
+
createLogger.formatter = formatter_1.formatter;
|
|
@@ -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;
|