@adatechnology/logger 0.0.10 → 0.0.12

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 CHANGED
@@ -90,6 +90,20 @@ interface LoggerConfig extends WinstonModuleConfig {
90
90
  * Suporta prefixo exato ou parcial via startsWith
91
91
  */
92
92
  interceptorExcludedPaths?: string[];
93
+ /**
94
+ * Habilita rastreamento de stack de chamadas nos logs (default: false)
95
+ */
96
+ enableTraceStack?: boolean;
97
+ /**
98
+ * Configuração de file transport para os logs
99
+ */
100
+ fileTransport?: {
101
+ enabled: boolean;
102
+ dir?: string;
103
+ filename?: string;
104
+ maxSize?: string;
105
+ maxFiles?: string;
106
+ };
93
107
  }
94
108
  declare const DEFAULT_LOGGER_CONFIG: LoggerConfig;
95
109
 
@@ -137,9 +151,14 @@ declare const HTTP_LOGGING_INTERCEPTOR_CONTEXT: {
137
151
 
138
152
  declare const ExcludeHttpLogging: () => _nestjs_common.CustomDecorator<string>;
139
153
 
140
- type RequestContext = Record<string, unknown> | undefined;
154
+ type RequestContext = (Record<string, unknown> & {
155
+ traceStack?: string[];
156
+ }) | undefined;
141
157
 
142
158
  declare function getContext(): RequestContext;
143
159
  declare function runWithContext<T>(ctx: Record<string, unknown>, fn: () => T): T;
160
+ declare function getTraceStack(): string[];
161
+ declare function pushToTraceStack(method: string): void;
162
+ declare function popFromTraceStack(): void;
144
163
 
145
- export { DEFAULT_LOGGER_CONFIG, type DebugParams, type DebugResult, type ErrorParams, type ErrorResult, ExcludeHttpLogging, HTTP_LOGGING_INTERCEPTOR, HTTP_LOGGING_INTERCEPTOR_CONTEXT, HttpLoggingInterceptor, type InfoParams, type InfoResult, LOGGER_CONFIG, LOGGER_PROVIDER, type LogParams, type LogResult, type LoggerConfig, LoggerLevel, LoggerModule, type LoggerProviderInterface, RequestContextMiddleware, type WarnParams, type WarnResult, type WriteLogParams, type WriteLogResult, getContext, runWithContext };
164
+ export { DEFAULT_LOGGER_CONFIG, type DebugParams, type DebugResult, type ErrorParams, type ErrorResult, ExcludeHttpLogging, HTTP_LOGGING_INTERCEPTOR, HTTP_LOGGING_INTERCEPTOR_CONTEXT, HttpLoggingInterceptor, type InfoParams, type InfoResult, LOGGER_CONFIG, LOGGER_PROVIDER, type LogParams, type LogResult, type LoggerConfig, LoggerLevel, LoggerModule, type LoggerProviderInterface, RequestContextMiddleware, type WarnParams, type WarnResult, type WriteLogParams, type WriteLogResult, getContext, getTraceStack, popFromTraceStack, pushToTraceStack, runWithContext };
package/dist/index.js CHANGED
@@ -9008,6 +9008,9 @@ __export(index_exports, {
9008
9008
  LoggerModule: () => LoggerModule,
9009
9009
  RequestContextMiddleware: () => RequestContextMiddleware,
9010
9010
  getContext: () => getContext,
9011
+ getTraceStack: () => getTraceStack,
9012
+ popFromTraceStack: () => popFromTraceStack,
9013
+ pushToTraceStack: () => pushToTraceStack,
9011
9014
  runWithContext: () => runWithContext
9012
9015
  });
9013
9016
  module.exports = __toCommonJS(index_exports);
@@ -9110,6 +9113,30 @@ function runWithContext(ctx, fn) {
9110
9113
  return asyncLocalStorage.run(ctx, fn);
9111
9114
  }
9112
9115
  __name(runWithContext, "runWithContext");
9116
+ function getTraceStack() {
9117
+ const ctx = getContext();
9118
+ if (!ctx) return [];
9119
+ if (!ctx.traceStack) {
9120
+ ctx.traceStack = [];
9121
+ }
9122
+ return ctx.traceStack;
9123
+ }
9124
+ __name(getTraceStack, "getTraceStack");
9125
+ function pushToTraceStack(method) {
9126
+ const ctx = getContext();
9127
+ if (!ctx) return;
9128
+ if (!ctx.traceStack) {
9129
+ ctx.traceStack = [];
9130
+ }
9131
+ ctx.traceStack.push(method);
9132
+ }
9133
+ __name(pushToTraceStack, "pushToTraceStack");
9134
+ function popFromTraceStack() {
9135
+ const ctx = getContext();
9136
+ if (!ctx || !ctx.traceStack) return;
9137
+ ctx.traceStack.pop();
9138
+ }
9139
+ __name(popFromTraceStack, "popFromTraceStack");
9113
9140
 
9114
9141
  // src/logger.constant.ts
9115
9142
  var DEFAULT_SENSITIVE_KEYS = [
@@ -9160,10 +9187,12 @@ var WinstonLoggerProvider = class {
9160
9187
  }
9161
9188
  logger;
9162
9189
  obfuscator;
9190
+ config;
9163
9191
  context;
9164
- constructor(logger, obfuscator) {
9192
+ constructor(logger, obfuscator, config) {
9165
9193
  this.logger = logger;
9166
9194
  this.obfuscator = obfuscator;
9195
+ this.config = config;
9167
9196
  }
9168
9197
  debug(payload) {
9169
9198
  this.log({
@@ -9218,6 +9247,12 @@ var WinstonLoggerProvider = class {
9218
9247
  requestId: requestIdFromContext || rest.requestId,
9219
9248
  meta: obfuscatedMeta
9220
9249
  };
9250
+ if (this.config?.enableTraceStack) {
9251
+ const traceStack = getTraceStack();
9252
+ if (traceStack.length > 0) {
9253
+ logInfo.traceStack = traceStack;
9254
+ }
9255
+ }
9221
9256
  this.logger.log(level, messageText, logInfo);
9222
9257
  }
9223
9258
  };
@@ -9225,10 +9260,12 @@ WinstonLoggerProvider = _ts_decorate2([
9225
9260
  (0, import_common2.Injectable)(),
9226
9261
  _ts_param2(0, (0, import_common2.Inject)(WINSTON_RAW)),
9227
9262
  _ts_param2(1, (0, import_common2.Inject)(WINSTON_OBFUSCATOR)),
9263
+ _ts_param2(2, (0, import_common2.Inject)("LOGGER_CONFIG")),
9228
9264
  _ts_metadata2("design:type", Function),
9229
9265
  _ts_metadata2("design:paramtypes", [
9230
9266
  typeof import_winston.Logger === "undefined" ? Object : import_winston.Logger,
9231
- typeof Obfuscator === "undefined" ? Object : Obfuscator
9267
+ typeof Obfuscator === "undefined" ? Object : Obfuscator,
9268
+ typeof LoggerConfig === "undefined" ? Object : LoggerConfig
9232
9269
  ])
9233
9270
  ], WinstonLoggerProvider);
9234
9271
 
@@ -9355,7 +9392,8 @@ function fillInfoFromMeta(info) {
9355
9392
  "libVersion",
9356
9393
  "libMethod",
9357
9394
  "appName",
9358
- "appVersion"
9395
+ "appVersion",
9396
+ "traceStack"
9359
9397
  ];
9360
9398
  for (const key of metadataKeys) {
9361
9399
  if (meta[key] && !info[key]) {
@@ -9440,6 +9478,7 @@ function formatDevelopmentLog(infoInput, useColors, levelColorizer) {
9440
9478
  const libMethod = asString(info.libMethod);
9441
9479
  const libVersion = asString(info.libVersion);
9442
9480
  const stack = asString(info.stack);
9481
+ const traceStack = Array.isArray(info.traceStack) ? info.traceStack : void 0;
9443
9482
  const meta = info.meta && typeof info.meta === "object" ? info.meta : void 0;
9444
9483
  const colors = {
9445
9484
  reset: "\x1B[0m",
@@ -9471,7 +9510,12 @@ function formatDevelopmentLog(infoInput, useColors, levelColorizer) {
9471
9510
  libMethod,
9472
9511
  colorize: /* @__PURE__ */ __name((text) => colorizeText(useColors, colors.magenta, colors.reset, text), "colorize")
9473
9512
  });
9474
- let output = `${appDisplay}${libDisplay}[${coloredRequestId}][${coloredTime}]${sourceDisplay}${libMethodDisplay}[${coloredLevel}] - ${message}`;
9513
+ let traceStackDisplay = "";
9514
+ if (traceStack && traceStack.length > 0) {
9515
+ const stackItems = traceStack.map((item) => `[${colorizeText(useColors, colors.magenta, colors.reset, item)}]`).join("");
9516
+ traceStackDisplay = stackItems;
9517
+ }
9518
+ let output = `${appDisplay}${libDisplay}[${coloredRequestId}][${coloredTime}]${sourceDisplay}${traceStackDisplay}${libMethodDisplay}[${coloredLevel}] - ${message}`;
9475
9519
  if (meta && typeof meta === "object" && Object.keys(meta).length > 0) {
9476
9520
  const inspectedMeta = (0, import_node_util.inspect)(meta, {
9477
9521
  colors: useColors,
@@ -9545,6 +9589,10 @@ var WinstonImplementationModule = class _WinstonImplementationModule {
9545
9589
  const winstonLogger = this.createWinstonLogger(config);
9546
9590
  const obfuscator = config?.obfuscator ?? buildDefaultObfuscator(config?.obfuscatorKeys);
9547
9591
  return [
9592
+ {
9593
+ provide: "LOGGER_CONFIG",
9594
+ useValue: config
9595
+ },
9548
9596
  {
9549
9597
  provide: WINSTON_RAW,
9550
9598
  useValue: winstonLogger
@@ -9587,12 +9635,29 @@ var WinstonImplementationModule = class _WinstonImplementationModule {
9587
9635
  const consoleTransport = new import_winston2.transports.Console({
9588
9636
  format: defaultFormat
9589
9637
  });
9638
+ const transportsList = [
9639
+ consoleTransport
9640
+ ];
9641
+ if (config?.fileTransport?.enabled) {
9642
+ try {
9643
+ const DailyRotateFile = require("winston-daily-rotate-file");
9644
+ const fileTransport = new DailyRotateFile({
9645
+ dirname: config.fileTransport.dir ?? "logs",
9646
+ filename: config.fileTransport.filename ?? "app-%DATE%.log",
9647
+ datePattern: "YYYY-MM-DD",
9648
+ maxSize: config.fileTransport.maxSize ?? "20m",
9649
+ maxFiles: config.fileTransport.maxFiles ?? "14d",
9650
+ format: isProduction ? import_winston2.format.json() : developmentFormat
9651
+ });
9652
+ transportsList.push(fileTransport);
9653
+ } catch (error) {
9654
+ console.warn("winston-daily-rotate-file not installed, skipping file transport");
9655
+ }
9656
+ }
9590
9657
  const defaultOptions = {
9591
9658
  level: config?.level || process.env.LOG_LEVEL || DEFAULT_LOG_LEVEL,
9592
9659
  format: defaultFormat,
9593
- transports: [
9594
- consoleTransport
9595
- ]
9660
+ transports: transportsList
9596
9661
  };
9597
9662
  const mergedOptions = config?.loggerOptions ? {
9598
9663
  ...defaultOptions,
@@ -9878,5 +9943,8 @@ var DEFAULT_LOGGER_CONFIG = {
9878
9943
  LoggerModule,
9879
9944
  RequestContextMiddleware,
9880
9945
  getContext,
9946
+ getTraceStack,
9947
+ popFromTraceStack,
9948
+ pushToTraceStack,
9881
9949
  runWithContext
9882
9950
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adatechnology/logger",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -16,7 +16,8 @@
16
16
  "tsup": "^8.5.1"
17
17
  },
18
18
  "dependencies": {
19
- "winston": "^3.9.0"
19
+ "winston": "^3.9.0",
20
+ "winston-daily-rotate-file": "^5.0.0"
20
21
  },
21
22
  "scripts": {
22
23
  "prebuild": "rm -rf dist",
@@ -15,3 +15,27 @@ export function runWithContext<T>(
15
15
  ): T {
16
16
  return asyncLocalStorage.run(ctx, fn as any);
17
17
  }
18
+
19
+ export function getTraceStack(): string[] {
20
+ const ctx = getContext();
21
+ if (!ctx) return [];
22
+ if (!ctx.traceStack) {
23
+ ctx.traceStack = [];
24
+ }
25
+ return ctx.traceStack as string[];
26
+ }
27
+
28
+ export function pushToTraceStack(method: string): void {
29
+ const ctx = getContext();
30
+ if (!ctx) return;
31
+ if (!ctx.traceStack) {
32
+ ctx.traceStack = [];
33
+ }
34
+ (ctx.traceStack as string[]).push(method);
35
+ }
36
+
37
+ export function popFromTraceStack(): void {
38
+ const ctx = getContext();
39
+ if (!ctx || !ctx.traceStack) return;
40
+ (ctx.traceStack as string[]).pop();
41
+ }
@@ -1 +1,3 @@
1
- export type RequestContext = Record<string, unknown> | undefined;
1
+ export type RequestContext = (Record<string, unknown> & {
2
+ traceStack?: string[];
3
+ }) | undefined;
@@ -25,6 +25,7 @@ type WritableLogInfo = Record<string, unknown> & {
25
25
  libMethod?: unknown;
26
26
  stack?: unknown;
27
27
  meta?: unknown;
28
+ traceStack?: unknown;
28
29
  };
29
30
 
30
31
  type MetaLogContext = {
@@ -62,6 +63,7 @@ function fillInfoFromMeta(info: WritableLogInfo): void {
62
63
  "libMethod",
63
64
  "appName",
64
65
  "appVersion",
66
+ "traceStack",
65
67
  ] as const;
66
68
 
67
69
  for (const key of metadataKeys) {
@@ -157,6 +159,7 @@ function formatDevelopmentLog(
157
159
  const libMethod = asString(info.libMethod);
158
160
  const libVersion = asString(info.libVersion);
159
161
  const stack = asString(info.stack);
162
+ const traceStack = Array.isArray(info.traceStack) ? info.traceStack : undefined;
160
163
 
161
164
  const meta =
162
165
  info.meta && typeof info.meta === "object"
@@ -212,7 +215,15 @@ function formatDevelopmentLog(
212
215
  colorizeText(useColors, colors.magenta, colors.reset, text),
213
216
  });
214
217
 
215
- let output = `${appDisplay}${libDisplay}[${coloredRequestId}][${coloredTime}]${sourceDisplay}${libMethodDisplay}[${coloredLevel}] - ${message}`;
218
+ let traceStackDisplay = "";
219
+ if (traceStack && traceStack.length > 0) {
220
+ const stackItems = traceStack
221
+ .map((item) => `[${colorizeText(useColors, colors.magenta, colors.reset, item)}]`)
222
+ .join("");
223
+ traceStackDisplay = stackItems;
224
+ }
225
+
226
+ let output = `${appDisplay}${libDisplay}[${coloredRequestId}][${coloredTime}]${sourceDisplay}${traceStackDisplay}${libMethodDisplay}[${coloredLevel}] - ${message}`;
216
227
 
217
228
  if (meta && typeof meta === "object" && Object.keys(meta).length > 0) {
218
229
  const inspectedMeta = inspect(meta, {
@@ -290,6 +301,10 @@ export class WinstonImplementationModule {
290
301
  config?.obfuscator ?? buildDefaultObfuscator(config?.obfuscatorKeys);
291
302
 
292
303
  return [
304
+ {
305
+ provide: "LOGGER_CONFIG",
306
+ useValue: config,
307
+ },
293
308
  {
294
309
  provide: WINSTON_RAW,
295
310
  useValue: winstonLogger,
@@ -344,11 +359,32 @@ export class WinstonImplementationModule {
344
359
  format: defaultFormat,
345
360
  });
346
361
 
362
+ const transportsList: any[] = [consoleTransport];
363
+
364
+ if (config?.fileTransport?.enabled) {
365
+ try {
366
+ const DailyRotateFile = require("winston-daily-rotate-file");
367
+ const fileTransport = new DailyRotateFile({
368
+ dirname: config.fileTransport.dir ?? "logs",
369
+ filename: config.fileTransport.filename ?? "app-%DATE%.log",
370
+ datePattern: "YYYY-MM-DD",
371
+ maxSize: config.fileTransport.maxSize ?? "20m",
372
+ maxFiles: config.fileTransport.maxFiles ?? "14d",
373
+ format: isProduction ? format.json() : developmentFormat,
374
+ });
375
+ transportsList.push(fileTransport);
376
+ } catch (error) {
377
+ console.warn(
378
+ "winston-daily-rotate-file not installed, skipping file transport",
379
+ );
380
+ }
381
+ }
382
+
347
383
  const defaultOptions: LoggerOptions = {
348
384
  level:
349
385
  config?.level || (process.env.LOG_LEVEL as any) || DEFAULT_LOG_LEVEL,
350
386
  format: defaultFormat,
351
- transports: [consoleTransport],
387
+ transports: transportsList,
352
388
  };
353
389
 
354
390
  const mergedOptions: LoggerOptions = config?.loggerOptions
@@ -15,9 +15,10 @@ import {
15
15
  type WriteLogResult,
16
16
  } from "../../logger.interface";
17
17
  import type { Obfuscator } from "./winston.logger.types";
18
- import { getContext } from "../../context/async-context.service";
18
+ import { getContext, getTraceStack } from "../../context/async-context.service";
19
19
  import { EMPTY_STRING } from "../../logger.constant";
20
20
  import { WINSTON_RAW, WINSTON_OBFUSCATOR } from "./winston.logger.token";
21
+ import type { LoggerConfig } from "../../logger.config";
21
22
 
22
23
  @Injectable()
23
24
  export class WinstonLoggerProvider implements LoggerProviderInterface {
@@ -26,6 +27,7 @@ export class WinstonLoggerProvider implements LoggerProviderInterface {
26
27
  constructor(
27
28
  @Inject(WINSTON_RAW) private readonly logger: WinstonLoggerType,
28
29
  @Inject(WINSTON_OBFUSCATOR) private readonly obfuscator?: Obfuscator,
30
+ @Inject("LOGGER_CONFIG") private readonly config?: LoggerConfig,
29
31
  ) {}
30
32
 
31
33
  debug(payload: DebugParams): DebugResult {
@@ -92,6 +94,13 @@ export class WinstonLoggerProvider implements LoggerProviderInterface {
92
94
  meta: obfuscatedMeta,
93
95
  };
94
96
 
97
+ if (this.config?.enableTraceStack) {
98
+ const traceStack = getTraceStack();
99
+ if (traceStack.length > 0) {
100
+ logInfo.traceStack = traceStack;
101
+ }
102
+ }
103
+
95
104
  this.logger.log(level as unknown as string, messageText, logInfo);
96
105
  }
97
106
  }
package/src/index.ts CHANGED
@@ -20,6 +20,12 @@ export { RequestContextMiddleware } from "./middleware/request-context.middlewar
20
20
  export { HttpLoggingInterceptor } from "./interceptors/http-logging.interceptor";
21
21
  export { HTTP_LOGGING_INTERCEPTOR_CONTEXT } from "./interceptors/http-logging.interceptor.constant";
22
22
  export { ExcludeHttpLogging } from "./interceptors/exclude-http-logging.decorator";
23
- export { getContext, runWithContext } from "./context/async-context.service";
23
+ export {
24
+ getContext,
25
+ runWithContext,
26
+ getTraceStack,
27
+ pushToTraceStack,
28
+ popFromTraceStack,
29
+ } from "./context/async-context.service";
24
30
  export type { LoggerConfig } from "./logger.config";
25
31
  export { DEFAULT_LOGGER_CONFIG } from "./logger.config";
@@ -52,6 +52,22 @@ export interface LoggerConfig extends WinstonModuleConfig {
52
52
  * Suporta prefixo exato ou parcial via startsWith
53
53
  */
54
54
  interceptorExcludedPaths?: string[];
55
+
56
+ /**
57
+ * Habilita rastreamento de stack de chamadas nos logs (default: false)
58
+ */
59
+ enableTraceStack?: boolean;
60
+
61
+ /**
62
+ * Configuração de file transport para os logs
63
+ */
64
+ fileTransport?: {
65
+ enabled: boolean;
66
+ dir?: string;
67
+ filename?: string;
68
+ maxSize?: string;
69
+ maxFiles?: string;
70
+ };
55
71
  }
56
72
 
57
73
  export const DEFAULT_LOGGER_CONFIG: LoggerConfig = {