@nocobase/server 0.18.0-alpha.1 → 0.18.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.
@@ -152,6 +152,7 @@ const _AppSupervisor = class _AppSupervisor extends import_events.EventEmitter {
152
152
  if (this.apps[app.name]) {
153
153
  throw new Error(`app ${app.name} already exists`);
154
154
  }
155
+ app.logger.info(`add app ${app.name} into supervisor`, { submodule: "supervisor", method: "addApp" });
155
156
  this.bindAppEvents(app);
156
157
  this.apps[app.name] = app;
157
158
  this.emit("afterAppAdded", app);
@@ -5,13 +5,14 @@ import { ACL } from '@nocobase/acl';
5
5
  import { AuthManager, AuthManagerOptions } from '@nocobase/auth';
6
6
  import { Cache, CacheManager, CacheManagerOptions } from '@nocobase/cache';
7
7
  import Database, { CollectionOptions, IDatabaseOptions } from '@nocobase/database';
8
- import { AppLoggerOptions, Logger } from '@nocobase/logger';
8
+ import { AppLogger, AppLoggerOptions, LoggerOptions } from '@nocobase/logger';
9
9
  import { ResourceOptions, Resourcer } from '@nocobase/resourcer';
10
10
  import { AsyncEmitter, ToposortOptions } from '@nocobase/utils';
11
11
  import { Command, CommandOptions, ParseOptions } from 'commander';
12
12
  import { IncomingMessage, Server, ServerResponse } from 'http';
13
13
  import { i18n, InitOptions } from 'i18next';
14
14
  import Koa, { DefaultContext as KoaDefaultContext, DefaultState as KoaDefaultState } from 'koa';
15
+ import { RecordableHistogram } from 'node:perf_hooks';
15
16
  import { AppCommand } from './app-command';
16
17
  import { AppSupervisor } from './app-supervisor';
17
18
  import { CronJobManager } from './cron/cron-job-manager';
@@ -19,7 +20,6 @@ import { ApplicationVersion } from './helpers/application-version';
19
20
  import { Locale } from './locale';
20
21
  import { Plugin } from './plugin';
21
22
  import { InstallOptions, PluginManager } from './plugin-manager';
22
- import { RecordableHistogram } from 'node:perf_hooks';
23
23
  export type PluginType = string | typeof Plugin;
24
24
  export type PluginConfiguration = PluginType | [PluginType, any];
25
25
  export interface ResourcerOptions {
@@ -101,8 +101,8 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
101
101
  get cronJobManager(): CronJobManager;
102
102
  protected _db: Database;
103
103
  get db(): Database;
104
- protected _logger: Logger;
105
- get logger(): Logger;
104
+ protected _logger: AppLogger;
105
+ get logger(): AppLogger;
106
106
  protected _resourcer: Resourcer;
107
107
  get resourcer(): Resourcer;
108
108
  protected _cacheManager: CacheManager;
@@ -125,7 +125,7 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
125
125
  get localeManager(): Locale;
126
126
  protected _version: ApplicationVersion;
127
127
  get version(): ApplicationVersion;
128
- get log(): Logger;
128
+ get log(): AppLogger;
129
129
  get name(): string;
130
130
  isMaintaining(): boolean;
131
131
  getMaintaining(): MaintainingCommandStatus;
@@ -149,6 +149,7 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
149
149
  createCli(): AppCommand;
150
150
  runAsCLI(argv?: string[], options?: ParseOptions & {
151
151
  throwError?: boolean;
152
+ reqId?: string;
152
153
  }): Promise<AppCommand>;
153
154
  start(options?: StartOptions): Promise<void>;
154
155
  emitStartedEvent(options?: StartOptions): Promise<void>;
@@ -165,6 +166,7 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
165
166
  name: string;
166
167
  };
167
168
  reInitEvents(): void;
169
+ createLogger(options: LoggerOptions): import("winston").Logger;
168
170
  protected init(): void;
169
171
  protected createDatabase(options: ApplicationOptions): Database;
170
172
  }
@@ -37,6 +37,8 @@ var import_auth = require("@nocobase/auth");
37
37
  var import_database = __toESM(require("@nocobase/database"));
38
38
  var import_logger = require("@nocobase/logger");
39
39
  var import_utils = require("@nocobase/utils");
