@nest-yalc-2/logger 1.0.0 → 2.0.0

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 (49) hide show
  1. package/package.json +1 -1
  2. package/src/__tests__/logger.factory.spec.ts +9 -9
  3. package/src/__tests__/logger.helper.spec.ts +1 -1
  4. package/src/index.d.ts +2 -7
  5. package/src/index.js +8 -24
  6. package/src/index.js.map +1 -1
  7. package/src/index.ts +2 -7
  8. package/src/logger-nest.service.d.ts +4 -4
  9. package/src/logger-nest.service.js +3 -3
  10. package/src/logger-nest.service.js.map +1 -1
  11. package/src/logger-nest.service.ts +4 -3
  12. package/src/logger.factory.d.ts +2 -2
  13. package/src/logger.factory.js +12 -47
  14. package/src/logger.factory.js.map +1 -1
  15. package/src/logger.factory.ts +11 -10
  16. package/src/logger.service.d.ts +1 -1
  17. package/src/logger.service.js +1 -1
  18. package/src/logger.service.js.map +1 -1
  19. package/src/logger.service.ts +3 -2
  20. package/src/typeorm-logger.d.ts +1 -1
  21. package/src/typeorm-logger.js +1 -1
  22. package/src/typeorm-logger.js.map +1 -1
  23. package/src/typeorm-logger.ts +2 -2
  24. package/src/__tests__/logger-console.service.spec.ts +0 -136
  25. package/src/__tests__/logger-pino.service.spec.ts +0 -149
  26. package/src/logger-abstract.service.d.ts +0 -65
  27. package/src/logger-abstract.service.js +0 -91
  28. package/src/logger-abstract.service.js.map +0 -1
  29. package/src/logger-abstract.service.ts +0 -210
  30. package/src/logger-console.service.d.ts +0 -5
  31. package/src/logger-console.service.js +0 -23
  32. package/src/logger-console.service.js.map +0 -1
  33. package/src/logger-console.service.ts +0 -80
  34. package/src/logger-pino.service.d.ts +0 -10
  35. package/src/logger-pino.service.js +0 -89
  36. package/src/logger-pino.service.js.map +0 -1
  37. package/src/logger-pino.service.ts +0 -161
  38. package/src/logger.enum.d.ts +0 -17
  39. package/src/logger.enum.js +0 -35
  40. package/src/logger.enum.js.map +0 -1
  41. package/src/logger.enum.ts +0 -32
  42. package/src/logger.event.d.ts +0 -11
  43. package/src/logger.event.js +0 -16
  44. package/src/logger.event.js.map +0 -1
  45. package/src/logger.event.ts +0 -11
  46. package/src/logger.helper.d.ts +0 -4
  47. package/src/logger.helper.js +0 -36
  48. package/src/logger.helper.js.map +0 -1
  49. package/src/logger.helper.ts +0 -40
