@faapi/faapi 6.9.1 → 6.10.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/cli/index.js CHANGED
@@ -6497,66 +6497,102 @@ function assertLogLevel(value, source) {
6497
6497
  );
6498
6498
  }
6499
6499
  }
6500
+ function createDefaultLoggingState() {
6501
+ return {
6502
+ level: null,
6503
+ consoleLevel: null,
6504
+ sink: null,
6505
+ fileSink: null,
6506
+ stdout: true,
6507
+ accessLog: true,
6508
+ disabled: false
6509
+ };
6510
+ }
6511
+ function getLoggingState() {
6512
+ const g = globalThis;
6513
+ let state2 = g[LOGGING_STATE_KEY];
6514
+ if (!state2) {
6515
+ state2 = createDefaultLoggingState();
6516
+ g[LOGGING_STATE_KEY] = state2;
6517
+ }
6518
+ return state2;
6519
+ }
6500
6520
  function configureLogging(config) {
6521
+ const state2 = getLoggingState();
6501
6522
  const objectConfig = typeof config === "object" ? config : {};
6502
6523
  if (objectConfig.sink && objectConfig.dir) {
6503
6524
  throw new Error(
6504
6525
  "[faapi] config.log.sink and config.log.dir are mutually exclusive: pick one output pipeline (custom sink OR built-in file logs)."
6505
6526
  );
6506
6527
  }
6507
- void globalState.fileSink?.close();
6508
- globalState.disabled = config === false;
6509
- globalState.sink = objectConfig.sink ?? null;
6510
- globalState.accessLog = objectConfig.accessLog ?? objectConfig.dir !== void 0;
6511
- globalState.stdout = objectConfig.stdout ?? true;
6512
- globalState.fileSink = objectConfig.dir ? createFileLogSink({ dir: objectConfig.dir, splitByLevel: objectConfig.splitByLevel }) : null;
6528
+ void state2.fileSink?.close();
6529
+ state2.disabled = config === false;
6530
+ state2.sink = objectConfig.sink ?? null;
6531
+ state2.accessLog = objectConfig.accessLog ?? true;
6532
+ state2.stdout = objectConfig.stdout ?? true;
6533
+ state2.fileSink = objectConfig.dir ? createFileLogSink({ dir: objectConfig.dir, splitByLevel: objectConfig.splitByLevel }) : null;
6534
+ if (objectConfig.consoleLevel !== void 0) {
6535
+ if (objectConfig.consoleLevel !== false) {
6536
+ assertLogLevel(objectConfig.consoleLevel, "config.log.consoleLevel");
6537
+ }
6538
+ state2.consoleLevel = objectConfig.consoleLevel;
6539
+ } else {
6540
+ state2.consoleLevel = null;
6541
+ }
6513
6542
  if (objectConfig.level !== void 0) {
6514
6543
  assertLogLevel(objectConfig.level, "config.log.level");
6515
- globalState.level = objectConfig.level;
6544
+ state2.level = objectConfig.level;
6516
6545
  } else if (process.env.LOG_LEVEL !== void 0) {
6517
6546
  assertLogLevel(process.env.LOG_LEVEL, "LOG_LEVEL env");
6518
- globalState.level = process.env.LOG_LEVEL;
6547
+ state2.level = process.env.LOG_LEVEL;
6519
6548
  } else {
6520
- globalState.level = null;
6549
+ state2.level = null;
6521
6550
  }
6522
6551
  }
