reggolxc 2.0.5 → 2.0.6
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/console.js +24 -19
- package/dist/console.test.js +10 -15
- package/package.json +6 -4
- package/src/console.test.ts +21 -27
- package/src/console.ts +71 -48
package/dist/console.js
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
// 1. Crear wrappers de metodos log nativos
|
|
2
|
-
// 2. Establecer sistema que determine si el
|
|
2
|
+
// 2. Establecer sistema que determine si el nivel del log aplica para ser impreso
|
|
3
3
|
// 3. Crear Objetos transports
|
|
4
4
|
// 4. Objetos transports deben recibir un callback
|
|
5
|
-
// 5. Logs en consola deben estar
|
|
5
|
+
// 5. Logs en consola deben estar estilizados para navegadores y terminales.
|
|
6
6
|
// 6. Desarrollar con TDD
|
|
7
|
-
export const LEVELS_ENUM = {
|
|
8
|
-
|
|
7
|
+
export const LEVELS_ENUM = {
|
|
8
|
+
DEBUG: 'debug',
|
|
9
|
+
INFO: 'info',
|
|
10
|
+
WARN: 'warn',
|
|
11
|
+
ERROR: 'error',
|
|
12
|
+
FATAL: 'fatal'
|
|
13
|
+
};
|
|
14
|
+
const LEVELS = {
|
|
15
|
+
[LEVELS_ENUM.DEBUG]: 1,
|
|
16
|
+
[LEVELS_ENUM.INFO]: 2,
|
|
17
|
+
[LEVELS_ENUM.WARN]: 3,
|
|
18
|
+
[LEVELS_ENUM.ERROR]: 4,
|
|
19
|
+
[LEVELS_ENUM.FATAL]: 5
|
|
20
|
+
};
|
|
9
21
|
const BROWSER_STYLES = {
|
|
10
22
|
debug: 'color: #808080; font-weight: bold;',
|
|
11
23
|
info: 'color: #0088ff; font-weight: bold;',
|
|
@@ -14,15 +26,15 @@ const BROWSER_STYLES = {
|
|
|
14
26
|
fatal: 'color: #ffffff; background-color: #ff0000; font-weight: bold; padding: 2px 4px; border-radius: 3px;'
|
|
15
27
|
};
|
|
16
28
|
const TIME_STYLE = 'color: #a9a9a9; font-weight: normal; font-style: italic;';
|
|
17
|
-
class Transport {
|
|
29
|
+
export class Transport {
|
|
18
30
|
constructor(minLevelValue = 0) {
|
|
19
31
|
this.minLevelValue = minLevelValue;
|
|
20
32
|
}
|
|
21
33
|
handle(levelName, levelValue, payload) { }
|
|
22
34
|
}
|
|
23
35
|
export class ConsoleTransport extends Transport {
|
|
24
|
-
|
|
25
|
-
|
|
36
|
+
constructor(minLevel = LEVELS_ENUM.DEBUG) {
|
|
37
|
+
super(LEVELS[minLevel]);
|
|
26
38
|
}
|
|
27
39
|
handle(levelName, levelValue, payload) {
|
|
28
40
|
if (levelValue < this.minLevelValue)
|
|
@@ -32,9 +44,8 @@ export class ConsoleTransport extends Transport {
|
|
|
32
44
|
}
|
|
33
45
|
}
|
|
34
46
|
export class TelemetryTransport extends Transport {
|
|
35
|
-
constructor(fetchFun,
|
|
36
|
-
|
|
37
|
-
super(minLevel);
|
|
47
|
+
constructor(fetchFun, minLevel = LEVELS_ENUM.ERROR) {
|
|
48
|
+
super(LEVELS[minLevel]);
|
|
38
49
|
this.fetchFun = fetchFun;
|
|
39
50
|
}
|
|
40
51
|
handle(levelName, levelValue, payload) {
|
|
@@ -46,12 +57,11 @@ export class TelemetryTransport extends Transport {
|
|
|
46
57
|
}
|
|
47
58
|
}
|
|
48
59
|
/**
|
|
49
|
-
* @description
|
|
50
|
-
|
|
51
|
-
*/
|
|
60
|
+
* @description Custom logger
|
|
61
|
+
*/
|
|
52
62
|
export class Roggel {
|
|
53
63
|
constructor(transports = []) {
|
|
54
|
-
this.transports = transports;
|
|
64
|
+
this.transports = Array.isArray(transports) ? transports : [transports];
|
|
55
65
|
}
|
|
56
66
|
addTransport(transport) {
|
|
57
67
|
const transportArr = Array.isArray(transport) ? transport : [transport];
|
|
@@ -73,13 +83,8 @@ export class Roggel {
|
|
|
73
83
|
});
|
|
74
84
|
}
|
|
75
85
|
debug(message, ...args) { this.processLog('debug', message, args); }
|
|
76
|
-
;
|
|
77
86
|
info(message, ...args) { this.processLog('info', message, args); }
|
|
78
|
-
;
|
|
79
87
|
warn(message, ...args) { this.processLog('warn', message, args); }
|
|
80
|
-
;
|
|
81
88
|
error(message, ...args) { this.processLog('error', message, args); }
|
|
82
|
-
;
|
|
83
89
|
fatal(message, ...args) { this.processLog('fatal', message, args); }
|
|
84
|
-
;
|
|
85
90
|
}
|
package/dist/console.test.js
CHANGED
|
@@ -1,30 +1,25 @@
|
|
|
1
1
|
import { Roggel, TelemetryTransport, ConsoleTransport, LEVELS_ENUM } from "./console";
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { describe, expect, it, vi } from "vitest";
|
|
3
3
|
describe('Logger', () => {
|
|
4
4
|
const logger = new Roggel();
|
|
5
5
|
it('should have functions [debug, info, warn, error, fatal]', () => {
|
|
6
|
-
expect(Object.values(LEVELS_ENUM).every((l) => typeof logger[l])).toBe(true);
|
|
6
|
+
expect(Object.values(LEVELS_ENUM).every((l) => typeof logger[l] === 'function')).toBe(true);
|
|
7
7
|
});
|
|
8
|
-
it('
|
|
8
|
+
it('addTransport should be a function', () => {
|
|
9
9
|
expect(typeof logger.addTransport).toBe('function');
|
|
10
10
|
});
|
|
11
|
-
it('
|
|
11
|
+
it('should add new transport', () => {
|
|
12
12
|
const transport = new ConsoleTransport();
|
|
13
13
|
logger.addTransport(transport);
|
|
14
14
|
expect(logger.getTransports().length).toBe(1);
|
|
15
15
|
});
|
|
16
16
|
it('should process log', () => {
|
|
17
|
-
const
|
|
18
|
-
const sendLogService =
|
|
19
|
-
return new Promise((resolve, reject) => {
|
|
20
|
-
setTimeout(() => {
|
|
21
|
-
resolve(payload);
|
|
22
|
-
}, 2000);
|
|
23
|
-
});
|
|
24
|
-
};
|
|
17
|
+
const localLogger = new Roggel();
|
|
18
|
+
const sendLogService = vi.fn().mockResolvedValue('ok');
|
|
25
19
|
const telemetry = new TelemetryTransport(sendLogService);
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
expect(() =>
|
|
20
|
+
const consoleTransport = new ConsoleTransport();
|
|
21
|
+
localLogger.addTransport([telemetry, consoleTransport]);
|
|
22
|
+
expect(() => localLogger.error('print test')).not.toThrow();
|
|
23
|
+
expect(sendLogService).toHaveBeenCalled();
|
|
29
24
|
});
|
|
30
25
|
});
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reggolxc",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
4
4
|
"description": "",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "./dist/console.js",
|
|
7
|
+
"module": "./dist/console.js",
|
|
8
|
+
"types": "./dist/console.d.ts",
|
|
6
9
|
"scripts": {
|
|
7
10
|
"test": "vitest"
|
|
8
11
|
},
|
|
@@ -11,6 +14,5 @@
|
|
|
11
14
|
"devDependencies": {
|
|
12
15
|
"typescript": "^7.0.2",
|
|
13
16
|
"vitest": "^4.1.11"
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/console.test.ts
CHANGED
|
@@ -1,39 +1,33 @@
|
|
|
1
|
-
import { Roggel, TelemetryTransport, ConsoleTransport, LEVELS_ENUM, type
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
|
|
1
|
+
import { Roggel, TelemetryTransport, ConsoleTransport, LEVELS_ENUM, type LogLevel, type LogPayload } from "./console";
|
|
2
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
3
|
|
|
5
4
|
describe('Logger', () => {
|
|
6
5
|
const logger = new Roggel();
|
|
7
6
|
|
|
8
7
|
it('should have functions [debug, info, warn, error, fatal]', () => {
|
|
9
|
-
expect(
|
|
10
|
-
|
|
8
|
+
expect(
|
|
9
|
+
Object.values(LEVELS_ENUM).every((l: LogLevel) => typeof logger[l as keyof Roggel] === 'function')
|
|
10
|
+
).toBe(true);
|
|
11
|
+
});
|
|
11
12
|
|
|
12
|
-
it('
|
|
13
|
+
it('addTransport should be a function', () => {
|
|
13
14
|
expect(typeof logger.addTransport).toBe('function');
|
|
14
|
-
})
|
|
15
|
-
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should add new transport', () => {
|
|
16
18
|
const transport = new ConsoleTransport();
|
|
17
19
|
logger.addTransport(transport);
|
|
18
20
|
expect(logger.getTransports().length).toBe(1);
|
|
19
|
-
})
|
|
20
|
-
it('should process log', () => {
|
|
21
|
-
const logger = new Roggel();
|
|
22
|
-
const sendLogService = async <T,>(payload: payloadI) => {
|
|
23
|
-
return new Promise<T>(
|
|
24
|
-
(resolve, reject) => {
|
|
25
|
-
setTimeout(() => {
|
|
26
|
-
resolve(payload as unknown as T);
|
|
27
|
-
}, 2000)
|
|
28
|
-
},
|
|
29
|
-
)
|
|
30
|
-
}
|
|
21
|
+
});
|
|
31
22
|
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
23
|
+
it('should process log', () => {
|
|
24
|
+
const localLogger = new Roggel();
|
|
25
|
+
const sendLogService = vi.fn().mockResolvedValue('ok');
|
|
26
|
+
const telemetry = new TelemetryTransport(sendLogService);
|
|
27
|
+
const consoleTransport = new ConsoleTransport();
|
|
28
|
+
localLogger.addTransport([telemetry, consoleTransport]);
|
|
29
|
+
expect(() => localLogger.error('print test')).not.toThrow();
|
|
30
|
+
expect(sendLogService).toHaveBeenCalled();
|
|
31
|
+
});
|
|
38
32
|
|
|
39
|
-
})
|
|
33
|
+
});
|
package/src/console.ts
CHANGED
|
@@ -1,12 +1,28 @@
|
|
|
1
1
|
// 1. Crear wrappers de metodos log nativos
|
|
2
|
-
// 2. Establecer sistema que determine si el
|
|
2
|
+
// 2. Establecer sistema que determine si el nivel del log aplica para ser impreso
|
|
3
3
|
// 3. Crear Objetos transports
|
|
4
4
|
// 4. Objetos transports deben recibir un callback
|
|
5
|
-
// 5. Logs en consola deben estar
|
|
5
|
+
// 5. Logs en consola deben estar estilizados para navegadores y terminales.
|
|
6
6
|
// 6. Desarrollar con TDD
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
const
|
|
8
|
+
|
|
9
|
+
export const LEVELS_ENUM = {
|
|
10
|
+
DEBUG: 'debug',
|
|
11
|
+
INFO: 'info',
|
|
12
|
+
WARN: 'warn',
|
|
13
|
+
ERROR: 'error',
|
|
14
|
+
FATAL: 'fatal'
|
|
15
|
+
} as const;
|
|
16
|
+
|
|
17
|
+
export type LogLevel = typeof LEVELS_ENUM[keyof typeof LEVELS_ENUM];
|
|
18
|
+
|
|
19
|
+
const LEVELS: Record<LogLevel, number> = {
|
|
20
|
+
[LEVELS_ENUM.DEBUG]: 1,
|
|
21
|
+
[LEVELS_ENUM.INFO]: 2,
|
|
22
|
+
[LEVELS_ENUM.WARN]: 3,
|
|
23
|
+
[LEVELS_ENUM.ERROR]: 4,
|
|
24
|
+
[LEVELS_ENUM.FATAL]: 5
|
|
25
|
+
};
|
|
10
26
|
|
|
11
27
|
const BROWSER_STYLES: Record<string, string> = {
|
|
12
28
|
debug: 'color: #808080; font-weight: bold;',
|
|
@@ -18,47 +34,55 @@ const BROWSER_STYLES: Record<string, string> = {
|
|
|
18
34
|
|
|
19
35
|
const TIME_STYLE = 'color: #a9a9a9; font-weight: normal; font-style: italic;';
|
|
20
36
|
|
|
21
|
-
export
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
timestamp: string,
|
|
27
|
-
message: string,
|
|
28
|
-
args: unknown[]
|
|
37
|
+
export interface LogPayload {
|
|
38
|
+
level: LogLevel;
|
|
39
|
+
timestamp: string;
|
|
40
|
+
message: string;
|
|
41
|
+
args: any[];
|
|
29
42
|
}
|
|
30
43
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
44
|
+
export type FetchTransportFn = (payload: LogPayload) => Promise<unknown>;
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
export class Transport {
|
|
48
|
+
public minLevelValue: number;
|
|
49
|
+
|
|
50
|
+
constructor(minLevelValue: number = 0) {
|
|
51
|
+
this.minLevelValue = minLevelValue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
handle(levelName: LogLevel, levelValue: number, payload: LogPayload): void {}
|
|
34
55
|
}
|
|
35
56
|
|
|
36
57
|
export class ConsoleTransport extends Transport {
|
|
37
|
-
|
|
38
|
-
|
|
58
|
+
constructor(minLevel: LogLevel = LEVELS_ENUM.DEBUG) {
|
|
59
|
+
super(LEVELS[minLevel]);
|
|
39
60
|
}
|
|
40
61
|
|
|
41
|
-
handle(levelName:
|
|
62
|
+
handle(levelName: LogLevel, levelValue: number, payload: LogPayload): void {
|
|
42
63
|
if (levelValue < this.minLevelValue) return;
|
|
43
|
-
const consoleMethod = (levelName === 'fatal' ? 'error' : levelName) as consoleKeysT;
|
|
44
64
|
|
|
45
|
-
|
|
65
|
+
const consoleMethod = (levelName === 'fatal' ? 'error' : levelName) as keyof Console;
|
|
66
|
+
|
|
67
|
+
(console[consoleMethod] as Function)(
|
|
46
68
|
`%c[${payload.timestamp}] %c[${levelName.toUpperCase()}]`,
|
|
47
69
|
TIME_STYLE,
|
|
48
|
-
BROWSER_STYLES[levelName] || '',
|
|
49
|
-
payload.message,
|
|
70
|
+
BROWSER_STYLES[levelName] || '',
|
|
71
|
+
payload.message,
|
|
50
72
|
...payload.args
|
|
51
73
|
);
|
|
52
74
|
}
|
|
53
75
|
}
|
|
54
76
|
|
|
55
77
|
export class TelemetryTransport extends Transport {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
78
|
+
private fetchFun: FetchTransportFn;
|
|
79
|
+
|
|
80
|
+
constructor(fetchFun: FetchTransportFn, minLevel: LogLevel = LEVELS_ENUM.ERROR) {
|
|
81
|
+
super(LEVELS[minLevel]);
|
|
82
|
+
this.fetchFun = fetchFun;
|
|
59
83
|
}
|
|
60
84
|
|
|
61
|
-
handle(levelName:
|
|
85
|
+
handle(levelName: LogLevel, levelValue: number, payload: LogPayload): void {
|
|
62
86
|
if (levelValue < this.minLevelValue) return;
|
|
63
87
|
|
|
64
88
|
this.fetchFun(payload).catch(err => {
|
|
@@ -67,44 +91,43 @@ export class TelemetryTransport extends Transport {
|
|
|
67
91
|
}
|
|
68
92
|
}
|
|
69
93
|
|
|
70
|
-
interface IRoggel {
|
|
71
94
|
|
|
72
|
-
|
|
95
|
+
/**
|
|
96
|
+
* @description Custom logger
|
|
97
|
+
*/
|
|
98
|
+
export class Roggel {
|
|
99
|
+
private transports: Transport[];
|
|
73
100
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
*/
|
|
78
|
-
export class Roggel implements IRoggel {
|
|
79
|
-
constructor(private transports: (ConsoleTransport | TelemetryTransport)[] = []) { }
|
|
101
|
+
constructor(transports: Transport | Transport[] = []) {
|
|
102
|
+
this.transports = Array.isArray(transports) ? transports : [transports];
|
|
103
|
+
}
|
|
80
104
|
|
|
81
|
-
|
|
105
|
+
addTransport(transport: Transport | Transport[]): void {
|
|
82
106
|
const transportArr = Array.isArray(transport) ? transport : [transport];
|
|
83
107
|
this.transports.push(...transportArr);
|
|
84
108
|
}
|
|
85
109
|
|
|
86
|
-
|
|
110
|
+
getTransports(): Transport[] {
|
|
87
111
|
return this.transports;
|
|
88
112
|
}
|
|
89
113
|
|
|
90
|
-
|
|
114
|
+
private processLog(levelName: LogLevel, message: string, args: any[]): void {
|
|
91
115
|
const levelValue = LEVELS[levelName];
|
|
92
|
-
const payload:
|
|
116
|
+
const payload: LogPayload = {
|
|
93
117
|
level: levelName,
|
|
94
|
-
timestamp: new Date().toISOString()
|
|
118
|
+
timestamp: new Date().toISOString(),
|
|
95
119
|
message,
|
|
96
120
|
args
|
|
97
|
-
}
|
|
121
|
+
};
|
|
122
|
+
|
|
98
123
|
this.transports.forEach(transport => {
|
|
99
124
|
transport.handle(levelName, levelValue, payload);
|
|
100
125
|
});
|
|
101
126
|
}
|
|
102
127
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
128
|
+
debug(message: string, ...args: any[]): void { this.processLog('debug', message, args); }
|
|
129
|
+
info(message: string, ...args: any[]): void { this.processLog('info', message, args); }
|
|
130
|
+
warn(message: string, ...args: any[]): void { this.processLog('warn', message, args); }
|
|
131
|
+
error(message: string, ...args: any[]): void { this.processLog('error', message, args); }
|
|
132
|
+
fatal(message: string, ...args: any[]): void { this.processLog('fatal', message, args); }
|
|
133
|
+
}
|