pols-logger 1.0.1 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +15 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +30 -17
- package/package.json +2 -3
- package/src/index.ts +36 -21
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import { PRecord } from 'pols-utils';
|
|
2
|
+
export declare enum PThemes {
|
|
3
|
+
INFO = "INFO",
|
|
4
|
+
WARNING = "WARNING",
|
|
5
|
+
ERROR = "ERROR",
|
|
6
|
+
DEBUG = "DEBUG",
|
|
7
|
+
SYSTEM = "SYSTEM",
|
|
8
|
+
FATAL = "FATAL"
|
|
9
|
+
}
|
|
2
10
|
export type PLoggerShowInParams = boolean | {
|
|
3
11
|
info?: boolean;
|
|
4
12
|
warning?: boolean;
|
|
5
13
|
error?: boolean;
|
|
6
14
|
debug?: boolean;
|
|
7
15
|
system?: boolean;
|
|
16
|
+
fatal?: boolean;
|
|
8
17
|
};
|
|
9
18
|
export type PLoggerShowInConfig = {
|
|
10
19
|
console?: PLoggerShowInParams;
|
|
@@ -12,7 +21,10 @@ export type PLoggerShowInConfig = {
|
|
|
12
21
|
};
|
|
13
22
|
export type PLoggerParams = {
|
|
14
23
|
destinationPath?: string;
|
|
15
|
-
fileName?: (
|
|
24
|
+
fileName?: ({ theme, now }: {
|
|
25
|
+
theme?: PThemes;
|
|
26
|
+
now?: Date;
|
|
27
|
+
}) => string;
|
|
16
28
|
showIn?: PLoggerShowInConfig;
|
|
17
29
|
};
|
|
18
30
|
export type PLoggerLogParams = {
|
|
@@ -25,7 +37,7 @@ export type PLoggerLogParams = {
|
|
|
25
37
|
export declare class PLogger {
|
|
26
38
|
destinationPath?: string;
|
|
27
39
|
showIn?: PLoggerShowInConfig;
|
|
28
|
-
fileName?:
|
|
40
|
+
fileName?: PLoggerParams['fileName'];
|
|
29
41
|
onEntryFinish?: (params: PLoggerLogParams) => void;
|
|
30
42
|
constructor(params?: PLoggerParams);
|
|
31
43
|
info(params: PLoggerLogParams): void;
|
|
@@ -33,5 +45,6 @@ export declare class PLogger {
|
|
|
33
45
|
error(params: PLoggerLogParams): void;
|
|
34
46
|
debug(params: PLoggerLogParams): void;
|
|
35
47
|
system(params: PLoggerLogParams): void;
|
|
48
|
+
fatal(params: PLoggerLogParams): void;
|
|
36
49
|
}
|
|
37
50
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAc,MAAM,YAAY,CAAA;AAEhD,oBAAY,OAAO;IAClB,IAAI,SAAS;IACb,OAAO,YAAY;IACnB,KAAK,UAAU;IACf,KAAK,UAAU;IACf,MAAM,WAAW;IACjB,KAAK,UAAU;CACf;AAED,MAAM,MAAM,mBAAmB,GAAG,OAAO,GAAG;IAC3C,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,KAAK,CAAC,EAAE,OAAO,CAAA;CACf,CAAA;AAED,MAAM,MAAM,mBAAmB,GAAG;IACjC,OAAO,CAAC,EAAE,mBAAmB,CAAA;IAC7B,IAAI,CAAC,EAAE,mBAAmB,CAAA;CAC1B,CAAA;AAED,MAAM,MAAM,aAAa,GAAG;IAC3B,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE;QAC3B,KAAK,CAAC,EAAE,OAAO,CAAA;QACf,GAAG,CAAC,EAAE,IAAI,CAAA;KACV,KAAK,MAAM,CAAA;IACZ,MAAM,CAAC,EAAE,mBAAmB,CAAA;CAC5B,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG;IAC9B,KAAK,EAAE,MAAM,CAAA;IACb,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,EAAE,GAAG,KAAK,CAAA;IAC3C,IAAI,CAAC,EAAE,OAAO,CAAA;CACd,CAAA;AAgGD,qBAAa,OAAO;IACnB,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,MAAM,CAAC,EAAE,mBAAmB,CAAA;IAC5B,QAAQ,CAAC,EAAE,aAAa,CAAC,UAAU,CAAC,CAAA;IAC5B,aAAa,CAAC,EAAE,CAAC,MAAM,EAAE,gBAAgB,KAAK,IAAI,CAAA;gBAE9C,MAAM,CAAC,EAAE,aAAa;IAMlC,IAAI,CAAC,MAAM,EAAE,gBAAgB;IAI7B,OAAO,CAAC,MAAM,EAAE,gBAAgB;IAIhC,KAAK,CAAC,MAAM,EAAE,gBAAgB;IAI9B,KAAK,CAAC,MAAM,EAAE,gBAAgB;IAI9B,MAAM,CAAC,MAAM,EAAE,gBAAgB;IAI/B,KAAK,CAAC,MAAM,EAAE,gBAAgB;CAG9B"}
|
package/dist/index.js
CHANGED
|
@@ -3,26 +3,36 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.PLogger = void 0;
|
|
6
|
+
exports.PLogger = exports.PThemes = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const
|
|
9
|
+
const pols_utils_1 = require("pols-utils");
|
|
10
|
+
var PThemes;
|
|
11
|
+
(function (PThemes) {
|
|
12
|
+
PThemes["INFO"] = "INFO";
|
|
13
|
+
PThemes["WARNING"] = "WARNING";
|
|
14
|
+
PThemes["ERROR"] = "ERROR";
|
|
15
|
+
PThemes["DEBUG"] = "DEBUG";
|
|
16
|
+
PThemes["SYSTEM"] = "SYSTEM";
|
|
17
|
+
PThemes["FATAL"] = "FATAL";
|
|
18
|
+
})(PThemes || (exports.PThemes = PThemes = {}));
|
|
10
19
|
const check = (theme, value, def) => {
|
|
11
20
|
if (value == null)
|
|
12
21
|
return def;
|
|
13
22
|
if (typeof value == 'boolean')
|
|
14
23
|
return value;
|
|
15
24
|
switch (theme) {
|
|
16
|
-
case
|
|
17
|
-
case
|
|
18
|
-
case
|
|
19
|
-
case
|
|
20
|
-
case
|
|
25
|
+
case PThemes.INFO: return value.info;
|
|
26
|
+
case PThemes.WARNING: return value.warning;
|
|
27
|
+
case PThemes.ERROR: return value.error;
|
|
28
|
+
case PThemes.DEBUG: return value.debug;
|
|
29
|
+
case PThemes.SYSTEM: return value.system;
|
|
30
|
+
case PThemes.FATAL: return value.fatal;
|
|
21
31
|
}
|
|
22
32
|
};
|
|
23
33
|
const logger = (theme, pLogger, { label, description, body, exit = false, tags }, executeEvent = true) => {
|
|
24
|
-
const now = new
|
|
25
|
-
const nowString =
|
|
34
|
+
const now = new Date;
|
|
35
|
+
const nowString = pols_utils_1.PUtilsDate.format(now, '@dd/@mm/@y @hh:@ii:@ss.@lll');
|
|
26
36
|
const headers = [`[${theme}]`, nowString, '::', label];
|
|
27
37
|
if (description)
|
|
28
38
|
headers.push('::', description);
|
|
@@ -59,7 +69,7 @@ const logger = (theme, pLogger, { label, description, body, exit = false, tags }
|
|
|
59
69
|
}
|
|
60
70
|
/* Por defecto, muestra el mensaje en consola */
|
|
61
71
|
if (check(theme, pLogger.showIn?.console, true)) {
|
|
62
|
-
if (theme
|
|
72
|
+
if ([].includes(theme)) {
|
|
63
73
|
console.error(headers.join(' '));
|
|
64
74
|
if (textBody.length)
|
|
65
75
|
console.error(textBody.join('\n'));
|
|
@@ -72,7 +82,7 @@ const logger = (theme, pLogger, { label, description, body, exit = false, tags }
|
|
|
72
82
|
}
|
|
73
83
|
/* Mensaje en archivo */
|
|
74
84
|
if (check(theme, pLogger.showIn?.file, false)) {
|
|
75
|
-
const fileName = pLogger.fileName?.() ?? `
|
|
85
|
+
const fileName = pLogger.fileName?.({ theme, now }) ?? `LOG_${pols_utils_1.PUtilsDate.format(now, '@y-@mm-@dd')}.log`;
|
|
76
86
|
if (!pLogger.destinationPath)
|
|
77
87
|
throw new Error(`La propiedad 'destinationPath' es requerida si la entrada debe ir a un archivo`);
|
|
78
88
|
const filePath = path_1.default.join(pLogger.destinationPath, fileName);
|
|
@@ -98,7 +108,7 @@ const logger = (theme, pLogger, { label, description, body, exit = false, tags }
|
|
|
98
108
|
pLogger.onEntryFinish?.({ label, description, body, exit, tags });
|
|
99
109
|
}
|
|
100
110
|
catch (error) {
|
|
101
|
-
logger(
|
|
111
|
+
logger(PThemes.ERROR, pLogger, { label: 'PLOGGER', description: 'Error al ejecutar el evento "onEntryFinish"', body: error }, false);
|
|
102
112
|
}
|
|
103
113
|
}
|
|
104
114
|
/* Si se ha dado la opción, se sale del programa */
|
|
@@ -115,19 +125,22 @@ class PLogger {
|
|
|
115
125
|
this.fileName = params?.fileName;
|
|
116
126
|
}
|
|
117
127
|
info(params) {
|
|
118
|
-
logger(
|
|
128
|
+
logger(PThemes.INFO, this, params);
|
|
119
129
|
}
|
|
120
130
|
warning(params) {
|
|
121
|
-
logger(
|
|
131
|
+
logger(PThemes.WARNING, this, params);
|
|
122
132
|
}
|
|
123
133
|
error(params) {
|
|
124
|
-
logger(
|
|
134
|
+
logger(PThemes.ERROR, this, params);
|
|
125
135
|
}
|
|
126
136
|
debug(params) {
|
|
127
|
-
logger(
|
|
137
|
+
logger(PThemes.DEBUG, this, params);
|
|
128
138
|
}
|
|
129
139
|
system(params) {
|
|
130
|
-
logger(
|
|
140
|
+
logger(PThemes.SYSTEM, this, params);
|
|
141
|
+
}
|
|
142
|
+
fatal(params) {
|
|
143
|
+
logger(PThemes.FATAL, this, { ...params, exit: true });
|
|
131
144
|
}
|
|
132
145
|
}
|
|
133
146
|
exports.PLogger = PLogger;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pols-logger",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -30,7 +30,6 @@
|
|
|
30
30
|
"typescript-eslint": "^8.11.0"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"pols-
|
|
34
|
-
"pols-utils": "^5.3.0"
|
|
33
|
+
"pols-utils": "^5.9.0"
|
|
35
34
|
}
|
|
36
35
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import fs from 'fs'
|
|
2
2
|
import path from 'path'
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import { PRecord, PUtilsDate } from 'pols-utils'
|
|
4
|
+
|
|
5
|
+
export enum PThemes {
|
|
6
|
+
INFO = 'INFO',
|
|
7
|
+
WARNING = 'WARNING',
|
|
8
|
+
ERROR = 'ERROR',
|
|
9
|
+
DEBUG = 'DEBUG',
|
|
10
|
+
SYSTEM = 'SYSTEM',
|
|
11
|
+
FATAL = 'FATAL',
|
|
12
|
+
}
|
|
7
13
|
|
|
8
14
|
export type PLoggerShowInParams = boolean | {
|
|
9
15
|
info?: boolean
|
|
@@ -11,6 +17,7 @@ export type PLoggerShowInParams = boolean | {
|
|
|
11
17
|
error?: boolean
|
|
12
18
|
debug?: boolean
|
|
13
19
|
system?: boolean
|
|
20
|
+
fatal?: boolean
|
|
14
21
|
}
|
|
15
22
|
|
|
16
23
|
export type PLoggerShowInConfig = {
|
|
@@ -20,7 +27,10 @@ export type PLoggerShowInConfig = {
|
|
|
20
27
|
|
|
21
28
|
export type PLoggerParams = {
|
|
22
29
|
destinationPath?: string
|
|
23
|
-
fileName?: (
|
|
30
|
+
fileName?: ({ theme, now }: {
|
|
31
|
+
theme?: PThemes
|
|
32
|
+
now?: Date
|
|
33
|
+
}) => string
|
|
24
34
|
showIn?: PLoggerShowInConfig
|
|
25
35
|
}
|
|
26
36
|
|
|
@@ -36,17 +46,18 @@ const check = (theme: PThemes, value: null | undefined | PLoggerShowInParams, de
|
|
|
36
46
|
if (value == null) return def
|
|
37
47
|
if (typeof value == 'boolean') return value
|
|
38
48
|
switch (theme) {
|
|
39
|
-
case
|
|
40
|
-
case
|
|
41
|
-
case
|
|
42
|
-
case
|
|
43
|
-
case
|
|
49
|
+
case PThemes.INFO: return value.info
|
|
50
|
+
case PThemes.WARNING: return value.warning
|
|
51
|
+
case PThemes.ERROR: return value.error
|
|
52
|
+
case PThemes.DEBUG: return value.debug
|
|
53
|
+
case PThemes.SYSTEM: return value.system
|
|
54
|
+
case PThemes.FATAL: return value.fatal
|
|
44
55
|
}
|
|
45
56
|
}
|
|
46
57
|
|
|
47
58
|
const logger = (theme: PThemes, pLogger: PLogger, { label, description, body, exit = false, tags }: PLoggerLogParams, executeEvent = true) => {
|
|
48
|
-
const now = new
|
|
49
|
-
const nowString =
|
|
59
|
+
const now = new Date
|
|
60
|
+
const nowString = PUtilsDate.format(now, '@dd/@mm/@y @hh:@ii:@ss.@lll')
|
|
50
61
|
|
|
51
62
|
const headers: string[] = [`[${theme}]`, nowString, '::', label]
|
|
52
63
|
if (description) headers.push('::', description)
|
|
@@ -81,7 +92,7 @@ const logger = (theme: PThemes, pLogger: PLogger, { label, description, body, ex
|
|
|
81
92
|
|
|
82
93
|
/* Por defecto, muestra el mensaje en consola */
|
|
83
94
|
if (check(theme, pLogger.showIn?.console, true)) {
|
|
84
|
-
if (theme
|
|
95
|
+
if ([].includes(theme)) {
|
|
85
96
|
console.error(headers.join(' '))
|
|
86
97
|
if (textBody.length) console.error(textBody.join('\n'))
|
|
87
98
|
} else {
|
|
@@ -92,7 +103,7 @@ const logger = (theme: PThemes, pLogger: PLogger, { label, description, body, ex
|
|
|
92
103
|
|
|
93
104
|
/* Mensaje en archivo */
|
|
94
105
|
if (check(theme, pLogger.showIn?.file, false)) {
|
|
95
|
-
const fileName = pLogger.fileName?.() ?? `
|
|
106
|
+
const fileName = pLogger.fileName?.({ theme, now }) ?? `LOG_${PUtilsDate.format(now, '@y-@mm-@dd')}.log`
|
|
96
107
|
if (!pLogger.destinationPath) throw new Error(`La propiedad 'destinationPath' es requerida si la entrada debe ir a un archivo`)
|
|
97
108
|
const filePath = path.join(pLogger.destinationPath, fileName)
|
|
98
109
|
|
|
@@ -117,7 +128,7 @@ const logger = (theme: PThemes, pLogger: PLogger, { label, description, body, ex
|
|
|
117
128
|
try {
|
|
118
129
|
pLogger.onEntryFinish?.({ label, description, body, exit, tags })
|
|
119
130
|
} catch (error) {
|
|
120
|
-
logger(
|
|
131
|
+
logger(PThemes.ERROR, pLogger, { label: 'PLOGGER', description: 'Error al ejecutar el evento "onEntryFinish"', body: error }, false)
|
|
121
132
|
}
|
|
122
133
|
}
|
|
123
134
|
|
|
@@ -128,7 +139,7 @@ const logger = (theme: PThemes, pLogger: PLogger, { label, description, body, ex
|
|
|
128
139
|
export class PLogger {
|
|
129
140
|
destinationPath?: string
|
|
130
141
|
showIn?: PLoggerShowInConfig
|
|
131
|
-
fileName?:
|
|
142
|
+
fileName?: PLoggerParams['fileName']
|
|
132
143
|
declare onEntryFinish?: (params: PLoggerLogParams) => void
|
|
133
144
|
|
|
134
145
|
constructor(params?: PLoggerParams) {
|
|
@@ -138,22 +149,26 @@ export class PLogger {
|
|
|
138
149
|
}
|
|
139
150
|
|
|
140
151
|
info(params: PLoggerLogParams) {
|
|
141
|
-
logger(
|
|
152
|
+
logger(PThemes.INFO, this, params)
|
|
142
153
|
}
|
|
143
154
|
|
|
144
155
|
warning(params: PLoggerLogParams) {
|
|
145
|
-
logger(
|
|
156
|
+
logger(PThemes.WARNING, this, params)
|
|
146
157
|
}
|
|
147
158
|
|
|
148
159
|
error(params: PLoggerLogParams) {
|
|
149
|
-
logger(
|
|
160
|
+
logger(PThemes.ERROR, this, params)
|
|
150
161
|
}
|
|
151
162
|
|
|
152
163
|
debug(params: PLoggerLogParams) {
|
|
153
|
-
logger(
|
|
164
|
+
logger(PThemes.DEBUG, this, params)
|
|
154
165
|
}
|
|
155
166
|
|
|
156
167
|
system(params: PLoggerLogParams) {
|
|
157
|
-
logger(
|
|
168
|
+
logger(PThemes.SYSTEM, this, params)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
fatal(params: PLoggerLogParams) {
|
|
172
|
+
logger(PThemes.FATAL, this, { ...params, exit: true })
|
|
158
173
|
}
|
|
159
174
|
}
|