6523
- function globalThreshold() {
6524
- if (globalState.level) return globalState.level;
6525
- if (globalState.fileSink) return void 0;
6526
- return "info";
6552
+ function pipelineThreshold() {
6553
+ return getLoggingState().level ?? void 0;
6554
+ }
6555
+ function consoleThreshold() {
6556
+ return getLoggingState().consoleLevel ?? "info";
6527
6557
  }
6528
6558
  function belowThreshold(level, threshold) {
6529
6559
  return threshold !== void 0 && LEVEL_RANK[level] < LEVEL_RANK[threshold];
6530
6560
  }
6531
6561
  function getEffectiveLogLevel() {
6532
- return globalThreshold();
6562
+ return pipelineThreshold();
6563
+ }
6564
+ function consoleDispatch(entry) {
6565
+ const threshold = consoleThreshold();
6566
+ if (threshold === false || belowThreshold(entry.level, threshold)) return;
6567
+ console[CONSOLE_METHOD[entry.level]](formatEntry(entry));
6533
6568
  }
6534
6569
  function globalDispatch(entry) {
6535
- if (globalState.disabled) return;
6536
- if (globalState.sink) {
6537
- globalState.sink(entry);
6570
+ const state2 = getLoggingState();
6571
+ if (state2.disabled) return;
6572
+ const pipeThreshold = pipelineThreshold();
6573
+ if (state2.sink) {
6574
+ if (!belowThreshold(entry.level, pipeThreshold)) state2.sink(entry);
6538
6575
  return;
6539
6576
  }
6540
- if (globalState.fileSink) {
6541
- globalState.fileSink.write(entry);
6542
- if (globalState.stdout) consoleSink(entry);
6577
+ if (state2.fileSink) {
6578
+ if (!belowThreshold(entry.level, pipeThreshold)) state2.fileSink.write(entry);
6579
+ if (state2.stdout) consoleDispatch(entry);
6543
6580
  return;
6544
6581
  }
6545
- consoleSink(entry);
6582
+ consoleDispatch(entry);
6546
6583
  }
6547
6584
  function writeLogEntry(entry) {
6548
- if (belowThreshold(entry.level, globalThreshold())) return;
6549
6585
  globalDispatch(entry);
6550
6586
  }
6551
- function isAccessLogPiped() {
6552
- return !globalState.disabled && globalState.accessLog;
6587
+ function isAccessLogEnabled() {
6588
+ const state2 = getLoggingState();
6589
+ return !state2.disabled && state2.accessLog;
6553
6590
  }
6554
6591
  function createLogger(scope, options) {
6555
6592
  const baseScope = scope;
6556
6593
  const baseFields = options?.fields;
6557
6594
  const write = (level, message, callFields, scopeSuffix) => {
6558
- const threshold = options?.level ?? globalThreshold();
6559
- if (belowThreshold(level, threshold)) return;
6595
+ if (belowThreshold(level, options?.level)) return;
6560
6596
  const entry = {
6561
6597
  level,
6562
6598
  message,
@@ -6582,7 +6618,7 @@ function createLogger(scope, options) {
6582
6618
  });
6583
6619
  return makeLogger(void 0);
6584
6620
  }
6585
- var LEVEL_RANK, VALID_LEVELS, globalState, consoleSink, CONSOLE_METHOD;
6621
+ var LEVEL_RANK, VALID_LEVELS, LOGGING_STATE_KEY, CONSOLE_METHOD;
6586
6622
  var init_logger = __esm({
6587
6623
  "src/logger/logger.ts"() {
6588
6624
  "use strict";
@@ -6590,10 +6626,7 @@ var init_logger = __esm({
6590
6626
  init_fileSink();
6591
6627
  LEVEL_RANK = { debug: 0, info: 1, warn: 2, error: 3 };
6592
6628
  VALID_LEVELS = ["debug", "info", "warn", "error"];
6593
- globalState = { level: null, sink: null, fileSink: null, stdout: true, accessLog: false, disabled: false };
6594
- consoleSink = (entry) => {
6595
- console[CONSOLE_METHOD[entry.level]](formatEntry(entry));
6596
- };
6629
+ LOGGING_STATE_KEY = /* @__PURE__ */ Symbol.for("faapi.logger.state");
6597
6630
  CONSOLE_METHOD = {
6598
6631
  debug: "debug",
6599
6632
  info: "info",
@@ -7947,18 +7980,14 @@ function levelForStatus(status) {
7947
7980
  return "info";
7948
7981
  }
7949
7982
  function defaultLog(entry, text) {
7950
- if (isAccessLogPiped()) {
7951
- const logEntry = {
7952
- level: levelForStatus(entry.status),
7953
- message: text,
7954
- time: (/* @__PURE__ */ new Date()).toISOString(),
7955
- scope: "access",
7956
- fields: entry
7957
- };
7958
- writeLogEntry(logEntry);
7959
- return;
7960
- }
7961
- console.log(entry);
7983
+ if (!isAccessLogEnabled()) return;
7984
+ writeLogEntry({
7985
+ level: levelForStatus(entry.status),
7986
+ message: text,
7987
+ time: (/* @__PURE__ */ new Date()).toISOString(),
7988
+ scope: "access",
7989
+ fields: entry
7990
+ });
7962
7991
  }
7963
7992
  function logger(options = {}) {
7964
7993
  return async (ctx, next) => {
@@ -8365,7 +8394,6 @@ function createServer(options) {
8365
8394
  compression: compressionOption,
8366
8395
  etag: etagOption,
8367
8396
  registries,
8368
- logger: loggerOption,
8369
8397
  bodyLimit = DEFAULT_BODY_LIMIT,
8370
8398
  http2: http2Option,
8371
8399
  trustedProxy = false
@@ -8382,8 +8410,7 @@ function createServer(options) {
8382
8410
  const helmOpts = typeof helmetOption === "object" ? helmetOption : {};
8383
8411
  configMiddlewares.push(helmet(helmOpts));
8384
8412
  }
8385
- const loggerMiddlewareInst = loggerOption === false ? null : loggerOption === true || loggerOption === void 0 ? logger() : logger(loggerOption);
8386
- if (loggerMiddlewareInst) configMiddlewares.push(loggerMiddlewareInst);
8413
+ configMiddlewares.push(logger());
8387
8414
  if (etagOption) {
8388
8415
  const etagOpts = typeof etagOption === "object" ? etagOption : {};
8389
8416
  configMiddlewares.push(etag(etagOpts));
@@ -9611,7 +9638,6 @@ async function createAppBase(options) {
9611
9638
  helmet: config?.helmet,
9612
9639
  compression: config?.compression,
9613
9640
  etag: config?.etag,
9614
- logger: config?.logger,
9615
9641
  bodyLimit: config?.bodyLimit,
9616
9642
  http2: config?.http2,
9617
9643
  trustedProxy: config?.trustedProxy,
@@ -9916,7 +9942,6 @@ var init_createAppCore = __esm({
9916
9942
  "compression",
9917
9943
  "etag",
9918
9944
  "bodyLimit",
9919
- "logger",
9920
9945
  "log",
9921
9946
  "http2",
9922
9947
  "trustedProxy",