@rawnodes/logger 2.12.0 → 2.12.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.
package/dist/index.mjs CHANGED
@@ -2,7 +2,6 @@ import { hostname } from 'os';
2
2
  import pino from 'pino';
3
3
  import { AsyncLocalStorage } from 'async_hooks';
4
4
  import { Transform, Writable } from 'stream';
5
- import { mkdir } from 'fs/promises';
6
5
  import { createStream } from 'rotating-file-stream';
7
6
  import { CloudWatchLogsClient, PutLogEventsCommand, CreateLogGroupCommand, CreateLogStreamCommand } from '@aws-sdk/client-cloudwatch-logs';
8
7
  import { randomUUID } from 'crypto';
@@ -39,6 +38,89 @@ function assertLogLevel(level) {
39
38
  throw new Error(`Invalid log level: "${level}". Valid levels: ${Object.keys(LOG_LEVELS).join(", ")}`);
40
39
  }
41
40
  }
41
+ function reportFileFailure(error) {
42
+ const code = "code" in error && typeof error.code === "string" ? error.code : error.name;
43
+ process.stderr.write(
44
+ `[Logger] File logging stopped (${code}); other transports remain active.
45
+ `
46
+ );
47
+ }
48
+ function createFileDestination(config) {
49
+ let failed = false;
50
+ let rotatingStream;
51
+ const fail = (error) => {
52
+ if (failed) return;
53
+ failed = true;
54
+ if (config.onError) {
55
+ try {
56
+ config.onError(error);
57
+ } catch {
58
+ reportFileFailure(error);
59
+ }
60
+ } else {
61
+ reportFileFailure(error);
62
+ }
63
+ };
64
+ try {
65
+ rotatingStream = createStream(
66
+ (time, index) => {
67
+ const date = time instanceof Date ? time : /* @__PURE__ */ new Date();
68
+ const dateStr = date.toISOString().split("T")[0];
69
+ const base = config.filename.replace(".log", "");
70
+ return `${base}-${dateStr}${index ? `.${index}` : ""}.log`;
71
+ },
72
+ {
73
+ path: config.dirname,
74
+ size: (config.maxSize || "20M").replace(
75
+ /([kmg])$/i,
76
+ (unit) => unit.toUpperCase()
77
+ ),
78
+ interval: config.interval || (config.datePattern === "YYYY-MM-DD-HH" ? "1h" : "1d"),
79
+ compress: config.zippedArchive ? "gzip" : false,
80
+ maxFiles: parseInt(config.maxFiles?.replace("d", "") || "14")
81
+ }
82
+ );
83
+ rotatingStream.on("error", fail);
84
+ } catch (error) {
85
+ fail(error instanceof Error ? error : new Error(String(error)));
86
+ }
87
+ return new Writable({
88
+ write(chunk, encoding, callback) {
89
+ if (failed || !rotatingStream) {
90
+ callback();
91
+ return;
92
+ }
93
+ try {
94
+ rotatingStream.write(chunk, encoding, (error) => {
95
+ if (error) fail(error);
96
+ callback();
97
+ });
98
+ } catch (error) {
99
+ fail(error instanceof Error ? error : new Error(String(error)));
100
+ callback();
101
+ }
102
+ },
103
+ final(callback) {
104
+ if (!rotatingStream || rotatingStream.destroyed) {
105
+ callback();
106
+ return;
107
+ }
108
+ try {
109
+ rotatingStream.end((error) => {
110
+ if (error) fail(error);
111
+ callback();
112
+ });
113
+ } catch (error) {
114
+ fail(error instanceof Error ? error : new Error(String(error)));
115
+ callback();
116
+ }
117
+ },
118
+ destroy(_error, callback) {
119
+ rotatingStream?.destroy();
120
+ callback();
121
+ }
122
+ });
123
+ }
42
124
 
43
125
  // src/transports/buffer.ts
44
126
  var MessageBuffer = class {
@@ -785,7 +867,9 @@ var CloudWatchTransport = class _CloudWatchTransport extends BaseHttpTransport {
785
867
  return this.isAwsErrorNamed(error, "ResourceNotFoundException");
786
868
  }
787
869
  isAwsErrorNamed(error, name) {
788
- return typeof error === "object" && error !== null && "name" in error && error.name === name;
870
+ if (typeof error !== "object" || error === null) return false;
871
+ if ("name" in error && error.name === name) return true;
872
+ return "__type" in error && typeof error.__type === "string" && error.__type.split("#").at(-1) === name;
789
873
  }
790
874
  };
791
875
 
@@ -947,26 +1031,7 @@ function createStreams(config, state) {
947
1031
  });
948
1032
  if (config.file) {
949
1033
  const fileConfig = config.file;
950
- mkdir(fileConfig.dirname, { recursive: true }).catch(() => {
951
- });
952
- const normalizeSize = (size) => {
953
- return size.replace(/([kmg])$/i, (match) => match.toUpperCase());
954
- };
955
- const rotatingStream = createStream(
956
- (time, index) => {
957
- const date = time instanceof Date ? time : /* @__PURE__ */ new Date();
958
- const dateStr = date.toISOString().split("T")[0];
959
- const base = fileConfig.filename.replace(".log", "");
960
- return `${base}-${dateStr}${index ? `.${index}` : ""}.log`;
961
- },
962
- {
963
- path: fileConfig.dirname,
964
- size: normalizeSize(fileConfig.maxSize || "20M"),
965
- interval: fileConfig.interval || (fileConfig.datePattern === "YYYY-MM-DD-HH" ? "1h" : "1d"),
966
- compress: fileConfig.zippedArchive ? "gzip" : false,
967
- maxFiles: parseInt(fileConfig.maxFiles?.replace("d", "") || "14")
968
- }
969
- );
1034
+ const rotatingStream = createFileDestination(fileConfig);
970
1035
  const fileStream = createFormattedFilterStream(
971
1036
  fileConfig.format,
972
1037
  fileConfig.level,
@@ -1358,7 +1423,8 @@ var FileConfigSchema = z.object({
1358
1423
  interval: z.string().regex(/^\d+[Mdhms]$/).optional(),
1359
1424
  zippedArchive: z.boolean().optional(),
1360
1425
  maxSize: z.string().optional(),
1361
- maxFiles: z.string().optional()
1426
+ maxFiles: z.string().optional(),
1427
+ onError: z.function().optional()
1362
1428
  });
1363
1429
  var HttpTransportBaseConfigSchema = z.object({
1364
1430
  level: LogLevelSchema.optional(),