@oxog/log 1.0.0 → 1.0.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.js CHANGED
@@ -1,3 +1,10 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
1
8
  // src/logger.ts
2
9
  import { createKernel } from "@oxog/plugin";
3
10
  import { createEmitter } from "@oxog/emitter";
@@ -163,7 +170,7 @@ function getCwd() {
163
170
 
164
171
  // src/utils/format.ts
165
172
  function formatJson(entry) {
166
- return JSON.stringify(entry, jsonReplacer);
173
+ return safeStringify(entry);
167
174
  }
168
175
  function jsonReplacer(_key, value) {
169
176
  if (typeof value === "bigint") {
@@ -327,27 +334,45 @@ function consoleTransport(options = {}) {
327
334
  } = options;
328
335
  const mergedColors = { ...DEFAULT_LEVEL_COLORS, ...levelColors };
329
336
  let pigment2;
337
+ function formatEntry(entry) {
338
+ if (colors && pigment2) {
339
+ return formatPretty(entry, pigment2, { timestamp, source: true });
340
+ } else if (colors) {
341
+ return formatWithAnsi(entry, mergedColors, timestamp);
342
+ } else {
343
+ return formatJson(entry);
344
+ }
345
+ }
330
346
  return {
331
347
  name: "console",
332
348
  write(entry) {
333
- let output;
334
349
  if (isBrowser()) {
335
350
  writeToBrowserConsole(entry, colors);
336
351
  return;
337
352
  }
338
- if (colors && pigment2) {
339
- output = formatPretty(entry, pigment2, { timestamp, source: true });
340
- } else if (colors) {
341
- output = formatWithAnsi(entry, mergedColors, timestamp);
342
- } else {
343
- output = formatJson(entry);
344
- }
353
+ const output = formatEntry(entry);
345
354
  if (entry.level >= 50) {
346
355
  process.stderr.write(output + "\n");
347
356
  } else {
348
357
  process.stdout.write(output + "\n");
349
358
  }
350
359
  },
360
+ writeSync(entry) {
361
+ if (isBrowser()) {
362
+ writeToBrowserConsole(entry, colors);
363
+ return;
364
+ }
365
+ const output = formatEntry(entry) + "\n";
366
+ const fd = entry.level >= 50 ? 2 : 1;
367
+ try {
368
+ if (entry.level >= 50) {
369
+ process.stderr.write(output);
370
+ } else {
371
+ process.stdout.write(output);
372
+ }
373
+ } catch {
374
+ }
375
+ },
351
376
  flush() {
352
377
  },
353
378
  close() {
@@ -670,6 +695,31 @@ function createLogger(options = {}) {
670
695
  kernel.use(plugin);
671
696
  }
672
697
  let closed = false;
698
+ function emitTransportError(transportName, error, entry) {
699
+ const err = error instanceof Error ? error : new Error(String(error));
700
+ emitter.emit("error", { transport: transportName, error: err, entry });
701
+ if (isNode()) {
702
+ process.stderr.write(`[LOG TRANSPORT ERROR] ${transportName}: ${err.message}
703
+ `);
704
+ }
705
+ }
706
+ function writeToTransportsSync(entry) {
707
+ if (closed) return;
708
+ for (const transport of activeTransports) {
709
+ try {
710
+ if (transport.writeSync) {
711
+ transport.writeSync(entry);
712
+ } else {
713
+ const result = transport.write(entry);
714
+ if (result instanceof Promise) {
715
+ result.catch((err) => emitTransportError(transport.name, err, entry));
716
+ }
717
+ }
718
+ } catch (err) {
719
+ emitTransportError(transport.name, err, entry);
720
+ }
721
+ }
722
+ }
673
723
  async function writeToTransports(entry) {
674
724
  if (closed) return;
675
725
  const promises = [];
@@ -677,14 +727,18 @@ function createLogger(options = {}) {
677
727
  try {
678
728
  const result = transport.write(entry);
679
729
  if (result instanceof Promise) {
680
- promises.push(result);
730
+ promises.push(
731
+ result.catch((err) => {
732
+ emitTransportError(transport.name, err, entry);
733
+ })
734
+ );
681
735
  }
682
736
  } catch (err) {
737
+ emitTransportError(transport.name, err, entry);
683
738
  }
684
739
  }
685
740
  if (promises.length > 0) {
686
- await Promise.all(promises).catch(() => {
687
- });
741
+ await Promise.all(promises);
688
742
  }
689
743
  }
690
744
  function createEntry(levelNum, levelName, msg, data, error) {
@@ -735,10 +789,10 @@ function createLogger(options = {}) {
735
789
  emitter.emit("log", entry);
736
790
  emitter.emit(`log:${levelName}`, entry);
737
791
  if (shouldSync(levelName)) {
738
- writeToTransports(entry).catch(() => {
739
- });
792
+ writeToTransportsSync(entry);
740
793
  } else {
741
- writeToTransports(entry).catch(() => {
794
+ writeToTransports(entry).catch((err) => {
795
+ emitTransportError("logger", err, entry);
742
796
  });
743
797
  }
744
798
  }
@@ -859,9 +913,10 @@ function createLogger(options = {}) {
859
913
  try {
860
914
  const result = transport.flush();
861
915
  if (result instanceof Promise) {
862
- promises.push(result);
916
+ promises.push(result.catch((err) => emitTransportError(transport.name, err)));
863
917
  }
864
- } catch {
918
+ } catch (err) {
919
+ emitTransportError(transport.name, err);
865
920
  }
866
921
  }
867
922
  }
@@ -871,6 +926,31 @@ function createLogger(options = {}) {
871
926
  async close() {
872
927
  if (closed) return;
873
928
  closed = true;
929
+ const ctx = kernel.getContext();
930
+ if (ctx.flushTimerId) {
931
+ clearInterval(ctx.flushTimerId);
932
+ ctx.flushTimerId = void 0;
933
+ }
934
+ if (ctx.buffer && ctx.buffer.length > 0) {
935
+ const bufferedEntries = ctx.buffer;
936
+ ctx.buffer = [];
937
+ for (const entry of bufferedEntries) {
938
+ for (const transport of activeTransports) {
939
+ try {
940
+ if (transport.writeSync) {
941
+ transport.writeSync(entry);
942
+ } else {
943
+ const result = transport.write(entry);
944
+ if (result instanceof Promise) {
945
+ await result.catch((err) => emitTransportError(transport.name, err, entry));
946
+ }
947
+ }
948
+ } catch (err) {
949
+ emitTransportError(transport.name, err, entry);
950
+ }
951
+ }
952
+ }
953
+ }
874
954
  await logger.flush();
875
955
  const promises = [];
876
956
  for (const transport of activeTransports) {
@@ -878,9 +958,10 @@ function createLogger(options = {}) {
878
958
  try {
879
959
  const result = transport.close();
880
960
  if (result instanceof Promise) {
881
- promises.push(result);
961
+ promises.push(result.catch((err) => emitTransportError(transport.name, err)));
882
962
  }
883
- } catch {
963
+ } catch (err) {
964
+ emitTransportError(transport.name, err);
884
965
  }
885
966
  }
886
967
  }
@@ -1036,6 +1117,36 @@ function fileTransport(options) {
1036
1117
  });
1037
1118
  });
1038
1119
  },
1120
+ writeSync(entry) {
1121
+ if (!fs) {
1122
+ try {
1123
+ const fsSync = __require("fs");
1124
+ const pathSync = __require("path");
1125
+ const dir = pathSync.dirname(filePath);
1126
+ if (!fsSync.existsSync(dir)) {
1127
+ fsSync.mkdirSync(dir, { recursive: true });
1128
+ }
1129
+ const line = formatJson(entry) + "\n";
1130
+ fsSync.appendFileSync(filePath, line, "utf8");
1131
+ currentSize += Buffer.byteLength(line, "utf8");
1132
+ } catch {
1133
+ process.stderr.write(formatJson(entry) + "\n");
1134
+ }
1135
+ return;
1136
+ }
1137
+ try {
1138
+ const line = formatJson(entry) + "\n";
1139
+ fs.appendFileSync(filePath, line, "utf8");
1140
+ currentSize += Buffer.byteLength(line, "utf8");
1141
+ } catch (err) {
1142
+ process.stderr.write(formatJson(entry) + "\n");
1143
+ throw new TransportError(
1144
+ `Failed to write sync to file: ${err instanceof Error ? err.message : String(err)}`,
1145
+ "file",
1146
+ err instanceof Error ? err : void 0
1147
+ );
1148
+ }
1149
+ },
1039
1150
  async flush() {
1040
1151
  if (writeStream && "flush" in writeStream) {
1041
1152
  return new Promise((resolve) => {
@@ -1685,7 +1796,9 @@ async function flushBuffer(ctx) {
1685
1796
  await Promise.all(
1686
1797
  entries.flatMap(
1687
1798
  (entry) => ctx.transports.map(
1688
- (transport) => Promise.resolve(transport.write(entry)).catch(() => {
1799
+ (transport) => Promise.resolve(transport.write(entry)).catch((err) => {
1800
+ const error = err instanceof Error ? err : new Error(String(err));
1801
+ ctx.emitter.emit("error", { transport: transport.name, error, entry });
1689
1802
  })
1690
1803
  )
1691
1804
  )