@@ -1,149 +0,0 @@
1
- import {
2
- expect,
3
- jest,
4
- describe,
5
- it,
6
- beforeEach,
7
- afterEach,
8
- } from '@jest/globals';
9
-
10
- import { Logger } from 'pino';
11
- import { PinoLogger, FLUSH_INTERVAL, flush } from '../index.js';
12
- import { LOG_LEVEL_ALL } from '../logger.enum.js';
13
-
14
- const logOptions = {
15
- context: 'test',
16
- data: { pass: 'data' },
17
- masks: ['pass'],
18
- };
19
-
20
- describe('Pino logger service test', () => {
21
- let pino: Logger;
22
- const container: { logger?: PinoLogger } = {
23
- logger: new PinoLogger('test', LOG_LEVEL_ALL),
24
- };
25
-
26
- beforeEach(async () => {
27
- container.logger = new PinoLogger('test', LOG_LEVEL_ALL);
28
- pino = container.logger?.getLogger();
29
- });
30
-
31
- afterEach(() => {
32
- jest.useRealTimers();
33
- });
34
-
35
- it('Test log', async () => {
36
- const method = jest.spyOn(pino, 'info');
37
- container.logger?.log('test');
38
-
39
- expect(method).toHaveBeenCalled();
40
- });
41
-
42
- it('Test log with options', async () => {
43
- const method = jest.spyOn(pino, 'info');
44
- container.logger?.log('info', logOptions);
45
-
46
- expect(method).toHaveBeenCalled();
47
- });
48
-
49
- it('Test error', async () => {
50
- const method = jest.spyOn(pino, 'error');
51
- container.logger?.error('error', 'trace');
52
-
53
- expect(method).toHaveBeenCalled();
54
- // expect(method).toHaveBeenCalledWith({ context: 'test' }, 'error trace');
55
- });
56
-
57
- it('Test error with options', async () => {
58
- const method = jest.spyOn(pino, 'error');
59
- container.logger?.error('error', undefined, logOptions);
60
-
61
- expect(method).toHaveBeenCalled();
62
- });
63
-
64
- it('Test warn', async () => {
65
- const method = jest.spyOn(pino, 'warn');
66
- container.logger?.warn('warn');
67
-
68
- expect(method).toHaveBeenCalled();
69
- });
70
-
71
- it('Test warn with options', async () => {
72
- const method = jest.spyOn(pino, 'warn');
73
- container.logger?.warn('warn', logOptions);
74
-
75
- expect(method).toHaveBeenCalled();
76
- });
77
-
78
- it('Test debug', async () => {
79
- const method = jest.spyOn(pino, 'debug');
80
- container.logger?.debug?.('debug');
81
-
82
- expect(method).toHaveBeenCalled();
83
- });
84
-
85
- it('Test debug with options', async () => {
86
- const method = jest.spyOn(pino, 'debug');
87
- container.logger?.debug?.('debug', logOptions);
88
-
89
- expect(method).toHaveBeenCalled();
90
- });
91
-
92
- it('Test verbose with options', async () => {
93
- const method = jest.spyOn(pino, 'trace');
94
- container.logger?.verbose?.('verbose', logOptions);
95
-
96
- expect(method).toHaveBeenCalled();
97
- });
98
-
99
- it('Test verbose & flush', async () => {
100
- jest.useFakeTimers();
101
-
102
- jest.mock('process', () => ({
103
- on: jest.fn((fn) => {
104
- fn();
105
- }),
106
- }));
107
-
108
- const _logger = new PinoLogger('test', LOG_LEVEL_ALL);
109
-
110
- const method = jest.spyOn(pino, 'trace');
111
- _logger.verbose?.('verbose');
112
-
113
- // Fast-forward until all timers have been executed
114
- jest.advanceTimersByTime(FLUSH_INTERVAL + 1000);
115
-
116
- expect(method).toHaveBeenCalled();
117
- });
118
-
119
- it('Test on application shutdown', async () => {
120
- const method = jest.spyOn(pino, 'info');
121
- container.logger?.log('test');
122
- await container.logger?.onApplicationShutdown();
123
- expect(method).toHaveBeenCalledWith({ context: 'test' }, 'test');
124
- });
125
-
126
- it('Test flush when logger has been destroyed', async () => {
127
- const method = jest.spyOn(pino, 'flush');
128
- method.mockImplementation((callback: any) => {
129
- callback();
130
- });
131
-
132
- delete container.logger;
133
- await flush();
134
- expect(container.logger).toBeUndefined();
135
- expect(method).toHaveBeenCalled();
136
- });
137
-
138
- it('Test flush with error', async () => {
139
- const method = jest.spyOn(pino, 'flush');
140
- method.mockImplementation((callback: any) => {
141
- callback(new Error('test'));
142
- });
143
-
144
- expect(
145
- async () => await container.logger?.onApplicationShutdown(),
146
- ).rejects.toThrow();
147
- expect(method).toHaveBeenCalled();
148
- });
149
- });
@@ -1,65 +0,0 @@
1
- import { LoggerService, LogLevel } from '@nestjs/common';
2
- import { type EventEmitter2 } from '@nestjs/event-emitter';
3
- import { PluginSystem } from '@nest-yalc-2/utils/plugin.helper.js';
4
- import { YalcGlobalClsService } from '../../app/src/cls.module.js';
5
- export interface LogMethodOptions {
6
- message?: any;
7
- data?: any;
8
- config?: any;
9
- masks?: string[];
10
- context?: string;
11
- stack?: string;
12
- event?: string | false;
13
- }
14
- export type LogMethod = (message: any, options?: LogMethodOptions) => void;
15
- export type LogMethodError = (message: any, stack?: string, options?: LogMethodOptions) => void;
16
- export interface ILoggerPluginMethods<TClsService = any> extends Record<string, {
17
- (...args: any[]): void;
18
- } | undefined> {
19
- onBeforeLogging?: (message: any, options: LogMethodOptions, clsService: TClsService) => void;
20
- }
21
- export interface ImprovedLoggerService extends ImprovedLoggerServiceMethods, PluginSystem<ILoggerPluginMethods> {
22
- }
23
- export interface ImprovedLoggerServiceMethods extends LoggerService {
24
- log: LogMethod;
25
- error: LogMethodError;
26
- warn: LogMethod;
27
- debug?: LogMethod | undefined;
28
- verbose?: LogMethod;
29
- }
30
- export declare const EVENT_LOG_DEFAULT = "EVENT_LOG_DEFAULT";
31
- export interface IImprovedLoggerOptions {
32
- event?: {
33
- eventEmitter?: EventEmitter2 | false;
34
- useFallbackEvent?: boolean;
35
- } | false;
36
- clsService?: YalcGlobalClsService;
37
- overrideLoggerLevels?: LogLevel[];
38
- }
39
- declare const LoggerAbstractService_base: {
40
- new (...args: any[]): {
41
- [x: string]: any;
42
- plugins: import("@nest-yalc-2/utils/plugin.helper.js").Plugin<ILoggerPluginMethods<any>>[];
43
- registerPlugin(plugin: import("@nest-yalc-2/utils/plugin.helper.js").Plugin<ILoggerPluginMethods<any>>): void;
44
- unregisterPlugin(plugin: import("@nest-yalc-2/utils/plugin.helper.js").Plugin<ILoggerPluginMethods<any>>): void;
45
- invokePlugins(methodName: keyof ILoggerPluginMethods<any>, ...args: any[]): void;
46
- };
47
- };
48
- export declare abstract class LoggerAbstractService extends LoggerAbstractService_base implements ImprovedLoggerService {
49
- protected context: string;
50
- protected logLevels: LogLevel[] | undefined;
51
- protected methods: ImprovedLoggerServiceMethods;
52
- protected options: IImprovedLoggerOptions;
53
- readonly isImprovedLoggerService = true;
54
- constructor(context: string, logLevels: LogLevel[] | undefined, methods: ImprovedLoggerServiceMethods, options?: IImprovedLoggerOptions);
55
- setLogLevels(levels: LogLevel[]): void;
56
- initializeLogger(): void;
57
- log: LogMethod;
58
- error: LogMethodError;
59
- warn: LogMethod;
60
- debug?: LogMethod | undefined;
61
- verbose?: LogMethod;
62
- beforeLogging(message: any, options: LogMethodOptions): void;
63
- }
64
- export declare function beforeLogging(message: any, options?: LogMethodOptions & IImprovedLoggerOptions['event']): void;
65
- export {};
@@ -1,91 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LoggerAbstractService = exports.EVENT_LOG_DEFAULT = void 0;
4
- exports.beforeLogging = beforeLogging;
5
- const logger_enum_js_1 = require("./logger.enum.js");
6
- const plugin_helper_js_1 = require("@nest-yalc-2/utils/plugin.helper.js");
7
- exports.EVENT_LOG_DEFAULT = 'EVENT_LOG_DEFAULT';
8
- class LoggerAbstractService extends (0, plugin_helper_js_1.WithPluginSystem)() {
9
- constructor(context, logLevels, methods, options = {}) {
10
- super();
11
- this.context = context;
12
- this.logLevels = logLevels;
13
- this.methods = methods;
14
- this.options = options;
15
- this.isImprovedLoggerService = true;
16
- this.initializeLogger();
17
- }
18
- setLogLevels(levels) {
19
- this.logLevels = levels;
20
- this.initializeLogger();
21
- }
22
- initializeLogger() {
23
- const enabledLevels = {};
24
- this.logLevels?.forEach((level) => {
25
- if (!(level.toUpperCase() in logger_enum_js_1.LogLevelEnum))
26
- throw new Error(`ERROR: Logger Level: ${level} is not supported!`);
27
- enabledLevels[level] = true;
28
- });
29
- this.log = (message, options = {}) => {
30
- void this.beforeLogging(message, options);
31
- (enabledLevels[logger_enum_js_1.LogLevelEnum.LOG] === true &&
32
- this.methods[logger_enum_js_1.LogLevelEnum.LOG]
33
- ? this.methods[logger_enum_js_1.LogLevelEnum.LOG]
34
- : () => { })(message, options);
35
- };
36
- this.error = (message, stack, options = {}) => {
37
- options.stack = stack;
38
- void this.beforeLogging(message, options);
39
- (enabledLevels[logger_enum_js_1.LogLevelEnum.ERROR] === true &&
40
- this.methods[logger_enum_js_1.LogLevelEnum.ERROR]
41
- ? this.methods[logger_enum_js_1.LogLevelEnum.ERROR]
42
- : () => { })(message, stack, options);
43
- };
44
- this.warn = (message, options = {}) => {
45
- void this.beforeLogging(message, options);
46
- (enabledLevels[logger_enum_js_1.LogLevelEnum.WARN] === true &&
47
- this.methods[logger_enum_js_1.LogLevelEnum.WARN]
48
- ? this.methods[logger_enum_js_1.LogLevelEnum.WARN]
49
- : () => { })(message, options);
50
- };
51
- if (enabledLevels[logger_enum_js_1.LogLevelEnum.DEBUG] === true &&
52
- this.methods[logger_enum_js_1.LogLevelEnum.DEBUG])
53
- this.debug = (message, options = {}) => {
54
- void this.beforeLogging(message, options);
55
- this.methods[logger_enum_js_1.LogLevelEnum.DEBUG](message, options);
56
- };
57
- if (enabledLevels[logger_enum_js_1.LogLevelEnum.VERBOSE] === true &&
58
- this.methods[logger_enum_js_1.LogLevelEnum.VERBOSE])
59
- this.verbose = (message, options = {}) => {
60
- void this.beforeLogging(message, options);
61
- this.methods[logger_enum_js_1.LogLevelEnum.VERBOSE](message, options);
62
- };
63
- }
64
- beforeLogging(message, options) {
65
- this.options.event = this.options.event ?? {};
66
- this.invokePlugins('onBeforeLogging', message, options, this.options.clsService);
67
- return beforeLogging(message, options);
68
- }
69
- }
70
- exports.LoggerAbstractService = LoggerAbstractService;
71
- function beforeLogging(message, options = {}) {
72
- const emitter = options && options.eventEmitter;
73
- if (!emitter)
74
- return;
75
- const useFallbackEvent = (options && options.useFallbackEvent) ?? false;
76
- const defaultEventName = useFallbackEvent ? exports.EVENT_LOG_DEFAULT : false;
77
- const eventName = options.event ?? defaultEventName;
78
- if (!eventName)
79
- return;
80
- const { event } = require('@nest-yalc-2/event-manager/event.js');
81
- event(eventName, {
82
- event: { emitter },
83
- data: options?.data,
84
- config: options?.config,
85
- masks: options?.masks,
86
- message,
87
- logger: false,
88
- stack: options?.stack,
89
- });
90
- }
91
- //# sourceMappingURL=logger-abstract.service.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"logger-abstract.service.js","sourceRoot":"","sources":["logger-abstract.service.ts"],"names":[],"mappings":";;;AA0LA,sCAuBC;AA/MD,qDAAgD;AAGhD,0EAG6C;AAuDhC,QAAA,iBAAiB,GAAG,mBAAmB,CAAC;AAiBrD,MAAsB,qBACpB,SAAQ,IAAA,mCAAgB,GAAwB;IAYhD,YACY,OAAe,EACf,SAAiC,EACjC,OAAqC,EACrC,UAAkC,EAAE;QAE9C,KAAK,EAAE,CAAC;QALE,YAAO,GAAP,OAAO,CAAQ;QACf,cAAS,GAAT,SAAS,CAAwB;QACjC,YAAO,GAAP,OAAO,CAA8B;QACrC,YAAO,GAAP,OAAO,CAA6B;QAbhC,4BAAuB,GAAG,IAAI,CAAC;QAiB7C,IAAI,CAAC,gBAAgB,EAAE,CAAC;IAC1B,CAAC;IAED,YAAY,CAAC,MAAkB;QAC7B,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC;QACxB,IAAI,CAAC,gBAAgB,EAAE,CAAC;IAC1B,CAAC;IAED,gBAAgB;QACd,MAAM,aAAa,GAA+B,EAAE,CAAC;QACrD,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;YAChC,IAAI,CAAC,CAAC,KAAK,CAAC,WAAW,EAAE,IAAI,6BAAY,CAAC;gBACxC,MAAM,IAAI,KAAK,CAAC,wBAAw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,aAAa,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACzC,CAAC;CACF;AAxGD,sDAwGC;AAED,SAAgB,aAAa,CAC3B,OAAY,EACZ,UAA8D,EAAE;IAEhE,MAAM,OAAO,GAAG,OAAO,IAAI,OAAO,CAAC,YAAY,CAAC;IAChD,IAAI,CAAC,OAAO;QAAE,OAAO;IAErB,MAAM,gBAAgB,GAAG,CAAC,OAAO,IAAI,OAAO,CAAC,gBAAgB,CAAC,IAAI,KAAK,CAAC;IACxE,MAAM,gBAAgB,GAAG,gBAAgB,CAAC,CAAC,CAAC,yBAAiB,CAAC,CAAC,CAAC,KAAK,CAAC;IACtE,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,IAAI,gBAAgB,CAAC;IAEpD,IAAI,CAAC,SAAS;QAAE,OAAO;IAEvB,MAAM,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC,qCAAqC,CAAC,CAAC;IACjE,KAAK,CAAC,SAAS,EAAE;QACf,KAAK,EAAE,EAAE,OAAO,EAAE;QAClB,IAAI,EAAE,OAAO,EAAE,IAAI;QACnB,MAAM,EAAE,OAAO,EAAE,MAAM;QACvB,KAAK,EAAE,OAAO,EAAE,KAAK;QACrB,OAAO;QACP,MAAM,EAAE,KAAK;QACb,KAAK,EAAE,OAAO,EAAE,KAAK;KACtB,CAAC,CAAC;AACL,CAAC"}
@@ -1,210 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unused-vars */
2
- import { LoggerService, LogLevel } from '@nestjs/common';
3
- import { LogLevelEnum } from './logger.enum.js';
4
- import { type EventEmitter2 } from '@nestjs/event-emitter';
5
- // Removed event import to break circular dependency
6
- import {
7
- PluginSystem,
8
- WithPluginSystem,
9
- } from '@nest-yalc-2/utils/plugin.helper.js';
10
- import { YalcGlobalClsService } from '../../app/src/cls.module.js';
11
-
12
- export interface LogMethodOptions {
13
- message?: any;
14
- /**
15
- * The data is the place where you want to add the extra information
16
- * that are not returned back as a response but they can be sent to the logger or the event emitter.
17
- */
18
- data?: any;
19
- /**
20
- * This can be used to log the configuration values of the event.
21
- * This might be helpful to filter the logs based on extra configuration values
22
- * that are not the basic error level, statusCode etc.
23
- */
24
- config?: any;
25
- masks?: string[];
26
- context?: string;
27
- stack?: string;
28
- /**
29
- * If false, it will not trigger an event.
30
- * If string, it will trigger an event with the string as the event name instead of the default event name.
31
- */
32
- event?: string | false;
33
- }
34
-
35
- export type LogMethod = (message: any, options?: LogMethodOptions) => void;
36
- export type LogMethodError = (
37
- message: any,
38
- stack?: string,
39
- options?: LogMethodOptions,
40
- ) => void;
41
-
42
- export interface ILoggerPluginMethods<TClsService = any> extends Record<
43
- string,
44
- { (...args: any[]): void } | undefined
45
- > {
46
- onBeforeLogging?: (
47
- message: any,
48
- options: LogMethodOptions,
49
- clsService: TClsService,
50
- ) => void;
51
- }
52
-
53
- export interface ImprovedLoggerService
54
- extends ImprovedLoggerServiceMethods, PluginSystem<ILoggerPluginMethods> {}
55
-
56
- export interface ImprovedLoggerServiceMethods extends LoggerService {
57
- log: LogMethod;
58
- error: LogMethodError;
59
- warn: LogMethod;
60
- debug?: LogMethod | undefined;
61
- verbose?: LogMethod;
62
- }
63
-
64
- export const EVENT_LOG_DEFAULT = 'EVENT_LOG_DEFAULT';
65
-
66
- export interface IImprovedLoggerOptions {
67
- event?:
68
- | {
69
- eventEmitter?: EventEmitter2 | false;
70
- /**
71
- * if set to true, it trigger an event with the name of EVENT_LOG_DEFAULT even
72
- * if we didn't specified the event name
73
- */
74
- useFallbackEvent?: boolean;
75
- }
76
- | false;
77
- clsService?: YalcGlobalClsService;
78
- overrideLoggerLevels?: LogLevel[];
79
- }
80
-
81
- export abstract class LoggerAbstractService
82
- extends WithPluginSystem<ILoggerPluginMethods>()
83
- implements ImprovedLoggerService
84
- {
85
- public readonly isImprovedLoggerService = true;
86
-
87
- /**
88
- * This constructor override its empty method based on passed
89
- * logLevels and this.methods
90
- *
91
- * @param logLevels
92
- * @param this.methods
93
- */
94
- constructor(
95
- protected context: string,
96
- protected logLevels: LogLevel[] | undefined,
97
- protected methods: ImprovedLoggerServiceMethods,
98
- protected options: IImprovedLoggerOptions = {},
99
- ) {
100
- super();
101
-
102
- this.initializeLogger();
103
- }
104
-
105
- setLogLevels(levels: LogLevel[]) {
106
- this.logLevels = levels;
107
- this.initializeLogger();
108
- }
109
-
110
- initializeLogger() {
111
- const enabledLevels: { [key: string]: boolean } = {};
112
- this.logLevels?.forEach((level) => {
113
- if (!(level.toUpperCase() in LogLevelEnum))
114
- throw new Error(`ERROR: Logger Level: ${level} is not supported!`);
115
-
116
- enabledLevels[level] = true;
117
- });
118
-
119
- this.log = (message: any, options: LogMethodOptions = {}) => {
120
- void this.beforeLogging(message, options);
121
- (enabledLevels[LogLevelEnum.LOG] === true &&
122
- this.methods[LogLevelEnum.LOG]
123
- ? this.methods[LogLevelEnum.LOG]
124
- : () => {})(message, options);
125
- };
126
-
127
- this.error = (
128
- message: any,
129
- stack?: string,
130
- options: LogMethodOptions = {},
131
- ) => {
132
- options.stack = stack;
133
- void this.beforeLogging(message, options);
134
-
135
- (enabledLevels[LogLevelEnum.ERROR] === true &&
136
- this.methods[LogLevelEnum.ERROR]
137
- ? this.methods[LogLevelEnum.ERROR]
138
- : () => {})(message, stack, options);
139
- };
140
-
141
- this.warn = (message: any, options: LogMethodOptions = {}) => {
142
- void this.beforeLogging(message, options);
143
-
144
- (enabledLevels[LogLevelEnum.WARN] === true &&
145
- this.methods[LogLevelEnum.WARN]
146
- ? this.methods[LogLevelEnum.WARN]
147
- : () => {})(message, options);
148
- };
149
-
150
- if (
151
- enabledLevels[LogLevelEnum.DEBUG] === true &&
152
- this.methods[LogLevelEnum.DEBUG]
153
- )
154
- this.debug = (message: any, options: LogMethodOptions = {}) => {
155
- void this.beforeLogging(message, options);
156
- this.methods[LogLevelEnum.DEBUG]!(message, options);
157
- };
158
-
159
- if (
160
- enabledLevels[LogLevelEnum.VERBOSE] === true &&
161
- this.methods[LogLevelEnum.VERBOSE]
162
- )
163
- this.verbose = (message: any, options: LogMethodOptions = {}) => {
164
- void this.beforeLogging(message, options);
165
- this.methods[LogLevelEnum.VERBOSE]!(message, options);
166
- };
167
- }
168
-
169
- log!: LogMethod;
170
- error!: LogMethodError;
171
- warn!: LogMethod;
172
- debug?: LogMethod | undefined;
173
- verbose?: LogMethod;
174
-
175
- beforeLogging(message: any, options: LogMethodOptions) {
176
- this.options.event = this.options.event ?? {};
177
- this.invokePlugins(
178
- 'onBeforeLogging',
179
- message,
180
- options,
181
- this.options.clsService,
182
- );
183
- return beforeLogging(message, options);
184
- }
185
- }
186
-
187
- export function beforeLogging(
188
- message: any,
189
- options: LogMethodOptions & IImprovedLoggerOptions['event'] = {},
190
- ) {
191
- const emitter = options && options.eventEmitter;
192
- if (!emitter) return;
193
-
194
- const useFallbackEvent = (options && options.useFallbackEvent) ?? false;
195
- const defaultEventName = useFallbackEvent ? EVENT_LOG_DEFAULT : false;
196
- const eventName = options.event ?? defaultEventName;
197
-
198
- if (!eventName) return;
199
-
200
- const { event } = require('@nest-yalc-2/event-manager/event.js');
201
- event(eventName, {
202
- event: { emitter },
203
- data: options?.data,
204
- config: options?.config,
205
- masks: options?.masks,
206
- message,
207
- logger: false,
208
- stack: options?.stack,
209
- });
210
- }
@@ -1,5 +0,0 @@
1
- import { LogLevel } from '@nestjs/common';
2
- import { IImprovedLoggerOptions, LoggerAbstractService } from './logger-abstract.service.js';
3
- export declare class ConsoleLogger extends LoggerAbstractService {
4
- constructor(context: string, logLevels: LogLevel[] | undefined, options?: IImprovedLoggerOptions);
5
- }
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConsoleLogger = void 0;
4
- const logger_abstract_service_js_1 = require("./logger-abstract.service.js");
5
- const logger_helper_js_1 = require("./logger.helper.js");
6
- const logOnlyDefined = (...args) => {
7
- return args.filter(function (element) {
8
- return element !== undefined;
9
- });
10
- };
11
- class ConsoleLogger extends logger_abstract_service_js_1.LoggerAbstractService {
12
- constructor(context, logLevels, options = {}) {
13
- super(context, logLevels, {
14
- log: (message, options, ...rest) => console.log(...logOnlyDefined(`[${options?.context ?? context}]`, message, (0, logger_helper_js_1.maskDataInObject)(options?.data, options?.masks, options?.stack), options?.config, ...rest)),
15
- error: (message, trace, options, ...rest) => console.error(...logOnlyDefined(`[${options?.context ?? context}]`, message, trace, (0, logger_helper_js_1.maskDataInObject)(options?.data, options?.masks), options?.config, ...rest)),
16
- debug: (message, options, ...rest) => console.debug(...logOnlyDefined(`[${options?.context ?? context}]`, message, (0, logger_helper_js_1.maskDataInObject)(options?.data, options?.masks, options?.stack), options?.config, ...rest)),
17
- warn: (message, options, ...rest) => console.warn(...logOnlyDefined(`[${options?.context ?? context}]`, message, (0, logger_helper_js_1.maskDataInObject)(options?.data, options?.masks, options?.stack), options?.config, ...rest)),
18
- verbose: (message, options, ...rest) => console.info(...logOnlyDefined(`[${options?.context ?? context}]`, message, (0, logger_helper_js_1.maskDataInObject)(options?.data, options?.masks, options?.stack), options?.config, ...rest)),
19
- }, options);
20
- }
21
- }
22
- exports.ConsoleLogger = ConsoleLogger;
23
- //# sourceMappingURL=logger-console.service.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"logger-console.service.js","sourceRoot":"","sources":["logger-console.service.ts"],"names":[],"mappings":";;;AAEA,6EAGsC;AACtC,yDAAsD;AAEtD,MAAM,cAAc,GAAG,CAAC,GAAG,IAAW,EAAE,EAAE;IACxC,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,OAAO;QAClC,OAAO,OAAO,KAAK,SAAS,CAAC;IAC/B,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAa,aAAc,SAAQ,kDAAqB;IACtD,YACE,OAAe,EACf,SAAiC,EACjC,UAAkC,EAAE;QAEpC,KAAK,CACH,OAAO,EACP,SAAS,EACT;YACE,GAAG,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,CACjC,O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AAE,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,CACrC,OAAO,CAAC,IAAI,CACV,GAAG,cAAc,CACf,IAAI,OAAO,EAAE,OAAO,IAAI,OAAO,GAAG,EAClC,OAAO,EACP,IAAA,mCAAgB,EAAC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,CAAC,EAC/D,OAAO,EAAE,MAAM,EACf,GAAG,IAAI,CACR,CACF;SACJ,EACD,OAAO,CACR,CAAC;IACJ,CAAC;CACF;AAjED,sCAiEC"}
@@ -1,80 +0,0 @@
1
- /* eslint-disable no-console */
2
- import { LogLevel } from '@nestjs/common';
3
- import {
4
- IImprovedLoggerOptions,
5
- LoggerAbstractService,
6
- } from './logger-abstract.service.js';
7
- import { maskDataInObject } from './logger.helper.js';
8
-
9
- const logOnlyDefined = (...args: any[]) => {
10
- return args.filter(function (element) {
11
- return element !== undefined;
12
- });
13
- };
14
-
15
- export class ConsoleLogger extends LoggerAbstractService {
16
- constructor(
17
- context: string,
18
- logLevels: LogLevel[] | undefined,
19
- options: IImprovedLoggerOptions = {},
20
- ) {
21
- super(
22
- context,
23
- logLevels,
24
- {
25
- log: (message, options, ...rest) =>
26
- console.log(
27
- ...logOnlyDefined(
28
- `[${options?.context ?? context}]`,
29
- message,
30
- maskDataInObject(options?.data, options?.masks, options?.stack),
31
- options?.config,
32
- ...rest,
33
- ),
34
- ),
35
- error: (message, trace, options, ...rest) =>
36
- console.error(
37
- ...logOnlyDefined(
38
- `[${options?.context ?? context}]`,
39
- message,
40
- trace,
41
- maskDataInObject(options?.data, options?.masks),
42
- options?.config,
43
- ...rest,
44
- ),
45
- ),
46
- debug: (message, options, ...rest) =>
47
- console.debug(
48
- ...logOnlyDefined(
49
- `[${options?.context ?? context}]`,
50
- message,
51
- maskDataInObject(options?.data, options?.masks, options?.stack),
52
- options?.config,
53
- ...rest,
54
- ),
55
- ),
56
- warn: (message, options, ...rest) =>
57
- console.warn(
58
- ...logOnlyDefined(
59
- `[${options?.context ?? context}]`,
60
- message,
61
- maskDataInObject(options?.data, options?.masks, options?.stack),
62
- options?.config,
63
- ...rest,
64
- ),
65
- ),
66
- verbose: (message, options, ...rest) =>
67
- console.info(
68
- ...logOnlyDefined(
69
- `[${options?.context ?? context}]`,
70
- message,
71
- maskDataInObject(options?.data, options?.masks, options?.stack),
72
- options?.config,
73
- ...rest,
74
- ),
75
- ),
76
- },
77
- options,
78
- );
79
- }
80
- }
@@ -1,10 +0,0 @@
1
- import { LogLevel, OnApplicationShutdown } from '@nestjs/common';
2
- import { type Logger } from 'pino';
3
- import { type IImprovedLoggerOptions, LoggerAbstractService } from './logger-abstract.service.js';
4
- export declare const FLUSH_INTERVAL = 10000;
5
- export declare class PinoLogger extends LoggerAbstractService implements OnApplicationShutdown {
6
- getLogger(): Logger;
7
- constructor(context: string, logLevels: LogLevel[], options?: IImprovedLoggerOptions);
8
- onApplicationShutdown(): Promise<void>;
9
- }
10
- export declare function flush(): Promise<unknown>;