miqro 7.0.9 → 7.0.10

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.
@@ -1,7 +1,7 @@
1
1
  import { chmodSync, constants, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
2
  import { basename, dirname, extname, join, relative, resolve } from "node:path";
3
3
  import { cwd, platform } from "node:process";
4
- import { getAuthConfigPath, getCORSConfigPath, getDBConfigPath, getErrorConfigPath, getMiddlewareConfigPath, getMigrationsPath, getMiqroJSONPath, getServerConfigPath, getWSConfigPath } from "../common/paths.js";
4
+ import { getAuthConfigPath, getCORSConfigPath, getDBConfigPath, getErrorConfigPath, getLogConfigPath, getMiddlewareConfigPath, getMigrationsPath, getMiqroJSONPath, getServerConfigPath, getWSConfigPath } from "../common/paths.js";
5
5
  import { getAsset } from "../common/assets.js";
6
6
  import { migration } from "@miqro/query";
7
7
  import { esBuild } from "../common/esbuild.js";
@@ -40,6 +40,9 @@ export async function inflateAppForSea(logger, inflateDir, services, port) {
40
40
  const WSLIST = services.filter(service => getWSConfigPath(resolve(cwd(), service))).map(service => {
41
41
  return `(await require("../${service}/ws.cjs")).default`;
42
42
  }).join(",");
43
+ const LOGCONFIGLIST = services.filter(service => getLogConfigPath(resolve(cwd(), service))).map(service => {
44
+ return `{config: (await require("../${service}/log.cjs")).default, service: "${service}" }`;
45
+ }).join(",");
43
46
  const SERVERCONFIGLIST = services.filter(service => getServerConfigPath(resolve(cwd(), service))).map(service => {
44
47
  return `(await require("../${service}/server.cjs")).default`;
45
48
  }).join(",\n");
@@ -56,21 +59,38 @@ export async function inflateAppForSea(logger, inflateDir, services, port) {
56
59
  })`;
57
60
  }).join(",");
58
61
  writeFile(logger, join(inflateDir, "sea", "package.json"), `{ "type": "module", "private": true }`);
59
- writeFile(logger, join(inflateDir, "sea", "app.cjs"), `const { createServerInterface, ServerRequestHandler, WebSocketManager, DBManager, App, LoggerHandler, LogProvider, LocalCache, ClusterCache } = require("./lib.cjs");
62
+ writeFile(logger, join(inflateDir, "sea", "app.cjs"), `const { createLogProviderOptions, createServerInterface, ServerRequestHandler, WebSocketManager, DBManager, App, LoggerHandler, LogProvider, LocalCache, ClusterCache } = require("./lib.cjs");
60
63
 
61
64
  async function main() {
62
- const PORT = "${PORT}";
63
- const loggerProvider = new LogProvider();
65
+ const PORT = "${PORT}";
66
+
67
+ const logConfigList = await Promise.all([${LOGCONFIGLIST}]);
68
+
69
+ const loggerProvider = new LogProvider(createLogProviderOptions({
70
+ inflated: {
71
+ logConfigMap: logConfigList.reduce((ret, config)=>{
72
+ ret[config.service] = config.config;
73
+ return ret;
74
+ }, {})
75
+ }
76
+ }));
77
+ const logger = loggerProvider.getLogger("SERVER");
64
78
  const localCache = new LocalCache();
65
79
  const cache = new ClusterCache();
66
- const webSocketManager = new WebSocketManager();
67
- const dbManager = new DBManager();
80
+ const webSocketManager = new WebSocketManager({
81
+ loggerProvider,
82
+ logger
83
+ });
84
+ const dbManager = new DBManager({
85
+ loggerProvider,
86
+ logger
87
+ });
68
88
  const serverInterface = createServerInterface({
69
89
  cache,
70
90
  localCache,
71
91
  loggerProvider,
72
92
  webSocketManager,
73
- logger: loggerProvider.getLogger("server"),
93
+ logger,
74
94
  dbManager,
75
95
  port: PORT
76
96
  });
@@ -81,6 +101,7 @@ async function main() {
81
101
 
82
102
  ${!WSLIST ? "" : `\n webSocketManager.replaceALLWS(await Promise.all([${WSLIST}]))`}
83
103
  const app = new App({
104
+ loggerFactory: loggerProvider.requestLoggerFactory,
84
105
  onUpgrade: (req, socket, head) => {
85
106
  try {
86
107
  req.server = serverInterface;
@@ -8,6 +8,7 @@ export { DBManager } from "./services/utils/db-manager.js";
8
8
  export { LogProvider, LogProviderOptions } from "./services/utils/log.js";
9
9
  export { Miqro, MiqroOptions, ServerRequestHandler } from "./services/app.js";
10
10
  export { jsx2HTML } from "./common/jsx.js";
11
+ export { createLogProviderOptions } from "./services/utils/log-transport.js";
11
12
  export { appendAPIModule } from "./inflate/utils/sea-utils.js";
12
13
  export { createServerInterface, ServerInterfaceImplOptions } from "./services/utils/server-interface.js";
13
14
  export { App, LoggerHandler, Router } from "@miqro/core";
@@ -13,6 +13,7 @@ export { Miqro, ServerRequestHandler } from "./services/app.js";
13
13
  // export { initGlobals, assertGlobalTampered } from "./services/globals.js";
14
14
  export { jsx2HTML } from "./common/jsx.js";
15
15
  //exported for --inflate-sea
16
+ export { createLogProviderOptions } from "./services/utils/log-transport.js";
16
17
  export { appendAPIModule } from "./inflate/utils/sea-utils.js";
17
18
  export { createServerInterface } from "./services/utils/server-interface.js";
18
19
  export { App, LoggerHandler, Router } from "@miqro/core";
@@ -5,7 +5,7 @@ import { WebSocketManager } from "./utils/websocketmanager.js";
5
5
  import { DBManager } from "./utils/db-manager.js";
6
6
  import { inflateApp } from "../inflate/inflate.js";
7
7
  import { setupServerConfig } from "../inflate/setup-server-config.js";
8
- import { BASEEDITOR_PATH, LOG_SOCKET_PATH } from "../../editor/common/constants.js";
8
+ import { BASEEDITOR_PATH, LOG_SOCKET_PATH, LOG_WRITE_EVENT } from "../../editor/common/constants.js";
9
9
  import editorWSConfig from "../../editor/ws.js";
10
10
  import editorServerConfig from "../../editor/server.js";
11
11
  import { ClusterCache } from "./utils/cluster-cache.js";
@@ -51,7 +51,9 @@ export class Miqro {
51
51
  services: [],
52
52
  ...(options ? options : {})
53
53
  };
54
- this.loggerProvider = new LogProvider(createLogProviderOptions(this));
54
+ const loggerOptions = createLogProviderOptions(this);
55
+ loggerOptions.transports.push(createEditorLoggerTransport(this));
56
+ this.loggerProvider = new LogProvider(loggerOptions);
55
57
  //if (!this.options.logger) {
56
58
  const SERVER_IDENTIFIER = cluster.isPrimary ?
57
59
  "" :
@@ -588,3 +590,32 @@ export function notifiyServerConfigSync(app, method) {
588
590
  });
589
591
  }
590
592
  }
593
+ function createEditorLoggerTransport(app) {
594
+ return {
595
+ level: "trace",
596
+ write: async (args) => {
597
+ try {
598
+ if (app.options.editor) {
599
+ try {
600
+ const ws = app.webSocketManager.getWS(LOG_SOCKET_PATH);
601
+ if (ws) {
602
+ //console.log("\n\n" + process.pid + " broadcasting " + LOG_SOCKET_PATH + "\n\n\n")
603
+ await ws.broadcast(JSON.stringify({
604
+ type: LOG_WRITE_EVENT,
605
+ level: args.level,
606
+ identifier: args.identifier,
607
+ out: args.out
608
+ }));
609
+ }
610
+ }
611
+ catch (e) {
612
+ console.error(e);
613
+ }
614
+ }
615
+ }
616
+ catch (e) {
617
+ console.error(e);
618
+ }
619
+ }
620
+ };
621
+ }
@@ -1,6 +1,18 @@
1
1
  import { LoggerTransport, WriteArgs } from "@miqro/core";
2
- import { Miqro } from "../app.js";
3
- export declare function createLogProviderOptions(app: Miqro): {
2
+ import { LogConfigMap } from "../../inflate/setup-log.js";
3
+ import { LogProviderOptions } from "./log.js";
4
+ export declare function createLogProviderOptions(app?: {
5
+ options?: {
6
+ dateTimeFormatOptions?: Intl.DateTimeFormatOptions;
7
+ logProviderOptions?: LogProviderOptions;
8
+ name?: string;
9
+ editor?: boolean;
10
+ logFile?: string | boolean;
11
+ };
12
+ inflated?: {
13
+ logConfigMap: LogConfigMap;
14
+ };
15
+ }): {
4
16
  name: string;
5
17
  transports: LoggerTransport[];
6
18
  formatter: (args: WriteArgs) => string;
@@ -1,38 +1,46 @@
1
1
  import { ConsoleTransport, FileTransport } from "@miqro/core";
2
2
  import { format } from "node:util";
3
- import { LOG_SOCKET_PATH, LOG_WRITE_EVENT } from "../../../editor/common/constants.js";
4
3
  export function createLogProviderOptions(app) {
5
4
  const defaultConsole = ConsoleTransport();
6
5
  //console.log("app.options.logFile [%s]", app.options.logFile);
7
- const defaultFile = app.options.logFile !== true && app.options.logFile !== false && String(app.options.logFile).toUpperCase() !== "TRUE" && String(app.options.logFile).toUpperCase() !== "FALSE" &&
6
+ const defaultFile = app && app.options && app?.options?.logFile !== true && app?.options?.logFile !== false && String(app?.options?.logFile).toUpperCase() !== "TRUE" && String(app?.options?.logFile).toUpperCase() !== "FALSE" &&
8
7
  app.options.logFile ? FileTransport(app.options.logFile) :
9
- String(app.options.logFile).toUpperCase() === "TRUE" || app.options.logFile === true || app.options.logFile === undefined ?
8
+ app && app.options && String(app?.options?.logFile).toUpperCase() === "TRUE" || app?.options?.logFile === true || app?.options?.logFile === undefined ?
10
9
  FileTransport() :
11
10
  undefined;
12
11
  const defaultWrite = async (args, level) => {
13
12
  try {
14
- const serviceNamesWithLogConfigReplaceConsole = level === undefined && app.inflated ?
15
- Object.keys(app.inflated.logConfigMap).filter(serviceName => app.inflated.logConfigMap[serviceName].replaceConsoleTransport) : [];
16
- const serviceNamesWithLogConfigReplaceFile = level === undefined && app.inflated ?
17
- Object.keys(app.inflated.logConfigMap).filter(serviceName => app.inflated.logConfigMap[serviceName].replaceFileTransport) : [];
13
+ // console.dir(app.inflated);
14
+ const serviceNamesWithLogConfigReplaceConsole = app?.inflated ?
15
+ Object.keys(app?.inflated?.logConfigMap).filter(serviceName => app?.inflated?.logConfigMap[serviceName].replaceConsoleTransport) : [];
16
+ const serviceNamesWithLogConfigReplaceFile = app?.inflated ?
17
+ Object.keys(app?.inflated?.logConfigMap).filter(serviceName => app?.inflated?.logConfigMap[serviceName].replaceFileTransport) : [];
18
+ // console.dir(level);
19
+ // console.dir(app?.inflated?.logConfigMap["m-admin"])
20
+ // console.dir(serviceNamesWithLogConfigReplaceConsole.length);
21
+ // console.dir(serviceNamesWithLogConfigReplaceFile.length);
18
22
  await Promise.allSettled((level === undefined ?
19
23
  [
20
24
  level === undefined && serviceNamesWithLogConfigReplaceConsole.length === 0 && defaultConsole ?
21
25
  defaultConsole.write(args) : Promise.resolve(),
22
26
  level === undefined && serviceNamesWithLogConfigReplaceFile.length === 0 && defaultFile ?
23
27
  defaultFile.write(args) : Promise.resolve()
24
- ] : []).concat(app.inflated ?
25
- Object.keys(app.inflated.logConfigMap).map(serviceName => app.inflated.logConfigMap[serviceName]).filter(c => c.level === level).map(c => c.write(args)) : []));
28
+ ] : []).concat(app && app.inflated && app?.inflated ?
29
+ Object.keys(app?.inflated?.logConfigMap).map(serviceName => app?.inflated?.logConfigMap[serviceName]).filter(c => c.level === level).map(c => c.write(args)) : []));
26
30
  }
27
31
  catch (e) {
28
32
  console.error(e);
29
33
  }
30
34
  };
31
35
  return {
32
- name: app.options.name,
36
+ name: app?.options?.name,
33
37
  formatter: (args) => {
34
38
  const params = args.optionalParams;
35
- return format(`${new Date().toISOString()} PID[${process.pid}] ${args.identifier ? `[${args.identifier}] ` : ""}${args.level !== "info" ? (args.level === "error" || args.level === "warn" ? `[${args.level.toUpperCase()}] ` : `[${args.level}] `) : ""}${args.message}`, ...params);
39
+ return format(`${new Date().toLocaleDateString(undefined, app?.options?.dateTimeFormatOptions ? app?.options?.dateTimeFormatOptions : {
40
+ hour12: false
41
+ })} ${new Date().toLocaleTimeString(undefined, app?.options?.dateTimeFormatOptions ? app?.options?.dateTimeFormatOptions : {
42
+ hour12: false
43
+ })} PID[${process.pid}] ${args.identifier ? `[${args.identifier}] ` : ""}${args.level !== "info" ? (args.level === "error" || args.level === "warn" ? `[${args.level.toUpperCase()}] ` : `[${args.level}] `) : ""}${args.message}`, ...params);
36
44
  },
37
45
  transports: [
38
46
  ...([undefined, "error", "warn", "info", "debug", "trace"].map(level => {
@@ -46,34 +54,8 @@ export function createLogProviderOptions(app) {
46
54
  await defaultWrite(args, undefined);
47
55
  }
48
56
  };
49
- })), {
50
- level: "trace",
51
- write: async (args) => {
52
- try {
53
- if (app.options.editor) {
54
- try {
55
- const ws = app.webSocketManager.getWS(LOG_SOCKET_PATH);
56
- if (ws) {
57
- //console.log("\n\n" + process.pid + " broadcasting " + LOG_SOCKET_PATH + "\n\n\n")
58
- await ws.broadcast(JSON.stringify({
59
- type: LOG_WRITE_EVENT,
60
- level: args.level,
61
- identifier: args.identifier,
62
- out: args.out
63
- }));
64
- }
65
- }
66
- catch (e) {
67
- console.error(e);
68
- }
69
- }
70
- }
71
- catch (e) {
72
- console.error(e);
73
- }
74
- }
75
- }
57
+ }))
76
58
  ],
77
- ...(app.options.logProviderOptions ? app.options.logProviderOptions : {})
59
+ ...(app?.options?.logProviderOptions ? app?.options?.logProviderOptions : {})
78
60
  };
79
61
  }
package/build/lib.cjs CHANGED
@@ -7133,6 +7133,7 @@ __export(lib_exports3, {
7133
7133
  ServerRequestHandler: () => ServerRequestHandler,
7134
7134
  WebSocketManager: () => WebSocketManager,
7135
7135
  appendAPIModule: () => appendAPIModule,
7136
+ createLogProviderOptions: () => createLogProviderOptions,
7136
7137
  createServerInterface: () => createServerInterface,
7137
7138
  jsx: () => lib_exports,
7138
7139
  jsx2HTML: () => jsx2HTML,
@@ -13053,6 +13054,9 @@ async function inflateAppForSea(logger, inflateDir, services, port) {
13053
13054
  const WSLIST = services.filter((service) => getWSConfigPath((0, import_node_path14.resolve)((0, import_node_process10.cwd)(), service))).map((service) => {
13054
13055
  return `(await require("../${service}/ws.cjs")).default`;
13055
13056
  }).join(",");
13057
+ const LOGCONFIGLIST = services.filter((service) => getLogConfigPath((0, import_node_path14.resolve)((0, import_node_process10.cwd)(), service))).map((service) => {
13058
+ return `{config: (await require("../${service}/log.cjs")).default, service: "${service}" }`;
13059
+ }).join(",");
13056
13060
  const SERVERCONFIGLIST = services.filter((service) => getServerConfigPath((0, import_node_path14.resolve)((0, import_node_process10.cwd)(), service))).map((service) => {
13057
13061
  return `(await require("../${service}/server.cjs")).default`;
13058
13062
  }).join(",\n");
@@ -13072,21 +13076,38 @@ async function inflateAppForSea(logger, inflateDir, services, port) {
13072
13076
  writeFile2(
13073
13077
  logger,
13074
13078
  (0, import_node_path14.join)(inflateDir, "sea", "app.cjs"),
13075
- `const { createServerInterface, ServerRequestHandler, WebSocketManager, DBManager, App, LoggerHandler, LogProvider, LocalCache, ClusterCache } = require("./lib.cjs");
13079
+ `const { createLogProviderOptions, createServerInterface, ServerRequestHandler, WebSocketManager, DBManager, App, LoggerHandler, LogProvider, LocalCache, ClusterCache } = require("./lib.cjs");
13076
13080
 
13077
13081
  async function main() {
13078
- const PORT = "${PORT2}";
13079
- const loggerProvider = new LogProvider();
13082
+ const PORT = "${PORT2}";
13083
+
13084
+ const logConfigList = await Promise.all([${LOGCONFIGLIST}]);
13085
+
13086
+ const loggerProvider = new LogProvider(createLogProviderOptions({
13087
+ inflated: {
13088
+ logConfigMap: logConfigList.reduce((ret, config)=>{
13089
+ ret[config.service] = config.config;
13090
+ return ret;
13091
+ }, {})
13092
+ }
13093
+ }));
13094
+ const logger = loggerProvider.getLogger("SERVER");
13080
13095
  const localCache = new LocalCache();
13081
13096
  const cache = new ClusterCache();
13082
- const webSocketManager = new WebSocketManager();
13083
- const dbManager = new DBManager();
13097
+ const webSocketManager = new WebSocketManager({
13098
+ loggerProvider,
13099
+ logger
13100
+ });
13101
+ const dbManager = new DBManager({
13102
+ loggerProvider,
13103
+ logger
13104
+ });
13084
13105
  const serverInterface = createServerInterface({
13085
13106
  cache,
13086
13107
  localCache,
13087
13108
  loggerProvider,
13088
13109
  webSocketManager,
13089
- logger: loggerProvider.getLogger("server"),
13110
+ logger,
13090
13111
  dbManager,
13091
13112
  port: PORT
13092
13113
  });
@@ -13100,6 +13121,7 @@ async function main() {
13100
13121
  ${!WSLIST ? "" : `
13101
13122
  webSocketManager.replaceALLWS(await Promise.all([${WSLIST}]))`}
13102
13123
  const app = new App({
13124
+ loggerFactory: loggerProvider.requestLoggerFactory,
13103
13125
  onUpgrade: (req, socket, head) => {
13104
13126
  try {
13105
13127
  req.server = serverInterface;
@@ -16850,30 +16872,33 @@ function getPORT() {
16850
16872
  // src/services/utils/log-transport.ts
16851
16873
  init_lib3();
16852
16874
  var import_node_util3 = require("node:util");
16853
- init_constants();
16854
16875
  function createLogProviderOptions(app) {
16855
16876
  const defaultConsole = ConsoleTransport();
16856
- const defaultFile = app.options.logFile !== true && app.options.logFile !== false && String(app.options.logFile).toUpperCase() !== "TRUE" && String(app.options.logFile).toUpperCase() !== "FALSE" && app.options.logFile ? FileTransport(app.options.logFile) : String(app.options.logFile).toUpperCase() === "TRUE" || app.options.logFile === true || app.options.logFile === void 0 ? FileTransport() : void 0;
16877
+ const defaultFile = app && app.options && app?.options?.logFile !== true && app?.options?.logFile !== false && String(app?.options?.logFile).toUpperCase() !== "TRUE" && String(app?.options?.logFile).toUpperCase() !== "FALSE" && app.options.logFile ? FileTransport(app.options.logFile) : app && app.options && String(app?.options?.logFile).toUpperCase() === "TRUE" || app?.options?.logFile === true || app?.options?.logFile === void 0 ? FileTransport() : void 0;
16857
16878
  const defaultWrite = async (args, level) => {
16858
16879
  try {
16859
- const serviceNamesWithLogConfigReplaceConsole = level === void 0 && app.inflated ? Object.keys(app.inflated.logConfigMap).filter((serviceName) => app.inflated.logConfigMap[serviceName].replaceConsoleTransport) : [];
16860
- const serviceNamesWithLogConfigReplaceFile = level === void 0 && app.inflated ? Object.keys(app.inflated.logConfigMap).filter((serviceName) => app.inflated.logConfigMap[serviceName].replaceFileTransport) : [];
16880
+ const serviceNamesWithLogConfigReplaceConsole = app?.inflated ? Object.keys(app?.inflated?.logConfigMap).filter((serviceName) => app?.inflated?.logConfigMap[serviceName].replaceConsoleTransport) : [];
16881
+ const serviceNamesWithLogConfigReplaceFile = app?.inflated ? Object.keys(app?.inflated?.logConfigMap).filter((serviceName) => app?.inflated?.logConfigMap[serviceName].replaceFileTransport) : [];
16861
16882
  await Promise.allSettled((level === void 0 ? [
16862
16883
  level === void 0 && serviceNamesWithLogConfigReplaceConsole.length === 0 && defaultConsole ? defaultConsole.write(args) : Promise.resolve(),
16863
16884
  level === void 0 && serviceNamesWithLogConfigReplaceFile.length === 0 && defaultFile ? defaultFile.write(args) : Promise.resolve()
16864
16885
  ] : []).concat(
16865
- app.inflated ? Object.keys(app.inflated.logConfigMap).map((serviceName) => app.inflated.logConfigMap[serviceName]).filter((c) => c.level === level).map((c) => c.write(args)) : []
16886
+ app && app.inflated && app?.inflated ? Object.keys(app?.inflated?.logConfigMap).map((serviceName) => app?.inflated?.logConfigMap[serviceName]).filter((c) => c.level === level).map((c) => c.write(args)) : []
16866
16887
  ));
16867
16888
  } catch (e) {
16868
16889
  console.error(e);
16869
16890
  }
16870
16891
  };
16871
16892
  return {
16872
- name: app.options.name,
16893
+ name: app?.options?.name,
16873
16894
  formatter: (args) => {
16874
16895
  const params = args.optionalParams;
16875
16896
  return (0, import_node_util3.format)(
16876
- `${(/* @__PURE__ */ new Date()).toISOString()} PID[${process.pid}] ${args.identifier ? `[${args.identifier}] ` : ""}${args.level !== "info" ? args.level === "error" || args.level === "warn" ? `[${args.level.toUpperCase()}] ` : `[${args.level}] ` : ""}${args.message}`,
16897
+ `${(/* @__PURE__ */ new Date()).toLocaleDateString(void 0, app?.options?.dateTimeFormatOptions ? app?.options?.dateTimeFormatOptions : {
16898
+ hour12: false
16899
+ })} ${(/* @__PURE__ */ new Date()).toLocaleTimeString(void 0, app?.options?.dateTimeFormatOptions ? app?.options?.dateTimeFormatOptions : {
16900
+ hour12: false
16901
+ })} PID[${process.pid}] ${args.identifier ? `[${args.identifier}] ` : ""}${args.level !== "info" ? args.level === "error" || args.level === "warn" ? `[${args.level.toUpperCase()}] ` : `[${args.level}] ` : ""}${args.message}`,
16877
16902
  ...params
16878
16903
  );
16879
16904
  },
@@ -16889,33 +16914,9 @@ function createLogProviderOptions(app) {
16889
16914
  await defaultWrite(args, void 0);
16890
16915
  }
16891
16916
  };
16892
- }),
16893
- {
16894
- level: "trace",
16895
- write: async (args) => {
16896
- try {
16897
- if (app.options.editor) {
16898
- try {
16899
- const ws = app.webSocketManager.getWS(LOG_SOCKET_PATH);
16900
- if (ws) {
16901
- await ws.broadcast(JSON.stringify({
16902
- type: LOG_WRITE_EVENT,
16903
- level: args.level,
16904
- identifier: args.identifier,
16905
- out: args.out
16906
- }));
16907
- }
16908
- } catch (e) {
16909
- console.error(e);
16910
- }
16911
- }
16912
- } catch (e) {
16913
- console.error(e);
16914
- }
16915
- }
16916
- }
16917
+ })
16917
16918
  ],
16918
- ...app.options.logProviderOptions ? app.options.logProviderOptions : {}
16919
+ ...app?.options?.logProviderOptions ? app?.options?.logProviderOptions : {}
16919
16920
  };
16920
16921
  }
16921
16922
 
@@ -16982,7 +16983,9 @@ var Miqro = class _Miqro {
16982
16983
  services: [],
16983
16984
  ...options ? options : {}
16984
16985
  };
16985
- this.loggerProvider = new LogProvider(createLogProviderOptions(this));
16986
+ const loggerOptions = createLogProviderOptions(this);
16987
+ loggerOptions.transports.push(createEditorLoggerTransport(this));
16988
+ this.loggerProvider = new LogProvider(loggerOptions);
16986
16989
  const SERVER_IDENTIFIER = import_node_cluster3.default.isPrimary ? "" : process.env["CLUSTER_NODE_NUMBER"] ? `WORKER_${process.env["CLUSTER_NODE_NUMBER"]}`.toUpperCase() : `WORKER`;
16987
16990
  this.logger = this.options.logger ? this.options.logger : this.loggerProvider.getLogger(SERVER_IDENTIFIER);
16988
16991
  this.listener = async (data) => {
@@ -17451,6 +17454,32 @@ function notifiyServerConfigSync(app, method) {
17451
17454
  });
17452
17455
  }
17453
17456
  }
17457
+ function createEditorLoggerTransport(app) {
17458
+ return {
17459
+ level: "trace",
17460
+ write: async (args) => {
17461
+ try {
17462
+ if (app.options.editor) {
17463
+ try {
17464
+ const ws = app.webSocketManager.getWS(LOG_SOCKET_PATH);
17465
+ if (ws) {
17466
+ await ws.broadcast(JSON.stringify({
17467
+ type: LOG_WRITE_EVENT,
17468
+ level: args.level,
17469
+ identifier: args.identifier,
17470
+ out: args.out
17471
+ }));
17472
+ }
17473
+ } catch (e) {
17474
+ console.error(e);
17475
+ }
17476
+ }
17477
+ } catch (e) {
17478
+ console.error(e);
17479
+ }
17480
+ }
17481
+ };
17482
+ }
17454
17483
 
17455
17484
  // src/inflate/utils/sea-utils.ts
17456
17485
  var import_node_path31 = require("node:path");
@@ -17488,6 +17517,7 @@ var JSX2 = {
17488
17517
  ServerRequestHandler,
17489
17518
  WebSocketManager,
17490
17519
  appendAPIModule,
17520
+ createLogProviderOptions,
17491
17521
  createServerInterface,
17492
17522
  jsx,
17493
17523
  jsx2HTML,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "miqro",
3
- "version": "7.0.9",
3
+ "version": "7.0.10",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "build/esm/src/lib.js",
@@ -4,7 +4,7 @@ import { basename, dirname, extname, join, relative, resolve } from "node:path";
4
4
  import { cwd, platform } from "node:process";
5
5
 
6
6
  import { RouteFileMap, StaticFileMap } from "./setup-http.js";
7
- import { getAuthConfigPath, getCORSConfigPath, getDBConfigPath, getErrorConfigPath, getMiddlewareConfigPath, getMigrationsPath, getMiqroJSONPath, getServerConfigPath, getServicePath, getWSConfigPath } from "../common/paths.js";
7
+ import { getAuthConfigPath, getCORSConfigPath, getDBConfigPath, getErrorConfigPath, getLogConfigPath, getMiddlewareConfigPath, getMigrationsPath, getMiqroJSONPath, getServerConfigPath, getServicePath, getWSConfigPath } from "../common/paths.js";
8
8
  import { getAsset } from "../common/assets.js";
9
9
  import { migration } from "@miqro/query";
10
10
  import { esBuild } from "../common/esbuild.js";
@@ -48,7 +48,11 @@ export async function inflateAppForSea(logger: Logger, inflateDir: string, servi
48
48
 
49
49
  const WSLIST = services.filter(service => getWSConfigPath(resolve(cwd(), service))).map(service => {
50
50
  return `(await require("../${service}/ws.cjs")).default`;
51
- }).join(",")
51
+ }).join(",");
52
+
53
+ const LOGCONFIGLIST = services.filter(service => getLogConfigPath(resolve(cwd(), service))).map(service => {
54
+ return `{config: (await require("../${service}/log.cjs")).default, service: "${service}" }`;
55
+ }).join(",");
52
56
 
53
57
  const SERVERCONFIGLIST = services.filter(service => getServerConfigPath(resolve(cwd(), service))).map(service => {
54
58
  return `(await require("../${service}/server.cjs")).default`;
@@ -69,21 +73,38 @@ export async function inflateAppForSea(logger: Logger, inflateDir: string, servi
69
73
 
70
74
  writeFile(logger, join(inflateDir, "sea", "package.json"), `{ "type": "module", "private": true }`);
71
75
 
72
- writeFile(logger, join(inflateDir, "sea", "app.cjs"), `const { createServerInterface, ServerRequestHandler, WebSocketManager, DBManager, App, LoggerHandler, LogProvider, LocalCache, ClusterCache } = require("./lib.cjs");
76
+ writeFile(logger, join(inflateDir, "sea", "app.cjs"), `const { createLogProviderOptions, createServerInterface, ServerRequestHandler, WebSocketManager, DBManager, App, LoggerHandler, LogProvider, LocalCache, ClusterCache } = require("./lib.cjs");
73
77
 
74
78
  async function main() {
75
- const PORT = "${PORT}";
76
- const loggerProvider = new LogProvider();
79
+ const PORT = "${PORT}";
80
+
81
+ const logConfigList = await Promise.all([${LOGCONFIGLIST}]);
82
+
83
+ const loggerProvider = new LogProvider(createLogProviderOptions({
84
+ inflated: {
85
+ logConfigMap: logConfigList.reduce((ret, config)=>{
86
+ ret[config.service] = config.config;
87
+ return ret;
88
+ }, {})
89
+ }
90
+ }));
91
+ const logger = loggerProvider.getLogger("SERVER");
77
92
  const localCache = new LocalCache();
78
93
  const cache = new ClusterCache();
79
- const webSocketManager = new WebSocketManager();
80
- const dbManager = new DBManager();
94
+ const webSocketManager = new WebSocketManager({
95
+ loggerProvider,
96
+ logger
97
+ });
98
+ const dbManager = new DBManager({
99
+ loggerProvider,
100
+ logger
101
+ });
81
102
  const serverInterface = createServerInterface({
82
103
  cache,
83
104
  localCache,
84
105
  loggerProvider,
85
106
  webSocketManager,
86
- logger: loggerProvider.getLogger("server"),
107
+ logger,
87
108
  dbManager,
88
109
  port: PORT
89
110
  });
@@ -94,6 +115,7 @@ async function main() {
94
115
 
95
116
  ${!WSLIST ? "" : `\n webSocketManager.replaceALLWS(await Promise.all([${WSLIST}]))`}
96
117
  const app = new App({
118
+ loggerFactory: loggerProvider.requestLoggerFactory,
97
119
  onUpgrade: (req, socket, head) => {
98
120
  try {
99
121
  req.server = serverInterface;
package/src/lib.ts CHANGED
@@ -16,6 +16,7 @@ export { Miqro, MiqroOptions, ServerRequestHandler } from "./services/app.js";
16
16
  export { jsx2HTML } from "./common/jsx.js";
17
17
 
18
18
  //exported for --inflate-sea
19
+ export { createLogProviderOptions } from "./services/utils/log-transport.js";
19
20
  export { appendAPIModule } from "./inflate/utils/sea-utils.js";
20
21
  export { createServerInterface, ServerInterfaceImplOptions } from "./services/utils/server-interface.js";
21
22
  export { App, LoggerHandler, Router } from "@miqro/core";
@@ -1,5 +1,5 @@
1
1
  import cluster from "node:cluster";
2
- import { App, Router, Logger, LoggerHandler } from "@miqro/core";
2
+ import { App, Router, Logger, LoggerHandler, LogLevel } from "@miqro/core";
3
3
  import { migration } from "@miqro/query";
4
4
  import { WebSocketManager } from "./utils/websocketmanager.js";
5
5
  import { DBManager } from "./utils/db-manager.js";
@@ -8,7 +8,7 @@ import { InflateError } from "../common/jsx.js";
8
8
  import { DBConfig, MigrateOptions, ServerInterface, ServerRequest, WSConfig } from "../types.js";
9
9
  import { RouteFileMap } from "../inflate/setup-http.js";
10
10
  import { ServerConfigMap, setupServerConfig } from "../inflate/setup-server-config.js";
11
- import { BASEEDITOR_PATH, LOG_SOCKET_PATH } from "../../editor/common/constants.js";
11
+ import { BASEEDITOR_PATH, LOG_SOCKET_PATH, LOG_WRITE_EVENT } from "../../editor/common/constants.js";
12
12
  import editorWSConfig from "../../editor/ws.js";
13
13
  import editorServerConfig from "../../editor/server.js";
14
14
 
@@ -107,7 +107,11 @@ export class Miqro {
107
107
  services: [],
108
108
  ...(options ? options : {})
109
109
  };
110
- this.loggerProvider = new LogProvider(createLogProviderOptions(this));
110
+
111
+ const loggerOptions = createLogProviderOptions(this);
112
+ loggerOptions.transports.push(createEditorLoggerTransport(this));
113
+ this.loggerProvider = new LogProvider(loggerOptions);
114
+
111
115
  //if (!this.options.logger) {
112
116
  const SERVER_IDENTIFIER = cluster.isPrimary ?
113
117
  "" :
@@ -701,3 +705,31 @@ export function notifiyServerConfigSync(app: Miqro, method: "unload" | "stop") {
701
705
  });
702
706
  }
703
707
  }
708
+
709
+ function createEditorLoggerTransport(app: Miqro) {
710
+ return {
711
+ level: "trace" as LogLevel,
712
+ write: async (args) => {
713
+ try {
714
+ if (app.options.editor) {
715
+ try {
716
+ const ws = app.webSocketManager.getWS(LOG_SOCKET_PATH);
717
+ if (ws) {
718
+ //console.log("\n\n" + process.pid + " broadcasting " + LOG_SOCKET_PATH + "\n\n\n")
719
+ await ws.broadcast(JSON.stringify({
720
+ type: LOG_WRITE_EVENT,
721
+ level: args.level,
722
+ identifier: args.identifier,
723
+ out: args.out
724
+ }));
725
+ }
726
+ } catch (e) {
727
+ console.error(e);
728
+ }
729
+ }
730
+ } catch (e) {
731
+ console.error(e);
732
+ }
733
+ }
734
+ }
735
+ }
@@ -2,41 +2,65 @@ import { ConsoleTransport, FileTransport, LoggerTransport, LoggerTransportWriteA
2
2
  import { format } from "node:util";
3
3
  import { LOG_SOCKET_PATH, LOG_WRITE_EVENT } from "../../../editor/common/constants.js";
4
4
  import { Miqro } from "../app.js";
5
+ import { LogConfigMap } from "../../inflate/setup-log.js";
6
+ // import { WebSocketManager } from "./websocketmanager.js";
7
+ import { LogProviderOptions } from "./log.js";
5
8
 
6
- export function createLogProviderOptions(app: Miqro) {
9
+ export function createLogProviderOptions(app?: {
10
+ options?: {
11
+ dateTimeFormatOptions?: Intl.DateTimeFormatOptions;
12
+ logProviderOptions?: LogProviderOptions;
13
+ name?: string;
14
+ editor?: boolean;
15
+ logFile?: string | boolean;
16
+ },
17
+ inflated?: {
18
+ logConfigMap: LogConfigMap;
19
+ }
20
+ }) {
7
21
  const defaultConsole = ConsoleTransport();
8
22
  //console.log("app.options.logFile [%s]", app.options.logFile);
9
23
  const defaultFile: LoggerTransport | undefined =
10
- app.options.logFile !== true && app.options.logFile !== false && String(app.options.logFile).toUpperCase() !== "TRUE" && String(app.options.logFile).toUpperCase() !== "FALSE" &&
24
+ app && app.options && app?.options?.logFile !== true && app?.options?.logFile !== false && String(app?.options?.logFile).toUpperCase() !== "TRUE" && String(app?.options?.logFile).toUpperCase() !== "FALSE" &&
11
25
  app.options.logFile ? FileTransport(app.options.logFile) :
12
- String(app.options.logFile).toUpperCase() === "TRUE" || app.options.logFile === true || app.options.logFile === undefined ?
26
+ app && app.options && String(app?.options?.logFile).toUpperCase() === "TRUE" || app?.options?.logFile === true || app?.options?.logFile === undefined ?
13
27
  FileTransport() :
14
28
  undefined;
15
29
  const defaultWrite = async (args: LoggerTransportWriteArgs, level?: LogLevel) => {
16
30
  try {
17
- const serviceNamesWithLogConfigReplaceConsole = level === undefined && app.inflated ?
18
- Object.keys(app.inflated.logConfigMap).filter(serviceName => app.inflated.logConfigMap[serviceName].replaceConsoleTransport) : [];
19
- const serviceNamesWithLogConfigReplaceFile = level === undefined && app.inflated ?
20
- Object.keys(app.inflated.logConfigMap).filter(serviceName => app.inflated.logConfigMap[serviceName].replaceFileTransport) : [];
31
+ // console.dir(app.inflated);
32
+ const serviceNamesWithLogConfigReplaceConsole = app?.inflated ?
33
+ Object.keys(app?.inflated?.logConfigMap).filter(serviceName => app?.inflated?.logConfigMap[serviceName].replaceConsoleTransport) : [];
34
+ const serviceNamesWithLogConfigReplaceFile = app?.inflated ?
35
+ Object.keys(app?.inflated?.logConfigMap).filter(serviceName => app?.inflated?.logConfigMap[serviceName].replaceFileTransport) : [];
36
+ // console.dir(level);
37
+
38
+ // console.dir(app?.inflated?.logConfigMap["m-admin"])
39
+ // console.dir(serviceNamesWithLogConfigReplaceConsole.length);
40
+ // console.dir(serviceNamesWithLogConfigReplaceFile.length);
21
41
  await Promise.allSettled((level === undefined ?
22
42
  [
23
43
  level === undefined && serviceNamesWithLogConfigReplaceConsole.length === 0 && defaultConsole ?
24
44
  defaultConsole.write(args) : Promise.resolve(),
25
45
  level === undefined && serviceNamesWithLogConfigReplaceFile.length === 0 && defaultFile ?
26
46
  defaultFile.write(args) : Promise.resolve()
27
- ] : []).concat(app.inflated ?
28
- Object.keys(app.inflated.logConfigMap).map(serviceName => app.inflated.logConfigMap[serviceName]).filter(c => c.level === level).map(c => c.write(args)) : []
47
+ ] : []).concat(app && app.inflated && app?.inflated ?
48
+ Object.keys(app?.inflated?.logConfigMap).map(serviceName => app?.inflated?.logConfigMap[serviceName]).filter(c => c.level === level).map(c => c.write(args)) : []
29
49
  ));
30
50
  } catch (e) {
31
51
  console.error(e);
32
52
  }
33
53
  }
34
54
  return {
35
- name: app.options.name,
55
+ name: app?.options?.name,
36
56
  formatter: (args: WriteArgs) => {
37
57
  const params: string[] = args.optionalParams;
38
58
  return format(
39
- `${new Date().toISOString()} PID[${process.pid}] ${args.identifier ? `[${args.identifier}] ` : ""}${args.level !== "info" ? (args.level === "error" || args.level === "warn" ? `[${args.level.toUpperCase()}] ` : `[${args.level}] `) : ""}${args.message}`,
59
+ `${new Date().toLocaleDateString(undefined, app?.options?.dateTimeFormatOptions ? app?.options?.dateTimeFormatOptions : {
60
+ hour12: false
61
+ })} ${new Date().toLocaleTimeString(undefined, app?.options?.dateTimeFormatOptions ? app?.options?.dateTimeFormatOptions : {
62
+ hour12: false
63
+ })} PID[${process.pid}] ${args.identifier ? `[${args.identifier}] ` : ""}${args.level !== "info" ? (args.level === "error" || args.level === "warn" ? `[${args.level.toUpperCase()}] ` : `[${args.level}] `) : ""}${args.message}`,
40
64
  ...params)
41
65
  },
42
66
  transports: [
@@ -51,31 +75,7 @@ export function createLogProviderOptions(app: Miqro) {
51
75
  await defaultWrite(args, undefined);
52
76
  }
53
77
  }
54
- })), {
55
- level: "trace" as LogLevel,
56
- write: async (args) => {
57
- try {
58
- if (app.options.editor) {
59
- try {
60
- const ws = app.webSocketManager.getWS(LOG_SOCKET_PATH);
61
- if (ws) {
62
- //console.log("\n\n" + process.pid + " broadcasting " + LOG_SOCKET_PATH + "\n\n\n")
63
- await ws.broadcast(JSON.stringify({
64
- type: LOG_WRITE_EVENT,
65
- level: args.level,
66
- identifier: args.identifier,
67
- out: args.out
68
- }));
69
- }
70
- } catch (e) {
71
- console.error(e);
72
- }
73
- }
74
- } catch (e) {
75
- console.error(e);
76
- }
77
- }
78
- }],
79
- ...(app.options.logProviderOptions ? app.options.logProviderOptions : {})
78
+ }))],
79
+ ...(app?.options?.logProviderOptions ? app?.options?.logProviderOptions : {})
80
80
  };
81
81
  }