reggolxc 2.0.0 → 2.0.3

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.
@@ -0,0 +1,85 @@
1
+ // 1. Crear wrappers de metodos log nativos
2
+ // 2. Establecer sistema que determine si el livel del log aplica para ser impreso
3
+ // 3. Crear Objetos transports
4
+ // 4. Objetos transports deben recibir un callback
5
+ // 5. Logs en consola deben estar stilizados para navegadores y terminales.
6
+ // 6. Desarrollar con TDD
7
+ export const LEVELS_ENUM = { DEBUG: 'debug', INFO: 'info', WARN: 'warn', ERROR: 'error', FATAL: 'fatal' };
8
+ const LEVELS = { [LEVELS_ENUM.DEBUG]: 1, [LEVELS_ENUM.INFO]: 2, [LEVELS_ENUM.WARN]: 3, [LEVELS_ENUM.ERROR]: 4, [LEVELS_ENUM.FATAL]: 5 };
9
+ const BROWSER_STYLES = {
10
+ debug: 'color: #808080; font-weight: bold;',
11
+ info: 'color: #0088ff; font-weight: bold;',
12
+ warn: 'color: #ffaa00; font-weight: bold;',
13
+ error: 'color: #ff0000; font-weight: bold;',
14
+ fatal: 'color: #ffffff; background-color: #ff0000; font-weight: bold; padding: 2px 4px; border-radius: 3px;'
15
+ };
16
+ const TIME_STYLE = 'color: #a9a9a9; font-weight: normal; font-style: italic;';
17
+ class Transport {
18
+ constructor(minLevelValue = 0) {
19
+ this.minLevelValue = minLevelValue;
20
+ }
21
+ handle(levelName, levelValue, payload) { }
22
+ }
23
+ export class ConsoleTransport extends Transport {
24
+ super(minLevel = LEVELS_ENUM.DEBUG) {
25
+ this.minLevelValue = LEVELS[minLevel];
26
+ }
27
+ handle(levelName, levelValue, payload) {
28
+ if (levelValue < this.minLevelValue)
29
+ return;
30
+ const consoleMethod = (levelName === 'fatal' ? 'error' : levelName);
31
+ console[consoleMethod](`%c[${payload.timestamp}] %c[${levelName.toUpperCase()}]`, TIME_STYLE, BROWSER_STYLES[levelName] || '', payload.message, ...payload.args);
32
+ }
33
+ }
34
+ export class TelemetryTransport extends Transport {
35
+ constructor(fetchFun, minLevelValue = LEVELS_ENUM.ERROR) {
36
+ const minLevel = LEVELS[minLevelValue];
37
+ super(minLevel);
38
+ this.fetchFun = fetchFun;
39
+ }
40
+ handle(levelName, levelValue, payload) {
41
+ if (levelValue < this.minLevelValue)
42
+ return;
43
+ this.fetchFun(payload).catch(err => {
44
+ console.error('Telemetry Transport Failed:', err);
45
+ });
46
+ }
47
+ }
48
+ /**
49
+ * @description Custom logger
50
+ * @param minLevel
51
+ */
52
+ export class Roggel {
53
+ constructor(transports = []) {
54
+ this.transports = transports;
55
+ }
56
+ addTransport(transport) {
57
+ const transportArr = Array.isArray(transport) ? transport : [transport];
58
+ this.transports.push(...transportArr);
59
+ }
60
+ getTransports() {
61
+ return this.transports;
62
+ }
63
+ processLog(levelName, message, args) {
64
+ const levelValue = LEVELS[levelName];
65
+ const payload = {
66
+ level: levelName,
67
+ timestamp: new Date().toISOString(),
68
+ message,
69
+ args
70
+ };
71
+ this.transports.forEach(transport => {
72
+ transport.handle(levelName, levelValue, payload);
73
+ });
74
+ }
75
+ debug(message, ...args) { this.processLog('debug', message, args); }
76
+ ;
77
+ info(message, ...args) { this.processLog('info', message, args); }
78
+ ;
79
+ warn(message, ...args) { this.processLog('warn', message, args); }
80
+ ;
81
+ error(message, ...args) { this.processLog('error', message, args); }
82
+ ;
83
+ fatal(message, ...args) { this.processLog('fatal', message, args); }
84
+ ;
85
+ }
@@ -0,0 +1,30 @@
1
+ import { Roggel, TelemetryTransport, ConsoleTransport, LEVELS_ENUM } from "./console";
2
+ import { describe, expect, it } from "vitest";
3
+ describe('Logger', () => {
4
+ const logger = new Roggel();
5
+ it('should have functions [debug, info, warn, error, fatal]', () => {
6
+ expect(Object.values(LEVELS_ENUM).every((l) => typeof logger[l])).toBe(true);
7
+ });
8
+ it('Addtransport be a fun', () => {
9
+ expect(typeof logger.addTransport).toBe('function');
10
+ });
11
+ it('shoould add new transport', () => {
12
+ const transport = new ConsoleTransport();
13
+ logger.addTransport(transport);
14
+ expect(logger.getTransports().length).toBe(1);
15
+ });
16
+ it('should process log', () => {
17
+ const logger = new Roggel();
18
+ const sendLogService = async (payload) => {
19
+ return new Promise((resolve, reject) => {
20
+ setTimeout(() => {
21
+ resolve(payload);
22
+ }, 2000);
23
+ });
24
+ };
25
+ const telemetry = new TelemetryTransport(sendLogService);
26
+ const console = new ConsoleTransport();
27
+ logger.addTransport([telemetry, console]);
28
+ expect(() => logger.error('print test')).not.toThrow();
29
+ });
30
+ });
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "reggolxc",
3
- "version": "2.0.0",
3
+ "version": "2.0.3",
4
4
  "description": "",
5
- "main": "index.js",
5
+ "main": "dist/console.js",
6
6
  "scripts": {
7
7
  "test": "vitest"
8
8
  },
package/src/console.ts CHANGED
@@ -23,7 +23,7 @@ type consoleKeysT = Exclude<levelsT, 'fatal'>
23
23
 
24
24
  export interface payloadI {
25
25
  level: levelsT,
26
- timestamp: Date,
26
+ timestamp: string,
27
27
  message: string,
28
28
  args: unknown[]
29
29
  }
@@ -89,8 +89,9 @@ export class Roggel implements IRoggel {
89
89
 
90
90
  protected processLog(levelName: levelsT, message: string, args: unknown[]) {
91
91
  const levelValue = LEVELS[levelName];
92
- const payload = {
93
- timestamp: new Date().toISOString(),
92
+ const payload:payloadI = {
93
+ level: levelName,
94
+ timestamp: new Date().toISOString() as string,
94
95
  message,
95
96
  args
96
97
  }
package/tsconfig.json CHANGED
@@ -55,7 +55,7 @@
55
55
  // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
56
56
  // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
57
57
  // "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. */
58
- // "outDir": "./", /* Specify an output folder for all emitted files. */
58
+ "outDir": "./dist", /* Specify an output folder for all emitted files. */
59
59
  // "removeComments": true, /* Disable emitting comments. */
60
60
  // "noEmit": true, /* Disable emitting files from a compilation. */
61
61
  // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */