nlogs 1.1.3 → 2.3.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 +0 -200
- package/dist/abstract.base.logger.d.ts +46 -0
- package/dist/abstract.base.logger.js +119 -0
- package/dist/abstract.base.logger.js.map +1 -0
- package/dist/base.logger.d.ts +60 -0
- package/dist/base.logger.js +101 -0
- package/dist/base.logger.js.map +1 -0
- package/dist/constants.d.ts +19 -0
- package/dist/constants.js +23 -0
- package/dist/constants.js.map +1 -0
- package/dist/errors/cat.nlogs.error.d.ts +4 -0
- package/dist/errors/cat.nlogs.error.js +12 -0
- package/dist/errors/cat.nlogs.error.js.map +1 -0
- package/dist/errors/fs.nlogs.error.d.ts +4 -0
- package/dist/errors/fs.nlogs.error.js +12 -0
- package/dist/errors/fs.nlogs.error.js.map +1 -0
- package/dist/errors/item.nlogs.error.d.ts +4 -0
- package/dist/errors/item.nlogs.error.js +8 -0
- package/dist/errors/item.nlogs.error.js.map +1 -0
- package/dist/errors/item.notfound.error.d.ts +4 -0
- package/dist/errors/item.notfound.error.js +12 -0
- package/dist/errors/item.notfound.error.js.map +1 -0
- package/dist/errors/msg.nlogs.error.d.ts +4 -0
- package/dist/errors/msg.nlogs.error.js +12 -0
- package/dist/errors/msg.nlogs.error.js.map +1 -0
- package/dist/errors/nlogs.error.d.ts +14 -0
- package/dist/errors/nlogs.error.js +30 -0
- package/dist/errors/nlogs.error.js.map +1 -0
- package/dist/errors/param.error.d.ts +6 -0
- package/dist/errors/param.error.js +14 -0
- package/dist/errors/param.error.js.map +1 -0
- package/dist/helpers/color.d.ts +55 -0
- package/dist/helpers/color.js +65 -0
- package/dist/helpers/color.js.map +1 -0
- package/dist/helpers/debug.d.ts +2 -0
- package/dist/helpers/debug.js +11 -0
- package/dist/helpers/debug.js.map +1 -0
- package/dist/helpers/fs.d.ts +6 -0
- package/dist/helpers/fs.js +43 -0
- package/dist/helpers/fs.js.map +1 -0
- package/dist/helpers/stack.d.ts +5 -0
- package/dist/helpers/stack.js +52 -0
- package/dist/helpers/stack.js.map +1 -0
- package/dist/helpers/string.d.ts +9 -0
- package/dist/helpers/string.js +53 -0
- package/dist/helpers/string.js.map +1 -0
- package/dist/helpers/symbols.d.ts +25 -0
- package/dist/helpers/symbols.js +32 -0
- package/dist/helpers/symbols.js.map +1 -0
- package/dist/helpers/template.d.ts +4 -0
- package/dist/helpers/template.js +58 -0
- package/dist/helpers/template.js.map +1 -0
- package/dist/helpers/testHelpers.d.ts +31 -0
- package/dist/helpers/testHelpers.js +239 -0
- package/dist/helpers/testHelpers.js.map +1 -0
- package/dist/helpers/time.d.ts +1 -0
- package/dist/helpers/time.js +6 -0
- package/dist/helpers/time.js.map +1 -0
- package/dist/helpers/types.d.ts +13 -0
- package/dist/helpers/types.js +3 -0
- package/dist/helpers/types.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +25 -0
- package/dist/logger.js +42 -0
- package/dist/logger.js.map +1 -0
- package/dist/logging.rules.d.ts +28 -0
- package/dist/logging.rules.js +64 -0
- package/dist/logging.rules.js.map +1 -0
- package/dist/message/details.d.ts +43 -0
- package/dist/message/details.js +100 -0
- package/dist/message/details.js.map +1 -0
- package/dist/message/error.details.d.ts +13 -0
- package/dist/message/error.details.js +26 -0
- package/dist/message/error.details.js.map +1 -0
- package/dist/message/log.info.d.ts +20 -0
- package/dist/message/log.info.js +30 -0
- package/dist/message/log.info.js.map +1 -0
- package/dist/message/meta.d.ts +12 -0
- package/dist/message/meta.js +22 -0
- package/dist/message/meta.js.map +1 -0
- package/dist/message/mod.details.d.ts +5 -0
- package/dist/message/mod.details.js +11 -0
- package/dist/message/mod.details.js.map +1 -0
- package/dist/message/time.details.d.ts +6 -0
- package/dist/message/time.details.js +16 -0
- package/dist/message/time.details.js.map +1 -0
- package/dist/message/time.range.d.ts +9 -0
- package/dist/message/time.range.js +23 -0
- package/dist/message/time.range.js.map +1 -0
- package/dist/options.d.ts +26 -0
- package/dist/options.js +80 -0
- package/dist/options.js.map +1 -0
- package/dist/template.logger.d.ts +12 -0
- package/dist/template.logger.js +21 -0
- package/dist/template.logger.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/utils/allowed.list.d.ts +18 -0
- package/dist/utils/allowed.list.js +91 -0
- package/dist/utils/allowed.list.js.map +1 -0
- package/dist/utils/category.d.ts +23 -0
- package/dist/utils/category.js +41 -0
- package/dist/utils/category.js.map +1 -0
- package/dist/utils/console.out.d.ts +10 -0
- package/dist/utils/console.out.js +19 -0
- package/dist/utils/console.out.js.map +1 -0
- package/dist/utils/dark.string.formatter.d.ts +24 -0
- package/dist/utils/dark.string.formatter.js +47 -0
- package/dist/utils/dark.string.formatter.js.map +1 -0
- package/dist/utils/items.counter.d.ts +8 -0
- package/dist/utils/items.counter.js +26 -0
- package/dist/utils/items.counter.js.map +1 -0
- package/dist/utils/items.manager.d.ts +55 -0
- package/dist/utils/items.manager.js +147 -0
- package/dist/utils/items.manager.js.map +1 -0
- package/dist/utils/items.timer.d.ts +9 -0
- package/dist/utils/items.timer.js +38 -0
- package/dist/utils/items.timer.js.map +1 -0
- package/dist/utils/json.formatter.d.ts +14 -0
- package/dist/utils/json.formatter.js +43 -0
- package/dist/utils/json.formatter.js.map +1 -0
- package/dist/utils/light.string.formatter.d.ts +6 -0
- package/dist/utils/light.string.formatter.js +16 -0
- package/dist/utils/light.string.formatter.js.map +1 -0
- package/dist/utils/log.reader.d.ts +16 -0
- package/dist/utils/log.reader.js +149 -0
- package/dist/utils/log.reader.js.map +1 -0
- package/dist/utils/mod.d.ts +7 -0
- package/dist/utils/mod.js +16 -0
- package/dist/utils/mod.js.map +1 -0
- package/dist/utils/mod.resolver.d.ts +12 -0
- package/dist/utils/mod.resolver.js +101 -0
- package/dist/utils/mod.resolver.js.map +1 -0
- package/dist/utils/static.logger.d.ts +31 -0
- package/dist/utils/static.logger.js +84 -0
- package/dist/utils/static.logger.js.map +1 -0
- package/dist/utils/string.formatter.d.ts +30 -0
- package/dist/utils/string.formatter.js +110 -0
- package/dist/utils/string.formatter.js.map +1 -0
- package/dist/utils/trace.store.d.ts +33 -0
- package/dist/utils/trace.store.js +76 -0
- package/dist/utils/trace.store.js.map +1 -0
- package/package.json +33 -12
- package/.eslintrc.js +0 -3
- package/index.d.ts +0 -217
- package/index.js +0 -12
- package/lib/config.js +0 -85
- package/lib/constants.js +0 -7
- package/lib/defaultConfig.js +0 -57
- package/lib/logger.js +0 -134
- package/lib/loggerCore.js +0 -105
- package/lib/stringFormatter.js +0 -4
- package/lib/timeFormatter.js +0 -3
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
3
|
+
import { MaybePromise } from '../helpers/types';
|
|
4
|
+
export interface TraceState<T extends object> {
|
|
5
|
+
traceId: string;
|
|
6
|
+
sessionId?: string;
|
|
7
|
+
parentTrace?: TraceState<T>;
|
|
8
|
+
details?: T;
|
|
9
|
+
}
|
|
10
|
+
export interface WithTraceId {
|
|
11
|
+
traceId?: string;
|
|
12
|
+
sessionId?: string;
|
|
13
|
+
details?: any;
|
|
14
|
+
[key: string]: any;
|
|
15
|
+
}
|
|
16
|
+
export declare class TraceStore<T extends object = object> {
|
|
17
|
+
readonly storage: AsyncLocalStorage<TraceState<T>>;
|
|
18
|
+
readonly rootTraceId: string;
|
|
19
|
+
get store(): TraceState<T>;
|
|
20
|
+
get traceId(): string;
|
|
21
|
+
set traceId(value: string);
|
|
22
|
+
get sessionId(): string;
|
|
23
|
+
set sessionId(value: string);
|
|
24
|
+
get traceIds(): string[] | undefined;
|
|
25
|
+
get details(): {};
|
|
26
|
+
assign(state: Partial<TraceState<T>>): void;
|
|
27
|
+
setDetails(details: T): void;
|
|
28
|
+
mergeDetails(details: T): void;
|
|
29
|
+
run<R extends MaybePromise<any>>(callback: () => R): R;
|
|
30
|
+
run<R extends MaybePromise<any>>(traceId: string, callback: () => R): R;
|
|
31
|
+
run<R extends MaybePromise<any>, D extends WithTraceId>(details: D, callback: () => R): R;
|
|
32
|
+
exit(): Promise<void>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TraceStore = void 0;
|
|
4
|
+
const async_hooks_1 = require("async_hooks");
|
|
5
|
+
const string_1 = require("../helpers/string");
|
|
6
|
+
const param_error_1 = require("../errors/param.error");
|
|
7
|
+
class TraceStore {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.storage = new async_hooks_1.AsyncLocalStorage();
|
|
10
|
+
this.rootTraceId = (0, string_1.uuid)();
|
|
11
|
+
}
|
|
12
|
+
get store() {
|
|
13
|
+
return this.storage.getStore() || { traceId: this.rootTraceId };
|
|
14
|
+
}
|
|
15
|
+
get traceId() {
|
|
16
|
+
return this.store?.traceId;
|
|
17
|
+
}
|
|
18
|
+
set traceId(value) {
|
|
19
|
+
this.assign({ traceId: value });
|
|
20
|
+
}
|
|
21
|
+
get sessionId() {
|
|
22
|
+
return this.store?.sessionId;
|
|
23
|
+
}
|
|
24
|
+
set sessionId(value) {
|
|
25
|
+
this.assign({ sessionId: value });
|
|
26
|
+
}
|
|
27
|
+
get traceIds() {
|
|
28
|
+
if (!this.store.parentTrace)
|
|
29
|
+
return undefined;
|
|
30
|
+
let state = this.store;
|
|
31
|
+
const ids = new Set();
|
|
32
|
+
while (state.parentTrace) {
|
|
33
|
+
state = state.parentTrace;
|
|
34
|
+
ids.add(state.traceId);
|
|
35
|
+
}
|
|
36
|
+
return [...ids].reverse();
|
|
37
|
+
}
|
|
38
|
+
get details() {
|
|
39
|
+
return this.store.details || {};
|
|
40
|
+
}
|
|
41
|
+
assign(state) {
|
|
42
|
+
this.storage.enterWith(Object.assign({}, this.storage.getStore(), state));
|
|
43
|
+
}
|
|
44
|
+
setDetails(details) {
|
|
45
|
+
this.assign({ details });
|
|
46
|
+
}
|
|
47
|
+
mergeDetails(details) {
|
|
48
|
+
this.assign({ details: Object.assign({}, this.details, details) });
|
|
49
|
+
}
|
|
50
|
+
run(tdc, callback) {
|
|
51
|
+
if (typeof tdc === 'function')
|
|
52
|
+
return this.run({}, tdc);
|
|
53
|
+
if (typeof callback !== 'function')
|
|
54
|
+
throw new param_error_1.ParamNlogsError('TraceStore.run', 'callback');
|
|
55
|
+
if (typeof tdc === 'string')
|
|
56
|
+
return this.run({ traceId: tdc }, callback);
|
|
57
|
+
const { traceId, sessionId, details, ...stateDetails } = tdc;
|
|
58
|
+
const state = {
|
|
59
|
+
...this.store,
|
|
60
|
+
traceId: traceId || (0, string_1.uuid)(),
|
|
61
|
+
sessionId: sessionId || this.sessionId,
|
|
62
|
+
details: {
|
|
63
|
+
...this.details,
|
|
64
|
+
...stateDetails,
|
|
65
|
+
...(details || {}),
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
state.parentTrace = this.store;
|
|
69
|
+
return this.storage.run(state, callback);
|
|
70
|
+
}
|
|
71
|
+
async exit() {
|
|
72
|
+
return new Promise(resolve => this.storage.exit(resolve));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.TraceStore = TraceStore;
|
|
76
|
+
//# sourceMappingURL=trace.store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace.store.js","sourceRoot":"","sources":["../../src/utils/trace.store.ts"],"names":[],"mappings":";;;AAAA,6CAA+C;AAC/C,8CAAwC;AAExC,uDAAuD;AAgBvD,MAAa,UAAU;IAAvB;QACW,YAAO,GAAG,IAAI,+BAAiB,EAAiB,CAAA;QAChD,gBAAW,GAAG,IAAA,aAAI,GAAE,CAAA;IAiF/B,CAAC;IA/EC,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,WAAW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
|
|
2
2
|
"name": "nlogs",
|
|
3
|
-
"version": "
|
|
4
|
-
"main": "index.js",
|
|
3
|
+
"version": "2.3.2",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
5
|
"repository": "git@github.com:nerjs/nlogs.git",
|
|
6
6
|
"author": "nerjs <karifan.stap@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
8
|
-
"
|
|
8
|
+
"scripts": {
|
|
9
|
+
"lint": "eslint .",
|
|
10
|
+
"clear": "rm -rf ./dist",
|
|
11
|
+
"build": "tsc --build",
|
|
12
|
+
"watch": "tsc --watch",
|
|
13
|
+
"start": "nodemon --enable-source-maps --trace-warnings ./dist",
|
|
14
|
+
"test": "jest",
|
|
15
|
+
"test:watch": "jest --watch",
|
|
16
|
+
"test:cov": "jest --coverage"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist/*"
|
|
20
|
+
],
|
|
9
21
|
"keywords": [
|
|
10
22
|
"logs",
|
|
11
23
|
"logger",
|
|
12
24
|
"console"
|
|
13
25
|
],
|
|
14
|
-
"dependencies": {
|
|
15
|
-
"colors": "^1.4.0",
|
|
16
|
-
"find-package": "^1.0.0",
|
|
17
|
-
"merge": "^1.2.1",
|
|
18
|
-
"moment": "^2.24.0",
|
|
19
|
-
"pretty-time": "^1.1.0",
|
|
20
|
-
"terminal-link": "^2.1.1"
|
|
21
|
-
},
|
|
22
26
|
"devDependencies": {
|
|
23
|
-
"@
|
|
27
|
+
"@types/jest": "29.2.4",
|
|
28
|
+
"@typescript-eslint/eslint-plugin": "5.47.0",
|
|
29
|
+
"@typescript-eslint/parser": "5.47.0",
|
|
30
|
+
"eslint": "8.23",
|
|
31
|
+
"eslint-config-prettier": "8.5.0",
|
|
32
|
+
"eslint-plugin-prettier": "4.2.1",
|
|
33
|
+
"jest": "29.3.1",
|
|
34
|
+
"nodemon": "2.0.19",
|
|
35
|
+
"prettier": "2.7.1",
|
|
36
|
+
"ts-jest": "29.0.3",
|
|
37
|
+
"typescript": "4.9.4",
|
|
38
|
+
"@types/debug": "4.1.7"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@types/node": "18.11.17",
|
|
42
|
+
"@types/pretty-time": "1.1.2",
|
|
43
|
+
"debug": "4.3.4",
|
|
44
|
+
"pretty-time": "1.1.0"
|
|
24
45
|
}
|
|
25
46
|
}
|
package/.eslintrc.js
DELETED
package/index.d.ts
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
export = Logger;
|
|
3
|
-
|
|
4
|
-
declare function Logger(mod: Logger.ParamModule, config: Logger.Config): Logger.NLogger;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
declare namespace Logger {
|
|
8
|
-
declare type ParamModule = NodeModule | {filename: string} | ImportMeta | string
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @description style methods for colors module
|
|
12
|
-
* @see {@link colors github}
|
|
13
|
-
* @see {@http https://github.com/Marak/colors.js}
|
|
14
|
-
*/
|
|
15
|
-
declare type Style = [
|
|
16
|
-
"black" |
|
|
17
|
-
"red" |
|
|
18
|
-
"green" |
|
|
19
|
-
"yellow" |
|
|
20
|
-
"blue" |
|
|
21
|
-
"magenta" |
|
|
22
|
-
"cyan" |
|
|
23
|
-
"white" |
|
|
24
|
-
"gray" |
|
|
25
|
-
"grey" |
|
|
26
|
-
"brightRed" |
|
|
27
|
-
"brightGreen" |
|
|
28
|
-
"brightYellow" |
|
|
29
|
-
"brightBlue" |
|
|
30
|
-
"brightMagenta" |
|
|
31
|
-
"brightCyan" |
|
|
32
|
-
"brightWhite" |
|
|
33
|
-
"bgBlack" |
|
|
34
|
-
"bgRed" |
|
|
35
|
-
"bgGreen" |
|
|
36
|
-
"bgYellow" |
|
|
37
|
-
"bgBlue" |
|
|
38
|
-
"bgMagenta" |
|
|
39
|
-
"bgCyan" |
|
|
40
|
-
"bgWhite" |
|
|
41
|
-
"bgGray" |
|
|
42
|
-
"bgGrey" |
|
|
43
|
-
"bgBrightRed" |
|
|
44
|
-
"bgBrightGreen" |
|
|
45
|
-
"bgBrightYellow" |
|
|
46
|
-
"bgBrightBlue" |
|
|
47
|
-
"bgBrightMagenta" |
|
|
48
|
-
"bgBrightCyan" |
|
|
49
|
-
"bgBrightWhite" |
|
|
50
|
-
"reset" |
|
|
51
|
-
"bold" |
|
|
52
|
-
"dim" |
|
|
53
|
-
"italic" |
|
|
54
|
-
"underline" |
|
|
55
|
-
"inverse" |
|
|
56
|
-
"hidden" |
|
|
57
|
-
"strikethroug" |
|
|
58
|
-
"rainbow" |
|
|
59
|
-
"zebra" |
|
|
60
|
-
"america" |
|
|
61
|
-
"trap" |
|
|
62
|
-
"random"
|
|
63
|
-
]
|
|
64
|
-
|
|
65
|
-
declare type Segments = {
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @description first segment (moduleName)
|
|
69
|
-
* @default ['yellow', 'italic', 'dim']
|
|
70
|
-
*/
|
|
71
|
-
first: Style,
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* @description filename style
|
|
75
|
-
* @default ['cyan', 'italic']
|
|
76
|
-
*/
|
|
77
|
-
last: Style,
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* @description path sections (from module dir to filename). Without separator
|
|
81
|
-
* @default ['grey', 'italic']
|
|
82
|
-
*/
|
|
83
|
-
all: Style,
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* @description separator path sections
|
|
87
|
-
* @default ['gray']
|
|
88
|
-
*/
|
|
89
|
-
delimiter: Style,
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
declare type MethodConfig = {
|
|
93
|
-
|
|
94
|
-
/** @description Sets whether this method works*/
|
|
95
|
-
show: boolean
|
|
96
|
-
|
|
97
|
-
/** @description style for method build time label. If not specified, use default */
|
|
98
|
-
time?: Style,
|
|
99
|
-
|
|
100
|
-
/** @description style for method build segments (path & moduleName) label. If not specified, use default */
|
|
101
|
-
segments?: Segments,
|
|
102
|
-
|
|
103
|
-
/** @description style for method build methodName label. If not specified, use default */
|
|
104
|
-
label?: Style,
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* @description Show message when invoking logger.time()? Only time method
|
|
108
|
-
* @default false
|
|
109
|
-
*/
|
|
110
|
-
showStartTimer: boolean,
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* @description warning if the label exists when invoking logger.time(). Only time method
|
|
114
|
-
* @default true
|
|
115
|
-
*/
|
|
116
|
-
warningPresentLabel: boolean,
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* @description warning if the label does not exist when invoking logger.timeEnd(). Only time method
|
|
121
|
-
* @default true
|
|
122
|
-
*/
|
|
123
|
-
warningMissingLabel: boolean,
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* @description overwrite the label if one already exists. Only time method
|
|
127
|
-
* @default false
|
|
128
|
-
*/
|
|
129
|
-
changePresentLabel: boolean,
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* @description diff time style. Only time method
|
|
133
|
-
* @default ['yellow']
|
|
134
|
-
*/
|
|
135
|
-
diffTimeColor: Style,
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* @description depth param to dir method
|
|
139
|
-
* @default 3
|
|
140
|
-
*/
|
|
141
|
-
depth: number
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
declare interface Config {
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* @description style for default build time label
|
|
149
|
-
* @default ['gray', 'bgBlack']
|
|
150
|
-
*/
|
|
151
|
-
time: Style
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* @description style for default build methodName label
|
|
155
|
-
* @default ['white', 'bold']
|
|
156
|
-
*/
|
|
157
|
-
label: Style
|
|
158
|
-
|
|
159
|
-
/** @description style for default build segments (path & moduleName) label */
|
|
160
|
-
segments: Segments;
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* @description custom formatters
|
|
164
|
-
*/
|
|
165
|
-
formatters: {
|
|
166
|
-
|
|
167
|
-
/** @description format string labels with styles */
|
|
168
|
-
string: (str: string, styles: Style) => string,
|
|
169
|
-
|
|
170
|
-
/** @description format time labels */
|
|
171
|
-
time: (timestamp: number, short: boolean) => string,
|
|
172
|
-
},
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* @default true
|
|
177
|
-
*/
|
|
178
|
-
enableFileLink: boolean;
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* @description if not NULL, each time the indicated timestamp of the log does not match the previous one, an additional message is displayed indicating the full date and time
|
|
182
|
-
* @default hour
|
|
183
|
-
*/
|
|
184
|
-
groupedEveryTime: 'minute' | 'hour' | 'day' | null,
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* @description config for each method
|
|
188
|
-
*/
|
|
189
|
-
methods: {
|
|
190
|
-
log: MethodConfig,
|
|
191
|
-
info: MethodConfig,
|
|
192
|
-
debug: MethodConfig,
|
|
193
|
-
warn: MethodConfig,
|
|
194
|
-
error: MethodConfig,
|
|
195
|
-
time: MethodConfig,
|
|
196
|
-
dir: MethodConfig,
|
|
197
|
-
table: MethodConfig
|
|
198
|
-
},
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
export declare function getConfig(): Config;
|
|
202
|
-
export declare function setConfig(newConfig: Config): undefined;
|
|
203
|
-
|
|
204
|
-
export declare class NLogger {
|
|
205
|
-
log(): undefined;
|
|
206
|
-
info(): undefined;
|
|
207
|
-
debug(): undefined;
|
|
208
|
-
warn(): undefined;
|
|
209
|
-
error(): undefined;
|
|
210
|
-
time(label: string): undefined;
|
|
211
|
-
timeLog(label: string): undefined;
|
|
212
|
-
timeEnd(label: string): undefined;
|
|
213
|
-
dir(obj: object, params: {depth: number}): undefined;
|
|
214
|
-
table(arr: [object]): undefined;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
package/index.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
const NLogger = require('./lib/logger')
|
|
2
|
-
const Settings = require('./lib/config')
|
|
3
|
-
|
|
4
|
-
const Logger = (mod, settings) => new NLogger(mod, settings)
|
|
5
|
-
|
|
6
|
-
exports = module.exports = Logger
|
|
7
|
-
|
|
8
|
-
exports.Logger = NLogger
|
|
9
|
-
|
|
10
|
-
exports.getConfig = () => Settings.config
|
|
11
|
-
|
|
12
|
-
exports.setConfig = newConfig => Settings.setConfig(newConfig)
|
package/lib/config.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
const merge = require('merge')
|
|
2
|
-
const findPackage = require('find-package')
|
|
3
|
-
const path = require('path')
|
|
4
|
-
const { fileURLToPath } = require('url')
|
|
5
|
-
const defaultConfig = require('./defaultConfig')
|
|
6
|
-
|
|
7
|
-
class NLoggerConfig {
|
|
8
|
-
constructor(mod, innerConfig) {
|
|
9
|
-
this.module = mod
|
|
10
|
-
this.config = innerConfig || {}
|
|
11
|
-
this.filename = null
|
|
12
|
-
this.packagePath = null
|
|
13
|
-
this.packageName = null
|
|
14
|
-
this.segments = []
|
|
15
|
-
|
|
16
|
-
this.initConfig()
|
|
17
|
-
this.initFileInfo()
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
initConfig() {
|
|
21
|
-
this.config = merge.recursive({}, NLoggerConfig.config, this.config || {})
|
|
22
|
-
|
|
23
|
-
let maxWidth = 0
|
|
24
|
-
|
|
25
|
-
Object.keys(this.config.methods).forEach(key => {
|
|
26
|
-
if (key.length > maxWidth) maxWidth = key.length
|
|
27
|
-
this.config.methods[key] = merge.recursive(
|
|
28
|
-
{},
|
|
29
|
-
{
|
|
30
|
-
time: this.config.time,
|
|
31
|
-
segments: this.config.segments,
|
|
32
|
-
label: this.config.label,
|
|
33
|
-
},
|
|
34
|
-
this.config.methods[key],
|
|
35
|
-
)
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
Object.keys(this.config.methods).forEach(key => {
|
|
39
|
-
this.config.methods[key].__space = Array(maxWidth - key.length)
|
|
40
|
-
.fill(null)
|
|
41
|
-
.map(() => ' ')
|
|
42
|
-
.join('')
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
this.formatters = this.config.formatters
|
|
46
|
-
|
|
47
|
-
if (typeof this.module === 'string') {
|
|
48
|
-
this.module = {
|
|
49
|
-
filename: this.module,
|
|
50
|
-
}
|
|
51
|
-
} else if (typeof this.module === 'object' && !this.module.filename && this.module.url) {
|
|
52
|
-
this.module = {
|
|
53
|
-
filename: fileURLToPath(this.module.url),
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (!this.module.path) this.module.path = path.dirname(this.module.filename)
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
initFileInfo() {
|
|
61
|
-
this.filename = this.module.filename
|
|
62
|
-
|
|
63
|
-
const packageInfo = findPackage(this.module.path, true)
|
|
64
|
-
|
|
65
|
-
if (!packageInfo) return
|
|
66
|
-
|
|
67
|
-
this.packageName = packageInfo.name
|
|
68
|
-
this.packagePath = path.dirname(packageInfo.paths.absolute)
|
|
69
|
-
|
|
70
|
-
this.segments = this.filename
|
|
71
|
-
.replace(this.packagePath, '')
|
|
72
|
-
.replace(/^\//, '')
|
|
73
|
-
.split(path.sep)
|
|
74
|
-
|
|
75
|
-
this.segments.unshift(this.packageName)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
static config = defaultConfig
|
|
79
|
-
|
|
80
|
-
static setConfig(newConfig) {
|
|
81
|
-
this.config = merge.recursive({}, this.config, newConfig)
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
module.exports = NLoggerConfig
|
package/lib/constants.js
DELETED
package/lib/defaultConfig.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
const stringFormatter = require('./stringFormatter')
|
|
2
|
-
const timeFormatter = require('./timeFormatter')
|
|
3
|
-
const { GROUP_TIME } = require('./constants')
|
|
4
|
-
|
|
5
|
-
module.exports = {
|
|
6
|
-
time: ['gray', 'bgBlack'],
|
|
7
|
-
label: ['white', 'bold'],
|
|
8
|
-
segments: {
|
|
9
|
-
first: ['yellow', 'italic', 'dim'],
|
|
10
|
-
last: ['cyan', 'italic'],
|
|
11
|
-
all: ['gray', 'italic'],
|
|
12
|
-
delimiter: ['gray'],
|
|
13
|
-
},
|
|
14
|
-
formatters: {
|
|
15
|
-
string: stringFormatter,
|
|
16
|
-
time: timeFormatter,
|
|
17
|
-
},
|
|
18
|
-
enableFileLink: true,
|
|
19
|
-
groupedEveryTime: GROUP_TIME.HOUR, // minute, hour, day, null
|
|
20
|
-
methods: {
|
|
21
|
-
log: {
|
|
22
|
-
show: true,
|
|
23
|
-
},
|
|
24
|
-
info: {
|
|
25
|
-
show: true,
|
|
26
|
-
label: ['green'],
|
|
27
|
-
},
|
|
28
|
-
debug: {
|
|
29
|
-
label: ['magenta'],
|
|
30
|
-
show: process.env.NODE_ENV != 'production',
|
|
31
|
-
},
|
|
32
|
-
warn: {
|
|
33
|
-
show: process.env.NODE_ENV != 'production',
|
|
34
|
-
label: ['yellow'],
|
|
35
|
-
},
|
|
36
|
-
error: {
|
|
37
|
-
label: ['red'],
|
|
38
|
-
show: true,
|
|
39
|
-
},
|
|
40
|
-
time: {
|
|
41
|
-
show: true,
|
|
42
|
-
showStartTimer: false,
|
|
43
|
-
warningMissingLabel: true,
|
|
44
|
-
warningPresentLabel: true,
|
|
45
|
-
changePresentLabel: false,
|
|
46
|
-
diffTimeColor: ['yellow'],
|
|
47
|
-
label: ['black', 'bgYellow'],
|
|
48
|
-
},
|
|
49
|
-
dir: {
|
|
50
|
-
show: process.env.NODE_ENV != 'production',
|
|
51
|
-
depth: 3,
|
|
52
|
-
},
|
|
53
|
-
table: {
|
|
54
|
-
show: process.env.NODE_ENV != 'production',
|
|
55
|
-
},
|
|
56
|
-
},
|
|
57
|
-
}
|
package/lib/logger.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
const prettyTime = require('pretty-time')
|
|
2
|
-
const merge = require('merge')
|
|
3
|
-
const NLoggerCore = require('./loggerCore')
|
|
4
|
-
const { TIME_LABELS_MAP } = require('./constants')
|
|
5
|
-
|
|
6
|
-
class NLogger extends NLoggerCore {
|
|
7
|
-
constructor(mod, config) {
|
|
8
|
-
super(mod, config)
|
|
9
|
-
|
|
10
|
-
this[TIME_LABELS_MAP] = new Map()
|
|
11
|
-
;[
|
|
12
|
-
'log',
|
|
13
|
-
'info',
|
|
14
|
-
'debug',
|
|
15
|
-
'warn',
|
|
16
|
-
'error',
|
|
17
|
-
'time',
|
|
18
|
-
'timeLog',
|
|
19
|
-
'timeEnd',
|
|
20
|
-
'dir',
|
|
21
|
-
'table',
|
|
22
|
-
].forEach(m => {
|
|
23
|
-
this[m] = this[m].bind(this)
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
log(...args) {
|
|
28
|
-
this.logFromMethod('log', 'log', args)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
info(...args) {
|
|
32
|
-
this.logFromMethod('info', 'info', args)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
debug(...args) {
|
|
36
|
-
this.logFromMethod('log', 'debug', args)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
warn(...args) {
|
|
40
|
-
this.logFromMethod('error', 'warn', args)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
error(...args) {
|
|
44
|
-
this.logFromMethod('error', 'error', args)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
time(label) {
|
|
48
|
-
const {
|
|
49
|
-
show,
|
|
50
|
-
showStartTimer,
|
|
51
|
-
warningPresentLabel,
|
|
52
|
-
changePresentLabel,
|
|
53
|
-
label: styleLabel,
|
|
54
|
-
} = this.getMethod('time')
|
|
55
|
-
if (!show) return
|
|
56
|
-
if (this[TIME_LABELS_MAP].has(label)) {
|
|
57
|
-
if (warningPresentLabel)
|
|
58
|
-
this.warn(`Timer: Label ${label} already exists for logger.time()`)
|
|
59
|
-
if (!changePresentLabel) return
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (showStartTimer)
|
|
63
|
-
console.log(
|
|
64
|
-
' ',
|
|
65
|
-
this.createTime('time'),
|
|
66
|
-
this.createSegments('time'),
|
|
67
|
-
this.formatStr('timeStart:', styleLabel),
|
|
68
|
-
label,
|
|
69
|
-
)
|
|
70
|
-
|
|
71
|
-
this[TIME_LABELS_MAP].set(label, Date.now())
|
|
72
|
-
|
|
73
|
-
const timeEnd = () => this.timeEnd(label)
|
|
74
|
-
timeEnd.log = () => this.timeLog(label)
|
|
75
|
-
|
|
76
|
-
return timeEnd
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
__timeLog(label, txt) {
|
|
80
|
-
const { show, warningMissingLabel, diffTimeColor, label: styleLabel } = this.getMethod(
|
|
81
|
-
'time',
|
|
82
|
-
)
|
|
83
|
-
if (!show) return
|
|
84
|
-
if (!this[TIME_LABELS_MAP].has(label)) {
|
|
85
|
-
if (warningMissingLabel)
|
|
86
|
-
this.warn(`Timer: No such label ${label} for logger.${`${txt}`.trim()}()`)
|
|
87
|
-
return
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const time = Date.now() - this[TIME_LABELS_MAP].get(label)
|
|
91
|
-
|
|
92
|
-
console.log(
|
|
93
|
-
' ',
|
|
94
|
-
this.createTime('time'),
|
|
95
|
-
this.createSegments('time'),
|
|
96
|
-
this.formatStr(txt, styleLabel),
|
|
97
|
-
`${label}:: `,
|
|
98
|
-
this.formatStr(prettyTime(1000000 * time, 'ms'), diffTimeColor),
|
|
99
|
-
)
|
|
100
|
-
|
|
101
|
-
return true
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
timeLog(label) {
|
|
105
|
-
this.__timeLog(label, ' timeLog: ')
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
timeEnd(label) {
|
|
109
|
-
const res = this.__timeLog(label, ' timeEnd: ')
|
|
110
|
-
if (res) this[TIME_LABELS_MAP].delete(label)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
dir(obj, params) {
|
|
114
|
-
const { show, depth } = this.getMethod('dir')
|
|
115
|
-
if (!show) return
|
|
116
|
-
console.group(...this.createStr('dir'))
|
|
117
|
-
console.dir(obj, merge({}, { depth }, params || {}))
|
|
118
|
-
console.groupEnd()
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
table(arr) {
|
|
122
|
-
const { show } = this.getMethod('table')
|
|
123
|
-
if (!show) return
|
|
124
|
-
console.group(...this.createStr('table'))
|
|
125
|
-
console.table(arr)
|
|
126
|
-
console.groupEnd()
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
clear() {
|
|
130
|
-
console.clear()
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
module.exports = NLogger
|