pols-logger 1.1.0 → 1.1.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.
@@ -15,7 +15,7 @@
15
15
  "runtimeArgs": [
16
16
  "run",
17
17
  "test",
18
- "test/stringify.ts"
18
+ "test/logger.ts"
19
19
  ]
20
20
  }
21
21
  ]
package/dist/index.d.ts CHANGED
@@ -22,8 +22,8 @@ export type PLoggerShowInConfig = {
22
22
  export type PLoggerParams = {
23
23
  destinationPath?: string;
24
24
  fileName?: ({ theme, now }: {
25
- theme?: PThemes;
26
- now?: Date;
25
+ theme: PThemes;
26
+ now: Date;
27
27
  }) => string;
28
28
  showIn?: PLoggerShowInConfig;
29
29
  };
@@ -1 +1 @@
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,E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,KAAK,CAAC,MAAM,EAAE,gBAAgB;IAI9B,MAAM,CAAC,MAAM,EAAE,gBAAgB;IAI/B,KAAK,CAAC,MAAM,EAAE,gBAAgB;CAG9B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pols-logger",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
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-date": "^1.5.1",
34
- "pols-utils": "^5.9.0"
33
+ "pols-utils": "^5.9.3"
35
34
  }
36
35
  }
package/src/index.ts DELETED
@@ -1,174 +0,0 @@
1
- import fs from 'fs'
2
- import path from 'path'
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
- }
13
-
14
- export type PLoggerShowInParams = boolean | {
15
- info?: boolean
16
- warning?: boolean
17
- error?: boolean
18
- debug?: boolean
19
- system?: boolean
20
- fatal?: boolean
21
- }
22
-
23
- export type PLoggerShowInConfig = {
24
- console?: PLoggerShowInParams
25
- file?: PLoggerShowInParams
26
- }
27
-
28
- export type PLoggerParams = {
29
- destinationPath?: string
30
- fileName?: ({ theme, now }: {
31
- theme?: PThemes
32
- now?: Date
33
- }) => string
34
- showIn?: PLoggerShowInConfig
35
- }
36
-
37
- export type PLoggerLogParams = {
38
- label: string
39
- description?: string
40
- tags?: string[]
41
- body?: string | PRecord | unknown[] | Error
42
- exit?: boolean
43
- }
44
-
45
- const check = (theme: PThemes, value: null | undefined | PLoggerShowInParams, def: boolean) => {
46
- if (value == null) return def
47
- if (typeof value == 'boolean') return value
48
- switch (theme) {
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
55
- }
56
- }
57
-
58
- const logger = (theme: PThemes, pLogger: PLogger, { label, description, body, exit = false, tags }: PLoggerLogParams, executeEvent = true) => {
59
- const now = new Date
60
- const nowString = PUtilsDate.format(now, '@dd/@mm/@y @hh:@ii:@ss.@lll')
61
-
62
- const headers: string[] = [`[${theme}]`, nowString, '::', label]
63
- if (description) headers.push('::', description)
64
- if (tags?.length) headers.push(...tags.map(tag => `[${tag}]`))
65
-
66
- const textBody: string[] = []
67
- if (body instanceof Array) {
68
- textBody.push(...body.map(b => {
69
- if (b instanceof Error) {
70
- return [
71
- 'Error: ' + b.message,
72
- b.stack.replace(/^Error.*?\n/, '')
73
- ]
74
- } else if (typeof b == 'string') {
75
- return b
76
- } else if (typeof b == 'number') {
77
- return b.toString()
78
- } else if (b == null) {
79
- return ''
80
- } else {
81
- b.toString()
82
- }
83
- }).flat())
84
- } else if (body instanceof Error) {
85
- textBody.push(
86
- 'Error: ' + body.message,
87
- body.stack.replace(/^Error.*?\n/, '')
88
- )
89
- } else if (typeof body == 'string') {
90
- textBody.push(body)
91
- }
92
-
93
- /* Por defecto, muestra el mensaje en consola */
94
- if (check(theme, pLogger.showIn?.console, true)) {
95
- if ([].includes(theme)) {
96
- console.error(headers.join(' '))
97
- if (textBody.length) console.error(textBody.join('\n'))
98
- } else {
99
- console.log(headers.join(' '))
100
- if (textBody.length) console.log(textBody.join('\n'))
101
- }
102
- }
103
-
104
- /* Mensaje en archivo */
105
- if (check(theme, pLogger.showIn?.file, false)) {
106
- const fileName = pLogger.fileName?.({ theme, now }) ?? `LOG_${PUtilsDate.format(now, '@y-@mm-@dd')}.log`
107
- if (!pLogger.destinationPath) throw new Error(`La propiedad 'destinationPath' es requerida si la entrada debe ir a un archivo`)
108
- const filePath = path.join(pLogger.destinationPath, fileName)
109
-
110
- if (!fs.existsSync(pLogger.destinationPath)) {
111
- /* Si no existe la carpeta para los logs, se intentará crear automáticamente */
112
- try {
113
- fs.mkdirSync(pLogger.destinationPath, { recursive: true })
114
- } catch (error) {
115
- throw new Error(`No fue posible crear el directorio '${pLogger.destinationPath}': ${error.message}`)
116
- }
117
- }
118
-
119
- try {
120
- fs.appendFileSync(filePath, `${headers.join(' ')}${textBody.length ? `\n${textBody.join('\n')}` : ''}\n`, { encoding: 'utf-8' })
121
- } catch (error) {
122
- throw new Error(`No fue posible registrar la entrada en el archivo '${filePath}': ${error.message}`)
123
- }
124
- }
125
-
126
- /* Ejecuta el evento del pLogger */
127
- if (executeEvent) {
128
- try {
129
- pLogger.onEntryFinish?.({ label, description, body, exit, tags })
130
- } catch (error) {
131
- logger(PThemes.ERROR, pLogger, { label: 'PLOGGER', description: 'Error al ejecutar el evento "onEntryFinish"', body: error }, false)
132
- }
133
- }
134
-
135
- /* Si se ha dado la opción, se sale del programa */
136
- if (exit) process.exit()
137
- }
138
-
139
- export class PLogger {
140
- destinationPath?: string
141
- showIn?: PLoggerShowInConfig
142
- fileName?: PLoggerParams['fileName']
143
- declare onEntryFinish?: (params: PLoggerLogParams) => void
144
-
145
- constructor(params?: PLoggerParams) {
146
- this.destinationPath = params?.destinationPath
147
- this.showIn = params?.showIn
148
- this.fileName = params?.fileName
149
- }
150
-
151
- info(params: PLoggerLogParams) {
152
- logger(PThemes.INFO, this, params)
153
- }
154
-
155
- warning(params: PLoggerLogParams) {
156
- logger(PThemes.WARNING, this, params)
157
- }
158
-
159
- error(params: PLoggerLogParams) {
160
- logger(PThemes.ERROR, this, params)
161
- }
162
-
163
- debug(params: PLoggerLogParams) {
164
- logger(PThemes.DEBUG, this, params)
165
- }
166
-
167
- system(params: PLoggerLogParams) {
168
- logger(PThemes.SYSTEM, this, params)
169
- }
170
-
171
- fatal(params: PLoggerLogParams) {
172
- logger(PThemes.FATAL, this, { ...params, exit: true })
173
- }
174
- }