@conduitplatform/module-tools 0.17.0-alpha.7 → 0.17.0-alpha.8

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.esm.js CHANGED
@@ -1242,9 +1242,84 @@ async function setupLoki(module, instance) {
1242
1242
  }
1243
1243
  }
1244
1244
 
1245
- // src/utilities/initializeSdk.ts
1246
- import winston2 from "winston";
1245
+ // src/logging/fileTransports.ts
1246
+ import fs from "fs";
1247
+ import os from "os";
1247
1248
  import path3 from "path";
1249
+ import winston2 from "winston";
1250
+ import DailyRotateFile from "winston-daily-rotate-file";
1251
+ var DEFAULT_RETENTION_DAYS = 3;
1252
+ var DEFAULT_MAX_SIZE = "20m";
1253
+ var SIZE_STRING = /^(?:0\.)?\d+[kmg]$/i;
1254
+ function positiveIntFromEnv(raw, fallback) {
1255
+ if (raw === void 0) return fallback;
1256
+ const trimmed = raw.trim();
1257
+ if (!/^[0-9]+$/.test(trimmed)) return fallback;
1258
+ const value = Number(trimmed);
1259
+ if (!Number.isSafeInteger(value) || value <= 0) return fallback;
1260
+ return value;
1261
+ }
1262
+ function logFileMaxSize() {
1263
+ const raw = process.env.LOG_FILE_MAX_SIZE?.trim();
1264
+ if (!raw) return DEFAULT_MAX_SIZE;
1265
+ if (SIZE_STRING.test(raw)) return raw.toLowerCase();
1266
+ if (/^[0-9]+$/.test(raw)) {
1267
+ const bytes = Number(raw);
1268
+ if (Number.isSafeInteger(bytes) && bytes > 0) return bytes;
1269
+ }
1270
+ return DEFAULT_MAX_SIZE;
1271
+ }
1272
+ function formatMeta(meta) {
1273
+ if (Array.isArray(meta)) {
1274
+ if (meta.length === 0) return "";
1275
+ return meta.map((item) => JSON.stringify(item)).join(" ");
1276
+ }
1277
+ if (meta !== null && typeof meta === "object") {
1278
+ if (Object.keys(meta).length === 0) return "";
1279
+ return JSON.stringify(meta);
1280
+ }
1281
+ return "";
1282
+ }
1283
+ function createFileFormat() {
1284
+ return winston2.format.combine(
1285
+ winston2.format.errors({ stack: true }),
1286
+ winston2.format.timestamp({
1287
+ format: "YYYY-MM-DD HH:mm:ss"
1288
+ }),
1289
+ winston2.format.printf((info) => {
1290
+ const metaText = formatMeta(info.meta);
1291
+ const line = `[${info.timestamp}] [${info.level.toUpperCase()}]: ${info.message}`;
1292
+ if (info.stack) {
1293
+ return `${line} ${info.stack}`;
1294
+ }
1295
+ return metaText ? `${line} ${metaText}` : line;
1296
+ })
1297
+ );
1298
+ }
1299
+ function rotateTransport(logDir, name, level) {
1300
+ return new DailyRotateFile({
1301
+ dirname: logDir,
1302
+ filename: `${name}-%DATE%.log`,
1303
+ datePattern: "YYYY-MM-DD",
1304
+ level,
1305
+ maxFiles: `${positiveIntFromEnv(process.env.LOG_RETENTION_DAYS, DEFAULT_RETENTION_DAYS)}d`,
1306
+ maxSize: logFileMaxSize(),
1307
+ zippedArchive: false,
1308
+ auditFile: path3.join(logDir, `.${name}-audit.json`),
1309
+ format: createFileFormat()
1310
+ });
1311
+ }
1312
+ function createFileLogTransports() {
1313
+ if (process.env.LOG_TO_FILE?.trim() === "false") {
1314
+ return [];
1315
+ }
1316
+ const logDir = process.env.LOG_FILE_DIR?.trim() || path3.join(os.tmpdir(), "conduit-logs");
1317
+ fs.mkdirSync(logDir, { recursive: true });
1318
+ return [
1319
+ rotateTransport(logDir, "combined", "info"),
1320
+ rotateTransport(logDir, "errors", "error")
1321
+ ];
1322
+ }
1248
1323
 
1249
1324
  // src/metrics/index.ts
1250
1325
  import {
@@ -1643,17 +1718,9 @@ function clientMiddleware() {
1643
1718
  var initializeSdk = (coreUrl, moduleName, watchModules = true, serviceHealthStatusGetter = () => HealthCheckStatus2.SERVING) => {
1644
1719
  let logger;
1645
1720
  try {
1646
- logger = new ConduitLogger([
1647
- new winston2.transports.File({
1648
- filename: path3.join(__dirname, ".logs/combined.log"),
1649
- level: "info"
1650
- }),
1651
- new winston2.transports.File({
1652
- filename: path3.join(__dirname, ".logs/errors.log"),
1653
- level: "error"
1654
- })
1655
- ]);
1656
- } catch (e) {
1721
+ const fileTransports = createFileLogTransports();
1722
+ logger = fileTransports.length > 0 ? new ConduitLogger(fileTransports) : new ConduitLogger();
1723
+ } catch {
1657
1724
  logger = new ConduitLogger();
1658
1725
  }
1659
1726
  const grpcSdk = new ConduitGrpcSdk7(