@rf-logger/logger 0.1.5 → 0.1.6

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/logFormat.js CHANGED
@@ -5,4 +5,4 @@ var LogFormatEnum;
5
5
  (function (LogFormatEnum) {
6
6
  LogFormatEnum["Json"] = "json";
7
7
  LogFormatEnum["Plain"] = "plain";
8
- })(LogFormatEnum = exports.LogFormatEnum || (exports.LogFormatEnum = {}));
8
+ })(LogFormatEnum || (exports.LogFormatEnum = LogFormatEnum = {}));
package/dist/logLevel.js CHANGED
@@ -14,4 +14,4 @@ var LogLevelEnum;
14
14
  LogLevelEnum["Verbose"] = "verbose";
15
15
  LogLevelEnum["Debug"] = "debug";
16
16
  LogLevelEnum["Silly"] = "silly";
17
- })(LogLevelEnum = exports.LogLevelEnum || (exports.LogLevelEnum = {}));
17
+ })(LogLevelEnum || (exports.LogLevelEnum = LogLevelEnum = {}));
package/dist/logger.js CHANGED
@@ -3,25 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Logger = void 0;
4
4
  const winston_1 = require("winston");
5
5
  const formats_1 = require("./formats");
6
+ const ExceptionHandlerTransport_1 = require("./transports/ExceptionHandlerTransport");
7
+ const CustomConsoleTransport_1 = require("./transports/CustomConsoleTransport");
6
8
  const defaultOptions = {
7
9
  silent: false,
8
10
  format: "json",
9
11
  level: "silly",
12
+ handleExceptions: true,
13
+ handleRejections: true,
10
14
  handleWarnings: true,
11
15
  };