40
+ var import_chalk = __toESM(require("chalk"));
41
+ var import_crypto = require("crypto");
40
42
  var import_koa = __toESM(require("koa"));
41
43
  var import_koa_compose = __toESM(require("koa-compose"));
42
44
  var import_lodash = __toESM(require("lodash"));
@@ -52,11 +54,11 @@ var import_application_version = require("./helpers/application-version");
52
54
  var import_locale = require("./locale");
53
55
  var import_plugin_manager = require("./plugin-manager");
54
56
  var import_package = __toESM(require("../package.json"));
55
- var import_chalk = __toESM(require("chalk"));
56
57
  const _Application = class _Application extends import_koa.default {
57
58
  constructor(options) {
58
59
  super();
59
60
  this.options = options;
61
+ this.context.reqId = (0, import_crypto.randomUUID)();
60
62
  this.rawOptions = this.name == "main" ? import_lodash.default.cloneDeep(options) : {};
61
63
  this.init();
62
64
  this._appSupervisor.addApp(this);
@@ -174,7 +176,7 @@ const _Application = class _Application extends import_koa.default {
174
176
  return import_package.default.version;
175
177
  }
176
178
  plugin(pluginClass, options) {
177
- this.log.debug(`add plugin ${pluginClass.name}`);
179
+ this.log.debug(`add plugin`, { method: "plugin", name: pluginClass.name });
178
180
  this.pm.addPreset(pluginClass, options);
179
181
  }
180
182
  // @ts-ignore
@@ -212,7 +214,7 @@ const _Application = class _Application extends import_koa.default {
212
214
  }
213
215
  if (options == null ? void 0 : options.reload) {
214
216
  this.setMaintainingMessage("app reload");
215
- this.log.info(`app.reload()`);
217
+ this.log.info(`app.reload()`, { method: "load" });
216
218
  const oldDb = this._db;
217
219
  this.init();
218
220
  if (!oldDb.closed()) {
@@ -234,17 +236,17 @@ const _Application = class _Application extends import_koa.default {
234
236
  this._loaded = true;
235
237
  }
236
238
  async reload(options) {
237
- this.log.debug(`start reload`);
239
+ this.log.debug(`start reload`, { method: "reload" });
238
240
  this._loaded = false;
239
241
  await this.emitAsync("beforeReload", this, options);
240
242
  await this.load({
241
243
  ...options,
242
244
  reload: true
243
245
  });
244
- this.log.debug("emit afterReload");
246
+ this.log.debug("emit afterReload", { method: "reload" });
245
247
  this.setMaintainingMessage("emit afterReload");
246
248
  await this.emitAsync("afterReload", this, options);
247
- this.log.debug(`finish reload`);
249
+ this.log.debug(`finish reload`, { method: "reload" });
248
250
  }
249
251
  getPlugin(name) {
250
252
  return this.pm.get(name);
@@ -295,6 +297,10 @@ const _Application = class _Application extends import_koa.default {
295
297
  if (this.activatedCommand) {
296
298
  return;
297
299
  }
300
+ if (options.reqId) {
301
+ this.context.reqId = options.reqId;
302
+ this._logger = this._logger.child({ reqId: this.context.reqId });
303
+ }
298
304
  this._maintainingStatusBeforeCommand = this._maintainingCommandStatus;
299
305
  try {
300
306
  const command = await this.cli.parseAsync(argv, options);
@@ -381,37 +387,37 @@ const _Application = class _Application extends import_koa.default {
381
387
  this.emit("__restarted", this, options);
382
388
  }
383
389
  async stop(options = {}) {
384
- this.log.debug("stop app...");
390
+ this.log.debug("stop app...", { method: "stop" });
385
391
  this.setMaintainingMessage("stopping app...");
386
392
  if (this.stopped) {
387
- this.log.warn(`Application ${this.name} already stopped`);
393
+ this.log.warn(`Application ${this.name} already stopped`, { method: "stop" });
388
394
  return;
389
395
  }
390
396
  await this.emitAsync("beforeStop", this, options);
391
397
  try {
392
398
  if (!this.db.closed()) {
393
- this.logger.info(`close db`);
399
+ this.log.info(`close db`, { method: "stop" });
394
400
  await this.db.close();
395
401
  }
396
402
  } catch (e) {
397
- this.log.error(e);
403
+ this.log.error(e.message, { method: "stop", err: e.stack });
398
404
  }
399
405
  if (this._cacheManager) {
400
406
  await this._cacheManager.close();
401
407
  }
402
408
  await this.emitAsync("afterStop", this, options);
403
409
  this.stopped = true;
404
- this.log.info(`${this.name} is stopped`);
410
+ this.log.info(`${this.name} is stopped`, { method: "stop" });
405
411
  this._started = false;
406
412
  }
407
413
  async destroy(options = {}) {
408
- this.logger.debug("start destroy app");
414
+ this.log.debug("start destroy app", { method: "destory" });
409
415
  this.setMaintainingMessage("destroying app...");
410
416
  await this.emitAsync("beforeDestroy", this, options);
411
417
  await this.stop(options);
412
- this.logger.debug("emit afterDestroy");
418
+ this.log.debug("emit afterDestroy", { method: "destory" });
413
419
  await this.emitAsync("afterDestroy", this, options);
414
- this.logger.debug("finish destroy app");
420
+ this.log.debug("finish destroy app", { method: "destory" });
415
421
  }
416
422
  async isInstalled() {
417
423
  return await this.db.collectionExistsInDb("applicationVersion") || await this.db.collectionExistsInDb("collections");
@@ -419,24 +425,24 @@ const _Application = class _Application extends import_koa.default {
419
425
  async install(options = {}) {
420
426
  var _a, _b;
421
427
  this.setMaintainingMessage("installing app...");
422
- this.log.debug("Database dialect: " + this.db.sequelize.getDialect());
428
+ this.log.debug("Database dialect: " + this.db.sequelize.getDialect(), { method: "install" });
423
429
  if ((options == null ? void 0 : options.clean) || ((_a = options == null ? void 0 : options.sync) == null ? void 0 : _a.force)) {
424
- this.log.debug("truncate database");
430
+ this.log.debug("truncate database", { method: "install" });
425
431
  await this.db.clean({ drop: true });
426
- this.log.debug("app reloading");
432
+ this.log.debug("app reloading", { method: "install" });
427
433
  await this.reload();
428
434
  } else if (await this.isInstalled()) {
429
- this.log.warn("app is installed");
435
+ this.log.warn("app is installed", { method: "install" });
430
436
  return;
431
437
  }
432
- this.log.debug("emit beforeInstall");
438
+ this.log.debug("emit beforeInstall", { method: "install" });
433
439
  this.setMaintainingMessage("call beforeInstall hook...");
434
440
  await this.emitAsync("beforeInstall", this, options);
435
- this.log.debug("start install plugins");
441
+ this.log.debug("start install plugins", { method: "install" });
436
442
  await this.pm.install(options);
437
- this.log.debug("update version");
443
+ this.log.debug("update version", { method: "install" });
438
444
  await this.version.update();
439
- this.log.debug("emit afterInstall");
445
+ this.log.debug("emit afterInstall", { method: "install" });
440
446
  this.setMaintainingMessage("call afterInstall hook...");
441
447
  await this.emitAsync("afterInstall", this, options);
442
448
  if ((_b = this._maintainingStatusBeforeCommand) == null ? void 0 : _b.error) {
@@ -484,21 +490,28 @@ const _Application = class _Application extends import_koa.default {
484
490
  }
485
491
  }
486
492
  }
493
+ createLogger(options) {
494
+ const { dirname } = options;
495
+ return (0, import_logger.createLogger)({
496
+ ...options,
497
+ dirname: (0, import_logger.getLoggerFilePath)(this.name || "main", dirname || "")
498
+ });
499
+ }
487
500
  init() {
501
+ var _a;
488
502
  const options = this.options;
489
- const logger = (0, import_logger.createAppLogger)({
490
- ...options.logger,
491
- defaultMeta: {
492
- app: this.name
493
- }
503
+ this._logger = (0, import_logger.createAppLogger)({
504
+ app: this.name,
505
+ ...((_a = options.logger) == null ? void 0 : _a.system) || {}
506
+ }).child({
507
+ reqId: this.context.reqId,
508
+ module: "application"
494
509
  });
495
- this._logger = logger.instance;
496
510
  this.reInitEvents();
497
511
  this.middleware = new import_utils.Toposort();
498
512
  this.plugins = /* @__PURE__ */ new Map();
499
513
  this._acl = (0, import_acl2.createACL)();
500
514
  this._cronJobManager = new import_cron_job_manager.CronJobManager(this);
501
- this.use(logger.middleware, { tag: "logger" });
502
515
  if (this._db) {
503
516
  this._db.removeAllListeners();
504
517
  }
@@ -507,7 +520,6 @@ const _Application = class _Application extends import_koa.default {
507
520
  this._cli = this.createCli();
508
521
  this._i18n = (0, import_helper.createI18n)(options);
509
522
  this.context.db = this._db;
510
- this.context.logger = this._logger;
511
523
  this.context.resourcer = this._resourcer;
512
524
  this.context.cacheManager = this._cacheManager;
513
525
  this.context.cache = this._cache;
@@ -541,13 +553,28 @@ const _Application = class _Application extends import_koa.default {
541
553
  this._version = new import_application_version.ApplicationVersion(this);
542
554
  }
543
555
  createDatabase(options) {
556
+ const sqlLogger = this.createLogger({
557
+ filename: "sql",
558
+ level: "debug"
559
+ });
560
+ const logging = /* @__PURE__ */ __name((msg) => {
561
+ if (typeof msg === "string") {
562
+ msg = msg.replace(/[\r\n]/gm, "").replace(/\s+/g, " ");
563
+ }
564
+ if (msg.includes("INSERT INTO")) {
565
+ msg = msg.substring(0, 2e3) + "...";
566
+ }
567
+ sqlLogger.debug({ reqId: this.context.reqId, message: msg });
568
+ }, "logging");
569
+ const dbOptions = options.database instanceof import_database.default ? options.database.options : options.database;
544
570
  const db = new import_database.default({
545
- ...options.database instanceof import_database.default ? options.database.options : options.database,
571
+ ...dbOptions,
572
+ logging: dbOptions.logging ? logging : false,
546
573
  migrator: {
547
574
  context: { app: this }
548
- }
575
+ },
576
+ logger: this._logger.child({ module: "database" })
549
577
  });
550
- db.setLogger(this._logger);
551
578
  return db;
552
579
  }
553
580
  };
@@ -41,6 +41,7 @@ export declare class Gateway extends EventEmitter {
41
41
  destroy(): void;
42
42
  reset(): void;
43
43
  addAppSelectorMiddleware(middleware: AppSelectorMiddleware, options?: ToposortOptions): void;
44
+ logger(req: IncomingRequest): Promise<import("winston").Logger>;
44
45
  responseError(res: ServerResponse, error: {
45
46
  status: number;
46
47
  maintaining: boolean;
@@ -56,6 +56,8 @@ var import_errors = require("./errors");
56
56
  var import_ipc_socket_client = require("./ipc-socket-client");
57
57
  var import_ipc_socket_server = require("./ipc-socket-server");
58
58
  var import_ws_server = require("./ws-server");
59
+ var import_logger = require("@nocobase/logger");
60
+ var import_crypto = require("crypto");
59
61
  const compress = (0, import_node_util.promisify)((0, import_compression.default)());
60
62
  const _Gateway = class _Gateway extends import_events.EventEmitter {
61
63
  /**
@@ -121,6 +123,12 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
121
123
  this.selectorMiddlewares.add(middleware, options);
122
124
  this.emit("appSelectorChanged");
123
125
  }
126
+ async logger(req) {
127
+ const reqId = (0, import_crypto.randomUUID)();
128
+ const appName = await this.getRequestHandleAppName(req);
129
+ req.headers["reqId"] = reqId;
130
+ return (0, import_logger.createLogger)({ filename: `${appName}_request` }).child({ reqId });
131
+ }
124
132
  responseError(res, error) {
125
133
  res.setHeader("Content-Type", "application/json");
126
134
  res.statusCode = error.status;
@@ -2,12 +2,15 @@
2
2
  /// <reference types="node" />
3
3
  import net from 'net';
4
4
  import * as events from 'events';
5
+ import { Logger } from '@nocobase/logger';
5
6
  export declare const writeJSON: (socket: net.Socket, data: object) => void;
6
7
  export declare class IPCSocketClient extends events.EventEmitter {
7
8
  client: net.Socket;
9
+ logger: Logger;
8
10
  constructor(client: net.Socket);
9
11
  static getConnection(serverPath: string): Promise<IPCSocketClient>;
10
- handleServerMessage({ type, payload }: {
12
+ handleServerMessage({ reqId, type, payload }: {
13
+ reqId: any;
11
14
  type: any;
12
15
  payload: any;
13
16
  }): Promise<void>;
@@ -34,13 +34,16 @@ __export(ipc_socket_client_exports, {
34
34
  module.exports = __toCommonJS(ipc_socket_client_exports);
35
35
  var import_net = __toESM(require("net"));
36
36
  var events = __toESM(require("events"));
37
+ var import_logger = require("@nocobase/logger");
37
38
  const writeJSON = /* @__PURE__ */ __name((socket, data) => {
38
39
  socket.write(JSON.stringify(data) + "\n", "utf8");
39
40
  }, "writeJSON");
40
41
  const _IPCSocketClient = class _IPCSocketClient extends events.EventEmitter {
41
42
  client;
43
+ logger;
42
44
  constructor(client) {
43
45
  super();
46
+ this.logger = (0, import_logger.createConsoleLogger)();
44
47
  this.client = client;
45
48
  this.client.on("data", (data) => {
46
49
  const dataAsString = data.toString();
@@ -64,19 +67,19 @@ const _IPCSocketClient = class _IPCSocketClient extends events.EventEmitter {
64
67
  });
65
68
  });
66
69
  }
67
- async handleServerMessage({ type, payload }) {
70
+ async handleServerMessage({ reqId, type, payload }) {
68
71
  switch (type) {
69
72
  case "error":
70
- console.error(payload.message);
73
+ this.logger.error({ reqId, message: `${payload.message}|${payload.stack}` });
71
74
  break;
72
75
  case "success":
73
- console.log("success");
76
+ this.logger.info({ reqId, message: "success" });
74
77
  break;
75
78
  default:
76
- console.log({ type, payload });
79
+ this.logger.info({ reqId, message: JSON.stringify({ type, payload }) });
77
80
  break;
78
81
  }
79
- this.emit("response", { type, payload });
82
+ this.emit("response", { reqId, type, payload });
80
83
  }
81
84
  close() {
82
85
  this.client.end();
@@ -4,7 +4,8 @@ export declare class IPCSocketServer {
4
4
  socketServer: net.Server;
5
5
  constructor(server: net.Server);
6
6
  static buildServer(socketPath: string): IPCSocketServer;
7
- static handleClientMessage({ type, payload }: {
7
+ static handleClientMessage({ reqId, type, payload }: {
8
+ reqId: any;
8
9
  type: any;
9
10
  payload: any;
10
11
  }): Promise<import("../app-command").AppCommand>;
@@ -35,6 +35,7 @@ var import_net = __toESM(require("net"));
35
35
  var import_fs = __toESM(require("fs"));
36
36
  var import_app_supervisor = require("../app-supervisor");
37
37
  var import_ipc_socket_client = require("./ipc-socket-client");
38
+ var import_crypto = require("crypto");
38
39
  const _IPCSocketServer = class _IPCSocketServer {
39
40
  socketServer;
40
41
  constructor(server) {
@@ -56,16 +57,20 @@ const _IPCSocketServer = class _IPCSocketServer {
56
57
  if (message.length === 0) {
57
58
  continue;
58
59
  }
60
+ const reqId = (0, import_crypto.randomUUID)();
59
61
  const dataObj = JSON.parse(message);
60
- _IPCSocketServer.handleClientMessage(dataObj).then(() => {
62
+ _IPCSocketServer.handleClientMessage({ reqId, ...dataObj }).then(() => {
61
63
  (0, import_ipc_socket_client.writeJSON)(c, {
64
+ reqId,
62
65
  type: "success"
63
66
  });
64
67
  }).catch((err) => {
65
68
  (0, import_ipc_socket_client.writeJSON)(c, {
69
+ reqId,
66
70
  type: "error",
67
71
  payload: {
68
- message: err.message
72
+ message: err.message,
73
+ stack: err.stack
69
74
  }
70
75
  });
71
76
  });
@@ -77,7 +82,7 @@ const _IPCSocketServer = class _IPCSocketServer {
77
82
  });
78
83
  return new _IPCSocketServer(socketServer);
79
84
  }
80
- static async handleClientMessage({ type, payload }) {
85
+ static async handleClientMessage({ reqId, type, payload }) {
81
86
  console.log(`cli received message ${type}`);
82
87
  if (type === "passCliArgv") {
83
88
  const argv = payload.argv;
@@ -89,6 +94,7 @@ const _IPCSocketServer = class _IPCSocketServer {
89
94
  throw new Error("Not handle by ipc server");
90
95
  }
91
96
  return mainApp.runAsCLI(argv, {
97
+ reqId,
92
98
  from: "node",
93
99
  throwError: true
94
100
  });
@@ -1,6 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import WebSocket from 'ws';
3
3
  import { IncomingMessage } from 'http';
4
+ import { Logger } from '@nocobase/logger';
4
5
  declare class WebSocketWithId extends WebSocket {
5
6
  id: string;
6
7
  }
@@ -14,6 +15,7 @@ interface WebSocketClient {
14
15
  export declare class WSServer {
15
16
  wss: WebSocket.Server;
16
17
  webSocketClients: Map<string, WebSocketClient>;
18
+ logger: Logger;
17
19
  constructor();
18
20
  addNewConnection(ws: WebSocketWithId, request: IncomingMessage): WebSocketClient;
19
21
  setClientApp(client: WebSocketClient): Promise<void>;
@@ -45,6 +45,7 @@ __name(getPayloadByErrorCode, "getPayloadByErrorCode");
45
45
  const _WSServer = class _WSServer {
46
46
  wss;
47
47
  webSocketClients = /* @__PURE__ */ new Map();
48
+ logger;
48
49
  constructor() {
49
50
  this.wss = new import_ws.WebSocketServer({ noServer: true });
50
51
  this.wss.on("connection", (ws, request) => {
package/lib/helper.js CHANGED
@@ -51,6 +51,8 @@ var import_data_template = require("./middlewares/data-template");
51
51
  var import_data_wrapping = require("./middlewares/data-wrapping");
52
52
  var import_db2resource = require("./middlewares/db2resource");
53
53
  var import_i18n = require("./middlewares/i18n");
54
+ var import_logger = require("@nocobase/logger");
55
+ var import_crypto = require("crypto");
54
56
  var import_perf_hooks = require("perf_hooks");
55
57
  function createI18n(options) {
56
58
  const instance = import_i18next.default.createInstance();
@@ -77,6 +79,11 @@ function createResourcer(options) {
77
79
  }
78
80
  __name(createResourcer, "createResourcer");
79
81
  function registerMiddlewares(app, options) {
82
+ app.use(async (ctx, next) => {
83
+ app.context.reqId = (0, import_crypto.randomUUID)();
84
+ await next();
85
+ });
86
+ app.use((0, import_logger.requestLogger)(app.name, options.logger), { tag: "logger" });
80
87
  app.use(
81
88
  (0, import_cors.default)({
82
89
  exposeHeaders: ["content-disposition"],
@@ -5,6 +5,6 @@ export declare class ApplicationVersion {
5
5
  protected collection: Collection;
6
6
  constructor(app: Application);
7
7
  get(): Promise<any>;
8
- update(): Promise<void>;
8
+ update(version?: string): Promise<void>;
9
9
  satisfies(range: string): Promise<boolean>;
10
10
  }
@@ -58,13 +58,13 @@ const _ApplicationVersion = class _ApplicationVersion {
58
58
  }
59
59
  return null;
60
60
  }
61
- async update() {
61
+ async update(version) {
62
62
  await this.collection.sync();
63
63
  await this.collection.model.destroy({
64
64
  truncate: true
65
65
  });
66
66
  await this.collection.model.create({
67
- value: this.app.getVersion()
67
+ value: version || this.app.getVersion()
68
68
  });
69
69
  }
70
70
  async satisfies(range) {
@@ -33,10 +33,10 @@ const _Locale = class _Locale {
33
33
  constructor(app) {
34
34
  this.app = app;
35
35
  this.app.on("afterLoad", async () => {
36
- this.app.log.debug("load locale resource");
36
+ this.app.log.debug("load locale resource", { submodule: "locale", method: "onAfterLoad" });
37
37
  this.app.setMaintainingMessage("load locale resource");
38
38
  await this.load();
39
- this.app.log.debug("locale resource loaded");
39
+ this.app.log.debug("locale resource loaded", { submodule: "locale", method: "onAfterLoad" });
40
40
  this.app.setMaintainingMessage("locale resource loaded");
41
41
  });
42
42
  }
@@ -97,8 +97,8 @@ const _Locale = class _Locale {
97
97
  const res = (0, import_resource.getResource)(packageName, lang);
98
98
  if (res) {
99
99
  resources[packageName] = { ...res };
100
- if (name !== packageName) {
101
- resources[name] = { ...res };
100
+ if (packageName.includes("@nocobase/plugin-")) {
101
+ resources[packageName.substring("@nocobase/plugin-".length)] = { ...res };
102
102
  }
103
103
  }
104
104
  } catch (err) {
@@ -49,7 +49,7 @@ const _package_name_unique_default = class _package_name_unique_default extends
49
49
  if (plugin.packageName) {
50
50
  continue;
51
51
  }
52
- const packageName = import_plugin_manager.PluginManager.getPackageName(name);
52
+ const packageName = await import_plugin_manager.PluginManager.getPackageName(name);
53
53
  await repository.update({
54
54
  filter: {
55
55
  name
@@ -221,7 +221,7 @@ const _PluginManager = class _PluginManager {
221
221
  } catch (error) {
222
222
  console.error(error);
223
223
  }
224
- this.app.log.debug(`adding plugin [${options.name}]...`);
224
+ this.app.log.debug(`adding plugin...`, { method: "add", submodule: "plugin-manager", name: options.name });
225
225
  let P;
226
226
  try {
227
227
  P = await _PluginManager.resolvePlugin(options.packageName || plugin, isUpgrade, !!options.packageName);
@@ -262,7 +262,7 @@ const _PluginManager = class _PluginManager {
262
262
  if (!plugin.enabled) {
263
263
  continue;
264
264
  }
265
- this.app.logger.debug(`before load plugin [${name}]...`);
265
+ this.app.logger.debug(`before load plugin...`, { submodule: "plugin-manager", method: "load", name });
266
266
  await plugin.beforeLoad();
267
267
  }
268
268
  current = 0;
@@ -277,11 +277,11 @@ const _PluginManager = class _PluginManager {
277
277
  continue;
278
278
  }
279
279
  await this.app.emitAsync("beforeLoadPlugin", plugin, options);
280
- this.app.logger.debug(`loading plugin [${name}]...`);
280
+ this.app.logger.debug(`loading plugin...`, { submodule: "plugin-manager", method: "load", name });
281
281
  await plugin.load();
282
282
  plugin.state.loaded = true;
283
283
  await this.app.emitAsync("afterLoadPlugin", plugin, options);
284
- this.app.logger.debug(`after load plugin [${name}]...`);
284
+ this.app.logger.debug(`after load plugin...`, { submodule: "plugin-manager", method: "load", name });
285
285
  }
286
286
  this.app.setMaintainingMessage("loaded plugins");
287
287
  }
@@ -490,11 +490,11 @@ const _PluginManager = class _PluginManager {
490
490
  const name = plugin.getName();
491
491
  await plugin.beforeLoad();
492
492
  await this.app.emitAsync("beforeLoadPlugin", plugin, {});
493
- this.app.logger.debug(`loading plugin [${name}]...`);
493
+ this.app.logger.debug(`loading plugin...`, { submodule: "plugin-manager", method: "loadOne", name });
494
494
  await plugin.load();
495
495
  plugin.state.loaded = true;
496
496
  await this.app.emitAsync("afterLoadPlugin", plugin, {});
497
- this.app.logger.debug(`after load plugin [${name}]...`);
497
+ this.app.logger.debug(`after load plugin...`, { submodule: "plugin-manager", method: "loadOne", name });
498
498
  this.app.setMaintainingMessage(`loaded plugin ${plugin.name}`);
499
499
  }
500
500
  async addViaCLI(urlOrName, options) {
package/lib/plugin.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { Model } from '@nocobase/database';
2
+ import { LoggerOptions } from '@nocobase/logger';
3
+ import type { TFuncKey, TOptions } from 'i18next';
2
4
  import { Application } from './application';
3
5
  import { InstallOptions } from './plugin-manager';
4
6
  export interface PluginInterface {
@@ -33,6 +35,7 @@ export declare abstract class Plugin<O = any> implements PluginInterface {
33
35
  set installed(value: any);
34
36
  setOptions(options: any): void;
35
37
  getName(): any;
38
+ createLogger(options: LoggerOptions): import("winston").Logger;
36
39
  afterAdd(): void;
37
40
  beforeLoad(): void;
38
41
  load(): Promise<void>;
@@ -45,6 +48,7 @@ export declare abstract class Plugin<O = any> implements PluginInterface {
45
48
  afterRemove(): Promise<void>;
46
49
  importCollections(collectionsPath: string): Promise<void>;
47
50
  requiredPlugins(): any[];
51
+ t(text: TFuncKey | TFuncKey[], options?: TOptions): import("i18next").TFunctionDetailedResult<object>;
48
52
  toJSON(options?: any): Promise<any>;
49
53
  }
50
54
  export default Plugin;
package/lib/plugin.js CHANGED
@@ -46,7 +46,10 @@ const _Plugin = class _Plugin {
46
46
  this.setOptions(options);
47
47
  }
48
48
  get log() {
49
- return this.app.log;
49
+ return this.app.log.child({
50
+ reqId: this.app.context.reqId,
51
+ module: this.name
52
+ });
50
53
  }
51
54
  get name() {
52
55
  return this.options.name;
@@ -75,6 +78,9 @@ const _Plugin = class _Plugin {
75
78
  getName() {
76
79
  return this.options.name;
77
80
  }
81
+ createLogger(options) {
82
+ return this.app.createLogger(options);
83
+ }
78
84
  afterAdd() {
79
85
  }
80
86
  beforeLoad() {
@@ -104,6 +110,9 @@ const _Plugin = class _Plugin {
104
110
  requiredPlugins() {
105
111
  return [];
106
112
  }
113
+ t(text, options = {}) {
114
+ return this.app.i18n.t(text, { ns: this.options["packageName"], ...options });
115
+ }
107
116
  async toJSON(options = {}) {
108
117
  const { locale = "en-US" } = options;
109
118
  const { name, packageName, packageJson } = this.options;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/server",
3
- "version": "0.18.0-alpha.1",
3
+ "version": "0.18.0-alpha.8",
4
4
  "main": "lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "license": "Apache-2.0",
@@ -10,16 +10,16 @@
10
10
  "@koa/cors": "^3.1.0",
11
11
  "@koa/multer": "^3.0.2",
12
12
  "@koa/router": "^9.4.0",
13
- "@nocobase/acl": "0.18.0-alpha.1",
14
- "@nocobase/actions": "0.18.0-alpha.1",
15
- "@nocobase/auth": "0.18.0-alpha.1",
16
- "@nocobase/cache": "0.18.0-alpha.1",
17
- "@nocobase/database": "0.18.0-alpha.1",
18
- "@nocobase/evaluators": "0.18.0-alpha.1",
19
- "@nocobase/logger": "0.18.0-alpha.1",
20
- "@nocobase/resourcer": "0.18.0-alpha.1",
21
- "@nocobase/sdk": "0.18.0-alpha.1",
22
- "@nocobase/utils": "0.18.0-alpha.1",
13
+ "@nocobase/acl": "0.18.0-alpha.8",
14
+ "@nocobase/actions": "0.18.0-alpha.8",
15
+ "@nocobase/auth": "0.18.0-alpha.8",
16
+ "@nocobase/cache": "0.18.0-alpha.8",
17
+ "@nocobase/database": "0.18.0-alpha.8",
18
+ "@nocobase/evaluators": "0.18.0-alpha.8",
19
+ "@nocobase/logger": "0.18.0-alpha.8",
20
+ "@nocobase/resourcer": "0.18.0-alpha.8",
21
+ "@nocobase/sdk": "0.18.0-alpha.8",
22
+ "@nocobase/utils": "0.18.0-alpha.8",
23
23
  "@types/decompress": "4.2.4",
24
24
  "@types/ini": "^1.3.31",
25
25
  "@types/koa-send": "^4.1.3",
@@ -52,5 +52,5 @@
52
52
  "@types/serve-handler": "^6.1.1",
53
53
  "@types/ws": "^8.5.5"
54
54
  },
55
- "gitHead": "0f5f1c0a37dc397a9dc4c8eec0c4ec20fd8107b0"
55
+ "gitHead": "727d42f6f14e5f863831da3dbf3255ba1165b567"
56
56
  }