@navios/core 1.1.0 → 2.0.0-alpha.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.
Files changed (237) hide show
  1. package/CHANGELOG.md +85 -13
  2. package/README.md +69 -220
  3. package/lib/async-context-BAdt7bno.d.mts +239 -0
  4. package/lib/async-context-BAdt7bno.d.mts.map +1 -0
  5. package/lib/async-context-RsWmy2Hi.d.cts +239 -0
  6. package/lib/async-context-RsWmy2Hi.d.cts.map +1 -0
  7. package/lib/attribute-factory-BW1kgBqf.d.mts +70 -0
  8. package/lib/attribute-factory-BW1kgBqf.d.mts.map +1 -0
  9. package/lib/attribute-factory-DqN4x5XM.d.cts +70 -0
  10. package/lib/attribute-factory-DqN4x5XM.d.cts.map +1 -0
  11. package/lib/attribute-value-BNiJjxl1.mjs +225 -0
  12. package/lib/attribute-value-BNiJjxl1.mjs.map +1 -0
  13. package/lib/attribute-value-Cux5HhOL.cjs +314 -0
  14. package/lib/attribute-value-Cux5HhOL.cjs.map +1 -0
  15. package/lib/basic-execution-context-5uq2DvEA.cjs +318 -0
  16. package/lib/basic-execution-context-5uq2DvEA.cjs.map +1 -0
  17. package/lib/basic-execution-context-u1VktGoM.mjs +277 -0
  18. package/lib/basic-execution-context-u1VktGoM.mjs.map +1 -0
  19. package/lib/execution-context-BVSmLkYp.d.cts +71 -0
  20. package/lib/execution-context-BVSmLkYp.d.cts.map +1 -0
  21. package/lib/execution-context-C5F9rojO.d.mts +71 -0
  22. package/lib/execution-context-C5F9rojO.d.mts.map +1 -0
  23. package/lib/execution-context-token--cKS4cJv.cjs +270 -0
  24. package/lib/execution-context-token--cKS4cJv.cjs.map +1 -0
  25. package/lib/execution-context-token-Cb3M3Ao0.mjs +223 -0
  26. package/lib/execution-context-token-Cb3M3Ao0.mjs.map +1 -0
  27. package/lib/index.cjs +1672 -4426
  28. package/lib/index.cjs.map +1 -1
  29. package/lib/index.d.cts +842 -3
  30. package/lib/index.d.cts.map +1 -0
  31. package/lib/index.d.mts +842 -3
  32. package/lib/index.d.mts.map +1 -0
  33. package/lib/index.mjs +1559 -4221
  34. package/lib/index.mjs.map +1 -1
  35. package/lib/legacy-compat/index.cjs +72 -371
  36. package/lib/legacy-compat/index.cjs.map +1 -1
  37. package/lib/legacy-compat/index.d.cts +42 -382
  38. package/lib/legacy-compat/index.d.cts.map +1 -1
  39. package/lib/legacy-compat/index.d.mts +41 -382
  40. package/lib/legacy-compat/index.d.mts.map +1 -1
  41. package/lib/legacy-compat/index.mjs +51 -345
  42. package/lib/legacy-compat/index.mjs.map +1 -1
  43. package/lib/module-Bz83ZnJ5.cjs +173 -0
  44. package/lib/module-Bz83ZnJ5.cjs.map +1 -0
  45. package/lib/module-options-CSqQUSTm.d.mts +51 -0
  46. package/lib/module-options-CSqQUSTm.d.mts.map +1 -0
  47. package/lib/module-options-DSRBri4J.d.cts +51 -0
  48. package/lib/module-options-DSRBri4J.d.cts.map +1 -0
  49. package/lib/module-zPXwR2qp.mjs +138 -0
  50. package/lib/module-zPXwR2qp.mjs.map +1 -0
  51. package/lib/settled-within-B6gpu2XW.cjs +32 -0
  52. package/lib/settled-within-B6gpu2XW.cjs.map +1 -0
  53. package/lib/settled-within-Bc2pj8WB.mjs +21 -0
  54. package/lib/settled-within-Bc2pj8WB.mjs.map +1 -0
  55. package/lib/testing/index.cjs +74 -414
  56. package/lib/testing/index.cjs.map +1 -1
  57. package/lib/testing/index.d.cts +25 -385
  58. package/lib/testing/index.d.cts.map +1 -1
  59. package/lib/testing/index.d.mts +24 -385
  60. package/lib/testing/index.d.mts.map +1 -1
  61. package/lib/testing/index.mjs +72 -408
  62. package/lib/testing/index.mjs.map +1 -1
  63. package/lib/transport.cjs +36 -0
  64. package/lib/transport.d.cts +254 -0
  65. package/lib/transport.d.cts.map +1 -0
  66. package/lib/transport.d.mts +254 -0
  67. package/lib/transport.d.mts.map +1 -0
  68. package/lib/transport.mjs +6 -0
  69. package/package.json +37 -17
  70. package/docs/README.md +0 -316
  71. package/docs/adapters.md +0 -308
  72. package/docs/application-setup.md +0 -524
  73. package/docs/attributes.md +0 -689
  74. package/docs/controllers.md +0 -373
  75. package/docs/endpoints.md +0 -444
  76. package/docs/exceptions.md +0 -352
  77. package/docs/guards.md +0 -550
  78. package/docs/legacy-compat.md +0 -356
  79. package/docs/modules.md +0 -377
  80. package/docs/quick-start.md +0 -290
  81. package/docs/services.md +0 -427
  82. package/docs/testing.md +0 -827
  83. package/lib/index-3LcTrbxz.d.mts +0 -3804
  84. package/lib/index-3LcTrbxz.d.mts.map +0 -1
  85. package/lib/index-B14SekVb.d.cts +0 -3804
  86. package/lib/index-B14SekVb.d.cts.map +0 -1
  87. package/lib/navios.factory-CUrO_p6K.cjs +0 -4538
  88. package/lib/navios.factory-CUrO_p6K.cjs.map +0 -1
  89. package/lib/navios.factory-DjUOOVVL.mjs +0 -4151
  90. package/lib/navios.factory-DjUOOVVL.mjs.map +0 -1
  91. package/lib/tokens-BEuBMGGX.cjs +0 -193
  92. package/lib/tokens-BEuBMGGX.cjs.map +0 -1
  93. package/lib/tokens-COyNGV1I.mjs +0 -97
  94. package/lib/tokens-COyNGV1I.mjs.map +0 -1
  95. package/lib/use-guards.decorator-DLmRl2CV.mjs +0 -336
  96. package/lib/use-guards.decorator-DLmRl2CV.mjs.map +0 -1
  97. package/lib/use-guards.decorator-DhumFTk3.cjs +0 -444
  98. package/lib/use-guards.decorator-DhumFTk3.cjs.map +0 -1
  99. package/project.json +0 -66
  100. package/src/__tests__/attribute.factory.spec.mts +0 -300
  101. package/src/__tests__/config.service.spec.mts +0 -43
  102. package/src/__tests__/console-logger.service.spec.mts +0 -312
  103. package/src/__tests__/controller-resolver.spec.mts +0 -229
  104. package/src/__tests__/controller.spec.mts +0 -58
  105. package/src/__tests__/guard-runner.service.spec.mts +0 -399
  106. package/src/__tests__/logger.service.spec.mts +0 -147
  107. package/src/__tests__/responders.spec.mts +0 -340
  108. package/src/__tests__/testing-module.spec.mts +0 -459
  109. package/src/__tests__/unit-testing-module.spec.mts +0 -424
  110. package/src/attribute.factory.mts +0 -325
  111. package/src/config/config-service.interface.mts +0 -47
  112. package/src/config/config.provider.mts +0 -69
  113. package/src/config/config.service.mts +0 -176
  114. package/src/config/index.mts +0 -5
  115. package/src/config/types.mts +0 -58
  116. package/src/config/utils/helpers.mts +0 -23
  117. package/src/config/utils/index.mts +0 -1
  118. package/src/decorators/controller.decorator.mts +0 -81
  119. package/src/decorators/endpoint.decorator.mts +0 -181
  120. package/src/decorators/header.decorator.mts +0 -38
  121. package/src/decorators/http-code.decorator.mts +0 -38
  122. package/src/decorators/index.mts +0 -8
  123. package/src/decorators/module.decorator.mts +0 -106
  124. package/src/decorators/multipart.decorator.mts +0 -169
  125. package/src/decorators/stream.decorator.mts +0 -155
  126. package/src/decorators/use-guards.decorator.mts +0 -73
  127. package/src/exceptions/bad-request.exception.mts +0 -28
  128. package/src/exceptions/conflict.exception.mts +0 -31
  129. package/src/exceptions/forbidden.exception.mts +0 -30
  130. package/src/exceptions/http.exception.mts +0 -33
  131. package/src/exceptions/index.mts +0 -7
  132. package/src/exceptions/internal-server-error.exception.mts +0 -33
  133. package/src/exceptions/not-found.exception.mts +0 -33
  134. package/src/exceptions/unauthorized.exception.mts +0 -30
  135. package/src/factories/adapter.factory.mts +0 -20
  136. package/src/factories/endpoint-adapter.factory.mts +0 -21
  137. package/src/factories/http-adapter.factory.mts +0 -20
  138. package/src/factories/index.mts +0 -8
  139. package/src/factories/multipart-adapter.factory.mts +0 -21
  140. package/src/factories/reply.factory.mts +0 -21
  141. package/src/factories/request.factory.mts +0 -21
  142. package/src/factories/stream-adapter.factory.mts +0 -20
  143. package/src/factories/xml-stream-adapter.factory.mts +0 -20
  144. package/src/index.mts +0 -16
  145. package/src/interfaces/abstract-adapter.interface.mts +0 -32
  146. package/src/interfaces/abstract-execution-context.inteface.mts +0 -48
  147. package/src/interfaces/abstract-http-adapter.interface.mts +0 -79
  148. package/src/interfaces/abstract-http-cors-options.interface.mts +0 -59
  149. package/src/interfaces/abstract-http-handler-adapter.interface.mts +0 -127
  150. package/src/interfaces/abstract-http-listen-options.interface.mts +0 -4
  151. package/src/interfaces/adapter-environment.interface.mts +0 -74
  152. package/src/interfaces/can-activate.mts +0 -38
  153. package/src/interfaces/http-header.mts +0 -18
  154. package/src/interfaces/index.mts +0 -14
  155. package/src/interfaces/navios-module.mts +0 -28
  156. package/src/interfaces/plugin-context.mts +0 -104
  157. package/src/interfaces/plugin-stage.mts +0 -62
  158. package/src/interfaces/plugin.interface.mts +0 -119
  159. package/src/interfaces/staged-plugin.interface.mts +0 -209
  160. package/src/legacy-compat/__type-tests__/legacy-decorators.spec-d.mts +0 -521
  161. package/src/legacy-compat/__type-tests__/tsconfig.json +0 -15
  162. package/src/legacy-compat/attribute.factory.mts +0 -351
  163. package/src/legacy-compat/decorators/controller.decorator.mts +0 -31
  164. package/src/legacy-compat/decorators/endpoint.decorator.mts +0 -73
  165. package/src/legacy-compat/decorators/header.decorator.mts +0 -43
  166. package/src/legacy-compat/decorators/http-code.decorator.mts +0 -39
  167. package/src/legacy-compat/decorators/index.mts +0 -11
  168. package/src/legacy-compat/decorators/module.decorator.mts +0 -37
  169. package/src/legacy-compat/decorators/multipart.decorator.mts +0 -72
  170. package/src/legacy-compat/decorators/stream.decorator.mts +0 -89
  171. package/src/legacy-compat/decorators/use-guards.decorator.mts +0 -80
  172. package/src/legacy-compat/index.mts +0 -57
  173. package/src/logger/README.md +0 -3
  174. package/src/logger/console-logger.service.mts +0 -714
  175. package/src/logger/index.mts +0 -6
  176. package/src/logger/log-levels.mts +0 -21
  177. package/src/logger/logger-service.interface.mts +0 -42
  178. package/src/logger/logger.service.mts +0 -113
  179. package/src/logger/logger.tokens.mts +0 -44
  180. package/src/logger/utils/cli-colors.util.mts +0 -17
  181. package/src/logger/utils/filter-log-levelts.util.mts +0 -29
  182. package/src/logger/utils/index.mts +0 -5
  183. package/src/logger/utils/is-log-level-enabled.mts +0 -33
  184. package/src/logger/utils/is-log-level.util.mts +0 -10
  185. package/src/logger/utils/shared.utils.mts +0 -51
  186. package/src/metadata/controller.metadata.mts +0 -79
  187. package/src/metadata/handler.metadata.mts +0 -80
  188. package/src/metadata/index.mts +0 -4
  189. package/src/metadata/module.metadata.mts +0 -116
  190. package/src/metadata/navios-managed.metadata.mts +0 -42
  191. package/src/navios.application.mts +0 -678
  192. package/src/navios.environment.mts +0 -42
  193. package/src/navios.factory.mts +0 -158
  194. package/src/responders/enums/framework-error.enum.mts +0 -38
  195. package/src/responders/enums/index.mts +0 -1
  196. package/src/responders/index.mts +0 -4
  197. package/src/responders/interfaces/error-responder.interface.mts +0 -42
  198. package/src/responders/interfaces/error-response.interface.mts +0 -22
  199. package/src/responders/interfaces/index.mts +0 -3
  200. package/src/responders/interfaces/problem-details.interface.mts +0 -34
  201. package/src/responders/services/error-response-producer.service.mts +0 -143
  202. package/src/responders/services/forbidden-responder.service.mts +0 -77
  203. package/src/responders/services/index.mts +0 -5
  204. package/src/responders/services/internal-server-error-responder.service.mts +0 -57
  205. package/src/responders/services/not-found-responder.service.mts +0 -76
  206. package/src/responders/services/validation-error-responder.service.mts +0 -78
  207. package/src/responders/tokens/index.mts +0 -1
  208. package/src/responders/tokens/responder.tokens.mts +0 -84
  209. package/src/services/abstract-handler-adapter.service.mts +0 -366
  210. package/src/services/guard-runner.service.mts +0 -152
  211. package/src/services/index.mts +0 -4
  212. package/src/services/instance-resolver.service.mts +0 -177
  213. package/src/services/module-loader.service.mts +0 -255
  214. package/src/stores/index.mts +0 -1
  215. package/src/stores/request-id.store.mts +0 -85
  216. package/src/testing/index.mts +0 -3
  217. package/src/testing/testing-module.mts +0 -393
  218. package/src/testing/unit-testing-module.mts +0 -298
  219. package/src/tokens/adapter.token.mts +0 -6
  220. package/src/tokens/endpoint-adapter.token.mts +0 -8
  221. package/src/tokens/execution-context.token.mts +0 -9
  222. package/src/tokens/http-adapter.token.mts +0 -8
  223. package/src/tokens/index.mts +0 -10
  224. package/src/tokens/multipart-adapter.token.mts +0 -8
  225. package/src/tokens/navios-options.token.mts +0 -6
  226. package/src/tokens/reply.token.mts +0 -3
  227. package/src/tokens/request.token.mts +0 -3
  228. package/src/tokens/stream-adapter.token.mts +0 -8
  229. package/src/tokens/xml-stream-adapter.token.mts +0 -8
  230. package/src/utils/adapter-supports.util.mts +0 -47
  231. package/src/utils/define-plugin.mts +0 -251
  232. package/src/utils/index.mts +0 -2
  233. package/tsconfig.json +0 -21
  234. package/tsconfig.lib.json +0 -8
  235. package/tsconfig.spec.json +0 -15
  236. package/tsdown.config.mts +0 -35
  237. package/vitest.config.mts +0 -15