12
16
  class Logger {
13
17
  constructor(inputOptions) {
14
18
  this.init = () => {
15
- var _a, _b;
16
- const formats = [(0, formats_1.errorFormat)(this.options)];
17
- (_a = this.options.plugins) === null || _a === void 0 ? void 0 : _a.forEach((plugin) => {
18
- plugin.registerFormats && formats.push(...plugin.registerFormats());
19
- });
20
- const baseFormat = winston_1.format.combine(...formats);
21
- const logger = this.createLogger(baseFormat);
22
- (_b = this.options.plugins) === null || _b === void 0 ? void 0 : _b.forEach((plugin) => {
23
- plugin.registerInterceptors && plugin.registerInterceptors(logger);
24
- });
19
+ const logger = this.createLogger();
25
20
  // displays stacktrace for warnings
26
21
  if (this.options.handleWarnings) {
27
22
  process.on("warning", (err) => logger.warn(err));
@@ -30,20 +25,52 @@ class Logger {
30
25
  };
31
26
  this.options = Object.assign(Object.assign({}, defaultOptions), inputOptions);
32
27
  }
33
- createLogger(baseFormat) {
34
- return (0, winston_1.createLogger)({
28
+ createLogger() {
29
+ var _a, _b, _c;
30
+ // register formats
31
+ const formats = [(0, formats_1.errorFormat)(this.options)];
32
+ (_a = this.options.plugins) === null || _a === void 0 ? void 0 : _a.forEach((plugin) => {
33
+ plugin.registerFormats && formats.push(...plugin.registerFormats());
34
+ });
35
+ const baseFormat = winston_1.format.combine(...formats);
36
+ // register exit handlers
37
+ let exitHandlers = [];
38
+ (_b = this.options.plugins) === null || _b === void 0 ? void 0 : _b.forEach((plugin) => {
39
+ if (!plugin.registerExitHandlers)
40
+ return;
41
+ exitHandlers = plugin.registerExitHandlers();
42
+ });
43
+ // create logger
44
+ const logger = (0, winston_1.createLogger)({
35
45
  transports: this.options.transports || [
36
- new winston_1.transports.Console({
46
+ new CustomConsoleTransport_1.CustomConsoleTransport({
37
47
  level: this.options.level,
38
- handleExceptions: true,
39
- handleRejections: true,
48
+ handleExceptions: this.options.handleExceptions,
49
+ handleRejections: this.options.handleRejections,
40
50
  }),
41
51
  ],
42
52
  silent: this.options.silent,
43
53
  format: this.options.format === "json"
44
54
  ? winston_1.format.combine(baseFormat, (0, formats_1.gcpFormat)(), winston_1.format.json())
45
55
  : winston_1.format.combine(baseFormat, winston_1.format.timestamp({ format: "HH:mm:ss.SSS" }), winston_1.format.colorize(), formats_1.simpleFormatWithTimestamp),
56
+ // execute exit handlers when catching an exception
57
+ exceptionHandlers: this.options.handleExceptions && [
58
+ new ExceptionHandlerTransport_1.ExceptionHandlerTransport(exitHandlers),
59
+ ],
60
+ // execute exit handlers when catching a rejection
61
+ rejectionHandlers: this.options.handleRejections && [
62
+ new ExceptionHandlerTransport_1.ExceptionHandlerTransport(exitHandlers),
63
+ ],
64
+ });
65
+ // execute exit handlers when closing the logger
66
+ for (const exitHandler of exitHandlers) {
67
+ logger.on("finish", exitHandler);
68
+ }
69
+ // register interceptors
70
+ (_c = this.options.plugins) === null || _c === void 0 ? void 0 : _c.forEach((plugin) => {
71
+ plugin.registerInterceptors && plugin.registerInterceptors(logger);
46
72
  });
73
+ return logger;
47
74
  }
48
75
  }
49
76
  exports.Logger = Logger;
@@ -0,0 +1,14 @@
1
+ import { transports } from "winston";
2
+ /**
3
+ * This is an override of the Console transport that emits a "finish" event
4
+ * as soon as the transport is closed by the exception handler, see
5
+ * https://github.com/winstonjs/winston/blob/91ec06961d2cc9a522f65c023cd02871aa6d97b2/lib/winston/exception-handler.js#L215C10-L215C10
6
+ * Its only goal is to improve the default Console transport in order for the NodeJS process
7
+ * to not wait the full 3 seconds timeout before exiting, see
8
+ * https://github.com/winstonjs/winston/blob/91ec06961d2cc9a522f65c023cd02871aa6d97b2/lib/winston/exception-handler.js#L225C10-L225C11
9
+ */
10
+ export declare class CustomConsoleTransport extends transports.Console {
11
+ protected _ending: boolean;
12
+ log(info: any, next: () => void): any;
13
+ private finishIfEnding;
14
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomConsoleTransport = void 0;
4
+ const winston_1 = require("winston");
5
+ /**
6
+ * This is an override of the Console transport that emits a "finish" event
7
+ * as soon as the transport is closed by the exception handler, see
8
+ * https://github.com/winstonjs/winston/blob/91ec06961d2cc9a522f65c023cd02871aa6d97b2/lib/winston/exception-handler.js#L215C10-L215C10
9
+ * Its only goal is to improve the default Console transport in order for the NodeJS process
10
+ * to not wait the full 3 seconds timeout before exiting, see
11
+ * https://github.com/winstonjs/winston/blob/91ec06961d2cc9a522f65c023cd02871aa6d97b2/lib/winston/exception-handler.js#L225C10-L225C11
12
+ */
13
+ class CustomConsoleTransport extends winston_1.transports.Console {
14
+ constructor() {
15
+ super(...arguments);
16
+ this._ending = false;
17
+ }
18
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
+ log(info, next) {
20
+ // @ts-expect-error The log function is defined for the Console transport
21
+ super.log(info, next);
22
+ this.finishIfEnding();
23
+ }
24
+ finishIfEnding() {
25
+ if (this._ending) {
26
+ this.emit("finish");
27
+ }
28
+ }
29
+ }
30
+ exports.CustomConsoleTransport = CustomConsoleTransport;
@@ -0,0 +1,15 @@
1
+ /// <reference types="node" />
2
+ import { Writable } from "stream";
3
+ import { transports } from "winston";
4
+ import { ExitHandler } from "../types";
5
+ /**
6
+ * This is a custom winston transport made to act as an exception handler and
7
+ * to wait for exit handlers to be processed before emitting a finish event.
8
+ */
9
+ export declare class ExceptionHandlerTransport extends transports.Stream {
10
+ private exitHandlers;
11
+ protected _ending: boolean;
12
+ protected _stream: Writable;
13
+ constructor(exitHandlers: ExitHandler[], options?: transports.StreamTransportOptions);
14
+ private finishIfEnding;
15
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExceptionHandlerTransport = void 0;
4
+ const stream_1 = require("stream");
5
+ const winston_1 = require("winston");
6
+ /**
7
+ * This is a custom winston transport made to act as an exception handler and
8
+ * to wait for exit handlers to be processed before emitting a finish event.
9
+ */
10
+ class ExceptionHandlerTransport extends winston_1.transports.Stream {
11
+ constructor(exitHandlers, options) {
12
+ super(Object.assign(Object.assign({}, options), { stream: new stream_1.Writable() }));
13
+ this.exitHandlers = exitHandlers;
14
+ this._ending = false;
15
+ this._stream = new stream_1.Writable();
16
+ this._stream._write = (chunk, encoding, callback) => {
17
+ this.finishIfEnding();
18
+ callback();
19
+ };
20
+ }
21
+ finishIfEnding() {
22
+ if (this._ending) {
23
+ this._stream.once("finish", () => this.emit("finish"));
24
+ void Promise.all(this.exitHandlers.map((h) => h())).then(() => this._stream.end());
25
+ }
26
+ }
27
+ }
28
+ exports.ExceptionHandlerTransport = ExceptionHandlerTransport;
package/dist/types.d.ts CHANGED
@@ -5,6 +5,8 @@ export type Options = {
5
5
  format?: LogFormat;
6
6
  silent?: boolean;
7
7
  level?: LogLevel;
8
+ handleExceptions?: boolean;
9
+ handleRejections?: boolean;
8
10
  handleWarnings?: boolean;
9
11
  plugins?: LoggerPlugin[];
10
12
  transports?: transport[];
@@ -16,9 +18,11 @@ export type ErrorMetaDeep = Record<string, string>;
16
18
  export type ErrorMeta = Record<string, string | ErrorMetaDeep>;
17
19
  export type ErrorMetaGenerator = (error: ErrorLog) => ErrorMeta;
18
20
  export type ErrorForwarder = (error: ErrorLog, info: Logform.TransformableInfo, meta: ErrorMeta) => void;
21
+ export type ExitHandler = () => Promise<void>;
19
22
  export type LoggerPlugin = {
20
23
  registerFormats?: () => Logform.Format[];
21
24
  registerInterceptors?: (logger: Logger) => void;
22
25
  registerErrorMetaGenerators?: () => ErrorMetaGenerator[];
23
26
  registerErrorForwarders?: () => ErrorForwarder[];
27
+ registerExitHandlers?: () => ExitHandler[];
24
28
  };
package/package.json CHANGED
@@ -21,7 +21,7 @@
21
21
  "engines": {
22
22
  "node": ">=16.0.0"
23
23
  },
24
- "version": "0.1.5",
24
+ "version": "0.1.6",
25
25
  "scripts": {
26
26
  "test": "jest",
27
27
  "version": "pnpm version --no-git-tag-version"