@@ -1,714 +0,0 @@
1
- import type { InspectOptions } from 'util'
2
-
3
- import { inspect } from 'util'
4
-
5
- import { Injectable } from '@navios/di'
6
-
7
- import type { LogLevel } from './log-levels.mjs'
8
- import type { LoggerService } from './logger-service.interface.mjs'
9
-
10
- import { getRequestId } from '../stores/request-id.store.mjs'
11
- import { LoggerOutput } from './logger.tokens.mjs'
12
- import {
13
- clc,
14
- isFunction,
15
- isLogLevelEnabled,
16
- isPlainObject,
17
- isString,
18
- isUndefined,
19
- yellow,
20
- } from './utils/index.mjs'
21
-
22
- const DEFAULT_DEPTH = 5
23
-
24
- /**
25
- * @publicApi
26
- */
27
- export interface ConsoleLoggerOptions {
28
- /**
29
- * Enabled log levels.
30
- */
31
- logLevels?: LogLevel[]
32
- /**
33
- * If enabled, will print timestamp difference between current and previous log message.
34
- * Note: This option is not used when `json` is enabled.
35
- * @default false
36
- */
37
- showTimeDiff?: boolean
38
- /**
39
- * A prefix to be used for each log message.
40
- * Note: This option is not used when `json` is enabled.
41
- */
42
- prefix?: string
43
- /**
44
- * If true, will print the process ID in the log message.
45
- * Note: This option is not used when `json` is enabled.
46
- * @default true
47
- */
48
- showPid?: boolean
49
- /**
50
- * If true, will print the log level in the log message.
51
- * Note: This option is not used when `json` is enabled.
52
- * @default true
53
- */
54
- showLogLevel?: boolean
55
- /**
56
- * If true, will print the prefix/app name in the log message.
57
- * Note: This option is not used when `json` is enabled.
58
- * @default true
59
- */
60
- showPrefix?: boolean
61
- /**
62
- * If true, will print the context in the log message.
63
- * Note: This option is not used when `json` is enabled.
64
- * @default true
65
- */
66
- showContext?: boolean
67
- /**
68
- * If true, will print the absolute timestamp in the log message.
69
- * Note: This option is not used when `json` is enabled.
70
- * @default true
71
- */
72
- showTimestamp?: boolean
73
- /**
74
- * If enabled, will add a request ID to the log message.
75
- */
76
- requestId?: boolean
77
- /**
78
- * If enabled, will print the log message in JSON format.
79
- */
80
- json?: boolean
81
- /**
82
- * If enabled, will print the log message in color.
83
- * Default true if json is disabled, false otherwise
84
- */
85
- colors?: boolean
86
- /**
87
- * The context of the logger.
88
- */
89
- context?: string
90
- /**
91
- * If enabled, will print the log message in a single line, even if it is an object with multiple properties.
92
- * If set to a number, the most n inner elements are united on a single line as long as all properties fit into breakLength. Short array elements are also grouped together.
93
- * Default true when `json` is enabled, false otherwise.
94
- */
95
- compact?: boolean | number
96
- /**
97
- * Specifies the maximum number of Array, TypedArray, Map, Set, WeakMap, and WeakSet elements to include when formatting.
98
- * Set to null or Infinity to show all elements. Set to 0 or negative to show no elements.
99
- * Ignored when `json` is enabled, colors are disabled, and `compact` is set to true as it produces a parseable JSON output.
100
- * @default 100
101
- */
102
- maxArrayLength?: number
103
- /**
104
- * Specifies the maximum number of characters to include when formatting.
105
- * Set to null or Infinity to show all elements. Set to 0 or negative to show no characters.
106
- * Ignored when `json` is enabled, colors are disabled, and `compact` is set to true as it produces a parseable JSON output.
107
- * @default 10000.
108
- */
109
- maxStringLength?: number
110
- /**
111
- * If enabled, will sort keys while formatting objects.
112
- * Can also be a custom sorting function.
113
- * Ignored when `json` is enabled, colors are disabled, and `compact` is set to true as it produces a parseable JSON output.
114
- * @default false
115
- */
116
- sorted?: boolean | ((a: string, b: string) => number)
117
- /**
118
- * Specifies the number of times to recurse while formatting object. T
119
- * This is useful for inspecting large objects. To recurse up to the maximum call stack size pass Infinity or null.
120
- * Ignored when `json` is enabled, colors are disabled, and `compact` is set to true as it produces a parseable JSON output.
121
- * @default 5
122
- */
123
- depth?: number
124
- /**
125
- * If true, object's non-enumerable symbols and properties are included in the formatted result.
126
- * WeakMap and WeakSet entries are also included as well as user defined prototype properties
127
- * @default false
128
- */
129
- showHidden?: boolean
130
- /**
131
- * The length at which input values are split across multiple lines. Set to Infinity to format the input as a single line (in combination with "compact" set to true).
132
- * Default Infinity when "compact" is true, 80 otherwise.
133
- * Ignored when `json` is enabled, colors are disabled, and `compact` is set to true as it produces a parseable JSON output.
134
- */
135
- breakLength?: number
136
- }
137
-
138
- const DEFAULT_LOG_LEVELS: LogLevel[] = [
139
- 'log',
140
- 'error',
141
- 'warn',
142
- 'debug',
143
- 'verbose',
144
- 'fatal',
145
- ]
146
-
147
- const dateTimeFormatter = new Intl.DateTimeFormat(undefined, {
148
- year: 'numeric',
149
- hour: 'numeric',
150
- minute: 'numeric',
151
- second: 'numeric',
152
- day: '2-digit',
153
- month: '2-digit',
154
- })
155
-
156
- /**
157
- * @publicApi
158
- */
159
- @Injectable({
160
- token: LoggerOutput,
161
- })
162
- export class ConsoleLogger implements LoggerService {
163
- /**
164
- * Creates a new ConsoleLogger instance with the given options.
165
- * This is a convenience method that instantiates and sets up the logger in one call.
166
- */
167
- static create(): ConsoleLogger
168
- static create(context: string): ConsoleLogger
169
- static create(options: ConsoleLoggerOptions): ConsoleLogger
170
- static create(context: string, options: ConsoleLoggerOptions): ConsoleLogger
171
- static create(
172
- contextOrOptions?: string | ConsoleLoggerOptions,
173
- options?: ConsoleLoggerOptions,
174
- ): ConsoleLogger {
175
- const logger = new ConsoleLogger()
176
- if (contextOrOptions !== undefined) {
177
- if (typeof contextOrOptions === 'string') {
178
- logger.setup(contextOrOptions, options ?? {})
179
- } else {
180
- logger.setup(contextOrOptions)
181
- }
182
- } else {
183
- logger.setup()
184
- }
185
- return logger
186
- }
187
-
188
- /**
189
- * The options of the logger.
190
- */
191
- protected options: ConsoleLoggerOptions = {}
192
- /**
193
- * The context of the logger (can be set manually or automatically inferred).
194
- */
195
- protected context?: string
196
- /**
197
- * The original context of the logger (set in the constructor).
198
- */
199
- protected originalContext?: string
200
- /**
201
- * The options used for the "inspect" method.
202
- */
203
- protected inspectOptions: InspectOptions = this.getInspectOptions()
204
- /**
205
- * The last timestamp at which the log message was printed.
206
- */
207
- protected lastTimestampAt?: number
208
-
209
- setup(): void
210
- setup(context: string): void
211
- setup(options: ConsoleLoggerOptions): void
212
- setup(context: string, options: ConsoleLoggerOptions): void
213
- setup(
214
- contextOrOptions?: string | ConsoleLoggerOptions,
215
- options?: ConsoleLoggerOptions,
216
- ) {
217
- // eslint-disable-next-line prefer-const
218
- let [context, opts] = isString(contextOrOptions)
219
- ? [contextOrOptions, options]
220
- : options
221
- ? [undefined, options]
222
- : [contextOrOptions?.context, contextOrOptions]
223
-
224
- opts = opts ?? {}
225
- opts.logLevels ??= DEFAULT_LOG_LEVELS
226
- opts.colors ??= opts.colors ?? (opts.json ? false : true)
227
- opts.prefix ??= 'Navios'
228
-
229
- this.options = opts
230
- this.inspectOptions = this.getInspectOptions()
231
-
232
- if (context) {
233
- this.context = context
234
- this.originalContext = context
235
- }
236
- }
237
-
238
- /**
239
- * Write a 'log' level log, if the configured level allows for it.
240
- * Prints to `stdout` with newline.
241
- */
242
- log(message: any, context?: string): void
243
- log(message: any, ...optionalParams: [...any, string?]): void
244
- log(message: any, ...optionalParams: any[]) {
245
- if (!this.isLevelEnabled('log')) {
246
- return
247
- }
248
- const { messages, context } = this.getContextAndMessagesToPrint([
249
- message,
250
- ...optionalParams,
251
- ])
252
- this.printMessages(messages, context, 'log')
253
- }
254
-
255
- /**
256
- * Write an 'error' level log, if the configured level allows for it.
257
- * Prints to `stderr` with newline.
258
- */
259
- error(message: any, stackOrContext?: string): void
260
- error(message: any, stack?: string, context?: string): void
261
- error(message: any, ...optionalParams: [...any, string?, string?]): void
262
- error(message: any, ...optionalParams: any[]) {
263
- if (!this.isLevelEnabled('error')) {
264
- return
265
- }
266
- const { messages, context, stack } =
267
- this.getContextAndStackAndMessagesToPrint([message, ...optionalParams])
268
-
269
- this.printMessages(messages, context, 'error', undefined, 'stderr', stack)
270
- this.printStackTrace(stack!)
271
- }
272
-
273
- /**
274
- * Write a 'warn' level log, if the configured level allows for it.
275
- * Prints to `stdout` with newline.
276
- */
277
- warn(message: any, context?: string): void
278
- warn(message: any, ...optionalParams: [...any, string?]): void
279
- warn(message: any, ...optionalParams: any[]) {
280
- if (!this.isLevelEnabled('warn')) {
281
- return
282
- }
283
- const { messages, context } = this.getContextAndMessagesToPrint([
284
- message,
285
- ...optionalParams,
286
- ])
287
- this.printMessages(messages, context, 'warn')
288
- }
289
-
290
- /**
291
- * Write a 'debug' level log, if the configured level allows for it.
292
- * Prints to `stdout` with newline.
293
- */
294
- debug(message: any, context?: string): void
295
- debug(message: any, ...optionalParams: [...any, string?]): void
296
- debug(message: any, ...optionalParams: any[]) {
297
- if (!this.isLevelEnabled('debug')) {
298
- return
299
- }
300
- const { messages, context } = this.getContextAndMessagesToPrint([
301
- message,
302
- ...optionalParams,
303
- ])
304
- this.printMessages(messages, context, 'debug')
305
- }
306
-
307
- /**
308
- * Write a 'verbose' level log, if the configured level allows for it.
309
- * Prints to `stdout` with newline.
310
- */
311
- verbose(message: any, context?: string): void
312
- verbose(message: any, ...optionalParams: [...any, string?]): void
313
- verbose(message: any, ...optionalParams: any[]) {
314
- if (!this.isLevelEnabled('verbose')) {
315
- return
316
- }
317
- const { messages, context } = this.getContextAndMessagesToPrint([
318
- message,
319
- ...optionalParams,
320
- ])
321
- this.printMessages(messages, context, 'verbose')
322
- }
323
-
324
- /**
325
- * Write a 'fatal' level log, if the configured level allows for it.
326
- * Prints to `stdout` with newline.
327
- */
328
- fatal(message: any, context?: string): void
329
- fatal(message: any, ...optionalParams: [...any, string?]): void
330
- fatal(message: any, ...optionalParams: any[]) {
331
- if (!this.isLevelEnabled('fatal')) {
332
- return
333
- }
334
- const { messages, context } = this.getContextAndMessagesToPrint([
335
- message,
336
- ...optionalParams,
337
- ])
338
- this.printMessages(messages, context, 'fatal')
339
- }
340
-
341
- /**
342
- * Set log levels
343
- * @param levels log levels
344
- */
345
- setLogLevels(levels: LogLevel[]) {
346
- if (!this.options) {
347
- this.options = {}
348
- }
349
- this.options.logLevels = levels
350
- }
351
-
352
- /**
353
- * Set logger context
354
- * @param context context
355
- */
356
- setContext(context: string) {
357
- this.context = context
358
- }
359
-
360
- /**
361
- * Resets the logger context to the value that was passed in the constructor.
362
- */
363
- resetContext() {
364
- this.context = this.originalContext
365
- }
366
-
367
- isLevelEnabled(level: LogLevel): boolean {
368
- const logLevels = this.options?.logLevels
369
- return isLogLevelEnabled(level, logLevels)
370
- }
371
-
372
- /**
373
- * Gets the current request ID from the AsyncLocalStorage store.
374
- * Only returns a value if the requestId option is enabled.
375
- */
376
- protected getCurrentRequestId(): string | undefined {
377
- if (!this.options.requestId) {
378
- return undefined
379
- }
380
- return getRequestId()
381
- }
382
-
383
- protected getTimestamp(): string {
384
- return dateTimeFormatter.format(Date.now())
385
- }
386
-
387
- protected printMessages(
388
- messages: unknown[],
389
- context = '',
390
- logLevel: LogLevel = 'log',
391
- requestId?: string,
392
- writeStreamType?: 'stdout' | 'stderr',
393
- errorStack?: unknown,
394
- ) {
395
- const resolvedRequestId = requestId ?? this.getCurrentRequestId()
396
- messages.forEach((message) => {
397
- if (this.options.json) {
398
- this.printAsJson(message, {
399
- context,
400
- logLevel,
401
- writeStreamType,
402
- errorStack,
403
- requestId: resolvedRequestId,
404
- })
405
- return
406
- }
407
- const pidMessage = this.formatPid(process.pid)
408
- const contextMessage = this.formatContext(context)
409
- const timestampDiff = this.updateAndGetTimestampDiff()
410
- const formattedLogLevel = logLevel.toUpperCase().padStart(7, ' ')
411
- const formattedMessage = this.formatMessage(
412
- logLevel,
413
- message,
414
- pidMessage,
415
- formattedLogLevel,
416
- contextMessage,
417
- timestampDiff,
418
- resolvedRequestId,
419
- )
420
-
421
- process[writeStreamType ?? 'stdout'].write(formattedMessage)
422
- })
423
- }
424
-
425
- protected printAsJson(
426
- message: unknown,
427
- options: {
428
- context: string
429
- logLevel: LogLevel
430
- writeStreamType?: 'stdout' | 'stderr'
431
- errorStack?: unknown
432
- requestId?: string
433
- },
434
- ) {
435
- type JsonLogObject = {
436
- level: LogLevel
437
- pid: number
438
- timestamp: number
439
- message: unknown
440
- context?: string
441
- stack?: unknown
442
- requestId?: string
443
- }
444
-
445
- const logObject: JsonLogObject = {
446
- level: options.logLevel,
447
- pid: process.pid,
448
- timestamp: Date.now(),
449
- message,
450
- }
451
-
452
- if (options.context) {
453
- logObject.context = options.context
454
- }
455
-
456
- if (options.errorStack) {
457
- logObject.stack = options.errorStack
458
- }
459
- if (this.options.requestId && options.requestId) {
460
- logObject.requestId = options.requestId
461
- }
462
-
463
- const formattedMessage =
464
- !this.options.colors && this.inspectOptions.compact === true
465
- ? JSON.stringify(logObject, this.stringifyReplacer)
466
- : inspect(logObject, this.inspectOptions)
467
- process[options.writeStreamType ?? 'stdout'].write(`${formattedMessage}\n`)
468
- }
469
-
470
- protected formatPid(pid: number) {
471
- const showPrefix = this.options.showPrefix ?? true
472
- const showPid = this.options.showPid ?? true
473
-
474
- if (!showPrefix && !showPid) {
475
- return ''
476
- }
477
-
478
- const prefix = showPrefix ? `[${this.options.prefix}]` : ''
479
- const pidPart = showPid ? `${pid}` : ''
480
- const separator = showPrefix && showPid ? ' ' : ''
481
- const suffix = showPrefix || showPid ? ' - ' : ''
482
-
483
- return `${prefix}${separator}${pidPart}${suffix}`
484
- }
485
-
486
- protected formatContext(context: string): string {
487
- const showContext = this.options.showContext ?? true
488
- if (!context || !showContext) {
489
- return ''
490
- }
491
-
492
- context = `[${context}] `
493
- return this.options.colors ? yellow(context) : context
494
- }
495
-
496
- protected formatMessage(
497
- logLevel: LogLevel,
498
- message: unknown,
499
- pidMessage: string,
500
- formattedLogLevel: string,
501
- contextMessage: string,
502
- timestampDiff: string,
503
- requestId?: string,
504
- ) {
505
- const output = this.stringifyMessage(message, logLevel)
506
- const showLogLevel = this.options.showLogLevel ?? true
507
- const showTimestamp = this.options.showTimestamp ?? true
508
-
509
- pidMessage = this.colorize(pidMessage, logLevel)
510
- formattedLogLevel = showLogLevel
511
- ? this.colorize(formattedLogLevel, logLevel)
512
- : ''
513
-
514
- const timestamp = showTimestamp ? this.getTimestamp() : ''
515
- const requestIdPart = this.getRequestId(requestId)
516
-
517
- // Build the message parts, filtering out empty ones
518
- const parts: string[] = []
519
-
520
- if (pidMessage) parts.push(pidMessage.trimEnd())
521
- if (requestIdPart) parts.push(requestIdPart.trimEnd())
522
- if (timestamp) parts.push(timestamp)
523
- if (formattedLogLevel) parts.push(formattedLogLevel)
524
- if (contextMessage) parts.push(contextMessage.trimEnd())
525
-
526
- const prefix = parts.length > 0 ? parts.join(' ') + ' ' : ''
527
-
528
- return `${prefix}${output}${timestampDiff}\n`
529
- }
530
-
531
- protected getRequestId(requestId?: string) {
532
- if (this.options.requestId && requestId) {
533
- return `(${this.colorize(requestId, 'log')}) `
534
- }
535
- return ''
536
- }
537
-
538
- protected stringifyMessage(message: unknown, logLevel: LogLevel): string {
539
- if (isFunction(message)) {
540
- const messageAsStr = Function.prototype.toString.call(message)
541
- const isClass = messageAsStr.startsWith('class ')
542
- if (isClass) {
543
- // If the message is a class, we will display the class name.
544
- return this.stringifyMessage(message.name, logLevel)
545
- }
546
- // If the message is a non-class function, call it and re-resolve its value.
547
- return this.stringifyMessage(message(), logLevel)
548
- }
549
-
550
- if (typeof message === 'string') {
551
- return this.colorize(message, logLevel)
552
- }
553
-
554
- const outputText = inspect(message, this.inspectOptions)
555
- if (isPlainObject(message)) {
556
- return `Object(${Object.keys(message).length}) ${outputText}`
557
- }
558
- if (Array.isArray(message)) {
559
- return `Array(${message.length}) ${outputText}`
560
- }
561
- return outputText
562
- }
563
-
564
- protected colorize(message: string, logLevel: LogLevel) {
565
- if (!this.options.colors || this.options.json) {
566
- return message
567
- }
568
- const color = this.getColorByLogLevel(logLevel)
569
- return color(message)
570
- }
571
-
572
- protected printStackTrace(stack: string) {
573
- if (!stack || this.options.json) {
574
- return
575
- }
576
- process.stderr.write(`${stack}\n`)
577
- }
578
-
579
- protected updateAndGetTimestampDiff(): string {
580
- const showTimeDiff = this.options?.showTimeDiff ?? false
581
- const includeTimestamp = this.lastTimestampAt && showTimeDiff
582
- const result = includeTimestamp
583
- ? this.formatTimestampDiff(Date.now() - this.lastTimestampAt!)
584
- : ''
585
- this.lastTimestampAt = Date.now()
586
- return result
587
- }
588
-
589
- protected formatTimestampDiff(timestampDiff: number) {
590
- const formattedDiff = ` +${timestampDiff}ms`
591
- return this.options.colors ? yellow(formattedDiff) : formattedDiff
592
- }
593
-
594
- protected getInspectOptions() {
595
- let breakLength = this.options.breakLength
596
- if (typeof breakLength === 'undefined') {
597
- breakLength = this.options.colors
598
- ? this.options.compact
599
- ? Infinity
600
- : undefined
601
- : this.options.compact === false
602
- ? undefined
603
- : Infinity // default breakLength to Infinity if inline is not set and colors is false
604
- }
605
-
606
- const inspectOptions: InspectOptions = {
607
- depth: this.options.depth ?? DEFAULT_DEPTH,
608
- sorted: this.options.sorted,
609
- showHidden: this.options.showHidden,
610
- compact: this.options.compact ?? (this.options.json ? true : false),
611
- colors: this.options.colors,
612
- breakLength,
613
- }
614
-
615
- if (this.options.maxArrayLength) {
616
- inspectOptions.maxArrayLength = this.options.maxArrayLength
617
- }
618
- if (this.options.maxStringLength) {
619
- inspectOptions.maxStringLength = this.options.maxStringLength
620
- }
621
-
622
- return inspectOptions
623
- }
624
-
625
- protected stringifyReplacer(key: string, value: unknown) {
626
- // Mimic util.inspect behavior for JSON logger with compact on and colors off
627
- if (typeof value === 'bigint') {
628
- return value.toString()
629
- }
630
- if (typeof value === 'symbol') {
631
- return value.toString()
632
- }
633
-
634
- if (
635
- value instanceof Map ||
636
- value instanceof Set ||
637
- value instanceof Error
638
- ) {
639
- return `${inspect(value, this.inspectOptions)}`
640
- }
641
- return value
642
- }
643
-
644
- private getContextAndMessagesToPrint(args: unknown[]) {
645
- if (args?.length <= 1) {
646
- return { messages: args, context: this.context }
647
- }
648
- const lastElement = args[args.length - 1]
649
- const isContext = isString(lastElement)
650
- if (!isContext) {
651
- return { messages: args, context: this.context }
652
- }
653
- return {
654
- context: lastElement,
655
- messages: args.slice(0, args.length - 1),
656
- }
657
- }
658
-
659
- private getContextAndStackAndMessagesToPrint(args: unknown[]) {
660
- if (args.length === 2) {
661
- return this.isStackFormat(args[1])
662
- ? {
663
- messages: [args[0]],
664
- stack: args[1] as string,
665
- context: this.context,
666
- }
667
- : {
668
- messages: [args[0]],
669
- context: args[1] as string,
670
- }
671
- }
672
-
673
- const { messages, context } = this.getContextAndMessagesToPrint(args)
674
- if (messages?.length <= 1) {
675
- return { messages, context }
676
- }
677
- const lastElement = messages[messages.length - 1]
678
- const isStack = isString(lastElement)
679
- // https://github.com/nestjs/nest/issues/11074#issuecomment-1421680060
680
- if (!isStack && !isUndefined(lastElement)) {
681
- return { messages, context }
682
- }
683
- return {
684
- stack: lastElement,
685
- messages: messages.slice(0, messages.length - 1),
686
- context,
687
- }
688
- }
689
-
690
- private isStackFormat(stack: unknown) {
691
- if (!isString(stack) && !isUndefined(stack)) {
692
- return false
693
- }
694
-
695
- return /^(.)+\n\s+at .+:\d+:\d+/.test(stack!)
696
- }
697
-
698
- private getColorByLogLevel(level: LogLevel) {
699
- switch (level) {
700
- case 'debug':
701
- return clc.magentaBright
702
- case 'warn':
703
- return clc.yellow
704
- case 'error':
705
- return clc.red
706
- case 'verbose':
707
- return clc.cyanBright
708
- case 'fatal':
709
- return clc.bold
710
- default:
711
- return clc.green
712
- }
713
- }
714
- }
@@ -1,6 +0,0 @@
1
- export * from './utils/index.mjs'
2
- export * from './console-logger.service.mjs'
3
- export * from './log-levels.mjs'
4
- export * from './logger.tokens.mjs'
5
- export * from './logger.service.mjs'
6
- export * from './logger-service.interface.mjs'