@nocobase/logger 0.18.0-alpha.7 → 0.18.0-alpha.9

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/lib/config.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export declare const getLoggerLevel: () => string;
2
2
  export declare const getLoggerFilePath: (...paths: string[]) => string;
3
3
  export declare const getLoggerTransport: () => ('console' | 'file' | 'dailyRotateFile')[];
4
- export declare const getLoggerFormat: () => 'logfmt' | 'json' | 'delimiter';
4
+ export declare const getLoggerFormat: () => 'logfmt' | 'json' | 'delimiter' | 'console';
package/lib/config.js CHANGED
@@ -40,7 +40,7 @@ const getLoggerFilePath = /* @__PURE__ */ __name((...paths) => {
40
40
  return import_path.default.resolve(process.env.LOGGER_BASE_PATH || import_path.default.resolve(process.cwd(), "storage", "logs"), ...paths);
41
41
  }, "getLoggerFilePath");
42
42
  const getLoggerTransport = /* @__PURE__ */ __name(() => (process.env.LOGGER_TRANSPORT || (process.env.APP_ENV === "development" ? "console" : "console,dailyRotateFile")).split(","), "getLoggerTransport");
43
- const getLoggerFormat = /* @__PURE__ */ __name(() => process.env.LOGGER_FORMAT || (process.env.APP_ENV === "development" ? "logfmt" : "json"), "getLoggerFormat");
43
+ const getLoggerFormat = /* @__PURE__ */ __name(() => process.env.LOGGER_FORMAT || (process.env.APP_ENV === "development" ? "console" : "json"), "getLoggerFormat");
44
44
  // Annotate the CommonJS export names for ESM import in node:
45
45
  0 && (module.exports = {
46
46
  getLoggerFilePath,
package/lib/format.d.ts CHANGED
@@ -4,6 +4,7 @@ export declare const getFormat: (format?: LoggerOptions['format']) => winston.Lo
4
4
  export declare const colorFormat: winston.Logform.Format;
5
5
  export declare const stripColorFormat: winston.Logform.Format;
6
6
  export declare const logfmtFormat: winston.Logform.Format;
7
+ export declare const consoleFormat: winston.Logform.Format;
7
8
  export declare const delimiterFormat: winston.Logform.Format;
8
9
  export declare const escapeFormat: winston.Logform.Format;
9
10
  export declare const sortFormat: winston.Logform.Format;
package/lib/format.js CHANGED
@@ -29,6 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  var format_exports = {};
30
30
  __export(format_exports, {
31
31
  colorFormat: () => colorFormat,
32
+ consoleFormat: () => consoleFormat,
32
33
  delimiterFormat: () => delimiterFormat,
33
34
  escapeFormat: () => escapeFormat,
34
35
  getFormat: () => getFormat,
@@ -40,17 +41,16 @@ module.exports = __toCommonJS(format_exports);
40
41
  var import_chalk = __toESM(require("chalk"));
41
42
  var import_winston = __toESM(require("winston"));
42
43
  var import_config = require("./config");
44
+ var import_lodash = require("lodash");
43
45
  const DEFAULT_DELIMITER = "|";
44
- const colorize = {
45
- errors: import_chalk.default.red,
46
- module: import_chalk.default.cyan,
47
- reqId: import_chalk.default.gray,
48
- request: import_chalk.default.green
49
- };
46
+ const colorize = {};
50
47
  const getFormat = /* @__PURE__ */ __name((format) => {
51
48
  const configFormat = format || (0, import_config.getLoggerFormat)();
52
49
  let logFormat;
53
50
  switch (configFormat) {
51
+ case "console":
52
+ logFormat = import_winston.default.format.combine(consoleFormat);
53
+ break;
54
54
  case "logfmt":
55
55
  logFormat = logfmtFormat;
56
56
  break;
@@ -58,7 +58,7 @@ const getFormat = /* @__PURE__ */ __name((format) => {
58
58
  logFormat = import_winston.default.format.combine(escapeFormat, delimiterFormat);
59
59
  break;
60
60
  case "json":
61
- logFormat = import_winston.default.format.combine(stripColorFormat, import_winston.default.format.json({ deterministic: false }));
61
+ logFormat = import_winston.default.format.combine(import_winston.default.format.json({ deterministic: false }));
62
62
  break;
63
63
  default:
64
64
  return import_winston.default.format.combine(format);
@@ -67,17 +67,15 @@ const getFormat = /* @__PURE__ */ __name((format) => {
67
67
  }, "getFormat");
68
68
  const colorFormat = import_winston.default.format((info) => {
69
69
  Object.entries(info).forEach(([k, v]) => {
70
- if (k === "message" && info["level"].includes("error")) {
71
- info[k] = colorize.errors(v);
72
- }
73
- if (k === "reqId" && v) {
74
- info[k] = colorize.reqId(v);
75
- }
76
- if ((k === "module" || k === "submodule") && v) {
77
- info[k] = colorize.module(v);
70
+ var _a;
71
+ const level = info["level"];
72
+ if (colorize[k]) {
73
+ info[k] = colorize[k](v);
74
+ return;
78
75
  }
79
- if (v === "request" || v === "response") {
80
- info[k] = colorize.request(v);
76
+ if ((_a = colorize[level]) == null ? void 0 : _a[k]) {
77
+ info[k] = colorize[level][k](v);
78
+ return;
81
79
  }
82
80
  });
83
81
  return info;
@@ -107,6 +105,36 @@ const logfmtFormat = import_winston.default.format.printf(
107
105
  return `${k}=${v}`;
108
106
  }).join(" ")
109
107
  );
108
+ const consoleFormat = import_winston.default.format.printf((info) => {
109
+ const keys = ["level", "timestamp", "message"];
110
+ Object.entries(info).forEach(([k, v]) => {
111
+ if (typeof v === "object") {
112
+ if ((0, import_lodash.isEmpty)(v)) {
113
+ info[k] = "";
114
+ return;
115
+ }
116
+ try {
117
+ info[k] = JSON.stringify(v);
118
+ } catch (error) {
119
+ info[k] = String(v);
120
+ }
121
+ }
122
+ if (v === void 0 || v === null) {
123
+ info[k] = "";
124
+ }
125
+ });
126
+ const tags = Object.entries(info).filter(([k, v]) => !keys.includes(k) && v).map(([k, v]) => `${k}=${v}`).join(" ");
127
+ const level = info.level.padEnd(5, " ");
128
+ const message = info.message.padEnd(44, " ");
129
+ const color = {
130
+ error: import_chalk.default.red,
131
+ warn: import_chalk.default.yellow,
132
+ info: import_chalk.default.green,
133
+ debug: import_chalk.default.blue
134
+ }[info.level] || import_chalk.default.white;
135
+ const colorized = message.startsWith("Executing") ? color(`${info.timestamp} [${level}]`) + ` ${message}` : color(`${info.timestamp} [${level}] ${message}`);
136
+ return `${colorized} ${tags}`;
137
+ });
110
138
  const delimiterFormat = import_winston.default.format.printf(
111
139
  (info) => Object.entries(info).map(([, v]) => {
112
140
  if (typeof v === "object") {
@@ -127,10 +155,11 @@ const escapeFormat = import_winston.default.format((info) => {
127
155
  }
128
156
  return { ...info, message };
129
157
  })();
130
- const sortFormat = import_winston.default.format((info) => ({ level: info.level, timestamp: info.timestamp, ...info }))();
158
+ const sortFormat = import_winston.default.format((info) => ({ level: info.level, ...info }))();
131
159
  // Annotate the CommonJS export names for ESM import in node:
132
160
  0 && (module.exports = {
133
161
  colorFormat,
162
+ consoleFormat,
134
163
  delimiterFormat,
135
164
  escapeFormat,
136
165
  getFormat,
package/lib/index.d.ts CHANGED
@@ -2,5 +2,4 @@ export * from './config';
2
2
  export * from './logger';
3
3
  export * from './system-logger';
4
4
  export * from './request-logger';
5
- export * from './app-logger';
6
5
  export * from './transports';
package/lib/index.js CHANGED
@@ -18,7 +18,6 @@ __reExport(src_exports, require("./config"), module.exports);
18
18
  __reExport(src_exports, require("./logger"), module.exports);
19
19
  __reExport(src_exports, require("./system-logger"), module.exports);
20
20
  __reExport(src_exports, require("./request-logger"), module.exports);
21
- __reExport(src_exports, require("./app-logger"), module.exports);
22
21
  __reExport(src_exports, require("./transports"), module.exports);
23
22
  // Annotate the CommonJS export names for ESM import in node:
24
23
  0 && (module.exports = {
@@ -26,6 +25,5 @@ __reExport(src_exports, require("./transports"), module.exports);
26
25
  ...require("./logger"),
27
26
  ...require("./system-logger"),
28
27
  ...require("./request-logger"),
29
- ...require("./app-logger"),
30
28
  ...require("./transports")
31
29
  });
package/lib/logger.d.ts CHANGED
@@ -1,21 +1,11 @@
1
1
  import winston, { Logger } from 'winston';
2
- import { SystemLoggerOptions } from './system-logger';
3
2
  import 'winston-daily-rotate-file';
4
3
  interface LoggerOptions extends Omit<winston.LoggerOptions, 'transports' | 'format'> {
5
4
  dirname?: string;
6
5
  filename?: string;
7
- format?: 'logfmt' | 'json' | 'delimiter' | winston.Logform.Format;
6
+ format?: 'logfmt' | 'json' | 'delimiter' | 'console' | winston.Logform.Format;
8
7
  transports?: ('console' | 'file' | 'dailyRotateFile' | winston.transport)[];
9
8
  }
10
9
  export declare const createLogger: (options: LoggerOptions) => winston.Logger;
11
10
  export declare const createConsoleLogger: (options?: winston.LoggerOptions) => winston.Logger;
12
11
  export { Logger, LoggerOptions };
13
- interface ReqeustLoggerOptions extends LoggerOptions {
14
- skip?: (ctx?: any) => Promise<boolean>;
15
- requestWhitelist?: string[];
16
- responseWhitelist?: string[];
17
- }
18
- export interface AppLoggerOptions {
19
- request: ReqeustLoggerOptions;
20
- system: SystemLoggerOptions;
21
- }
package/lib/logger.js CHANGED
@@ -58,7 +58,7 @@ const createConsoleLogger = /* @__PURE__ */ __name((options) => {
58
58
  import_winston.default.format.timestamp({
59
59
  format: "YYYY-MM-DD HH:mm:ss"
60
60
  }),
61
- format || import_winston.default.format.combine(import_format.sortFormat, import_format.colorFormat, import_format.logfmtFormat)
61
+ format || import_format.consoleFormat
62
62
  ),
63
63
  ...rest || {},
64
64
  transports: [new import_winston.default.transports.Console()]
@@ -1,2 +1,7 @@
1
- import { AppLoggerOptions } from './logger';
2
- export declare const requestLogger: (appName: string, options?: AppLoggerOptions) => (ctx: any, next: any) => Promise<void>;
1
+ import { LoggerOptions } from './logger';
2
+ export interface RequestLoggerOptions extends LoggerOptions {
3
+ skip?: (ctx?: any) => Promise<boolean>;
4
+ requestWhitelist?: string[];
5
+ responseWhitelist?: string[];
6
+ }
7
+ export declare const requestLogger: (appName: string, options?: RequestLoggerOptions) => (ctx: any, next: any) => Promise<void>;
@@ -37,10 +37,10 @@ const requestLogger = /* @__PURE__ */ __name((appName, options) => {
37
37
  const requestLogger2 = (0, import_logger.createLogger)({
38
38
  dirname: (0, import_config.getLoggerFilePath)(appName),
39
39
  filename: "request",
40
- ...(options == null ? void 0 : options.request) || {}
40
+ ...options || {}
41
41
  });
42
42
  return async (ctx, next) => {
43
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
43
+ var _a, _b, _c, _d, _e, _f, _g, _h;
44
44
  const reqId = ctx.reqId;
45
45
  const path = /^\/api\/(.+):(.+)/.exec(ctx.path);
46
46
  const contextLogger = ctx.app.log.child({ reqId, module: path == null ? void 0 : path[1], submodule: path == null ? void 0 : path[2] });
@@ -51,11 +51,12 @@ const requestLogger = /* @__PURE__ */ __name((appName, options) => {
51
51
  path: ctx.url
52
52
  };
53
53
  requestLogger2.info({
54
- reqId,
55
- message: "request",
54
+ message: `request ${ctx.method} ${ctx.url}`,
56
55
  ...requestInfo,
57
- req: (0, import_lodash.pick)(ctx.request.toJSON(), ((_a = options == null ? void 0 : options.request) == null ? void 0 : _a.requestWhitelist) || defaultRequestWhitelist),
58
- action: (_c = (_b = ctx.action) == null ? void 0 : _b.toJSON) == null ? void 0 : _c.call(_b)
56
+ req: (0, import_lodash.pick)(ctx.request.toJSON(), (options == null ? void 0 : options.requestWhitelist) || defaultRequestWhitelist),
57
+ action: (_b = (_a = ctx.action) == null ? void 0 : _a.toJSON) == null ? void 0 : _b.call(_a),
58
+ app: appName,
59
+ reqId
59
60
  });
60
61
  let error;
61
62
  try {
@@ -66,19 +67,20 @@ const requestLogger = /* @__PURE__ */ __name((appName, options) => {
66
67
  const cost = Date.now() - startTime;
67
68
  const status = ctx.status;
68
69
  const info = {
69
- reqId,
70
- message: "response",
70
+ message: `response ${ctx.url}`,
71
71
  ...requestInfo,
72
- res: (0, import_lodash.pick)(ctx.response.toJSON(), ((_d = options == null ? void 0 : options.request) == null ? void 0 : _d.responseWhitelist) || defaultResponseWhitelist),
73
- action: (_f = (_e = ctx.action) == null ? void 0 : _e.toJSON) == null ? void 0 : _f.call(_e),
74
- userId: (_h = (_g = ctx.auth) == null ? void 0 : _g.user) == null ? void 0 : _h.id,
72
+ res: (0, import_lodash.pick)(ctx.response.toJSON(), (options == null ? void 0 : options.responseWhitelist) || defaultResponseWhitelist),
73
+ action: (_d = (_c = ctx.action) == null ? void 0 : _c.toJSON) == null ? void 0 : _d.call(_c),
74
+ userId: (_f = (_e = ctx.auth) == null ? void 0 : _e.user) == null ? void 0 : _f.id,
75
75
  status: ctx.status,
76
- cost
76
+ cost,
77
+ app: appName,
78
+ reqId
77
79
  };
78
80
  if (Math.floor(status / 100) == 5) {
79
- requestLogger2.error({ ...info, res: ((_i = ctx.body) == null ? void 0 : _i["errors"]) || ctx.body });
81
+ requestLogger2.error({ ...info, res: ((_g = ctx.body) == null ? void 0 : _g["errors"]) || ctx.body });
80
82
  } else if (Math.floor(status / 100) == 4) {
81
- requestLogger2.warn({ ...info, res: ((_j = ctx.body) == null ? void 0 : _j["errors"]) || ctx.body });
83
+ requestLogger2.warn({ ...info, res: ((_h = ctx.body) == null ? void 0 : _h["errors"]) || ctx.body });
82
84
  } else {
83
85
  requestLogger2.info(info);
84
86
  }
@@ -1,6 +1,18 @@
1
- import winston from 'winston';
1
+ import { Logger } from 'winston';
2
2
  import { LoggerOptions } from './logger';
3
3
  export interface SystemLoggerOptions extends LoggerOptions {
4
4
  seperateError?: boolean;
5
5
  }
6
- export declare const createSystemLogger: (options: SystemLoggerOptions) => winston.Logger;
6
+ export type logMethod = (message: string, meta?: {
7
+ module?: string;
8
+ submodule?: string;
9
+ method?: string;
10
+ [key: string]: any;
11
+ }) => SystemLogger;
12
+ export interface SystemLogger extends Omit<Logger, 'info' | 'warn' | 'error' | 'debug'> {
13
+ info: logMethod;
14
+ warn: logMethod;
15
+ error: logMethod;
16
+ debug: logMethod;
17
+ }
18
+ export declare const createSystemLogger: (options: SystemLoggerOptions) => SystemLogger;
@@ -58,17 +58,18 @@ const _SystemLoggerTransport = class _SystemLoggerTransport extends import_winst
58
58
  }
59
59
  }
60
60
  log(info, callback) {
61
- const { level, message, reqId, [import_triple_beam.SPLAT]: args } = info;
61
+ const { level, message, reqId, app, [import_triple_beam.SPLAT]: args } = info;
62
62
  const logger = level === "error" && this.errorLogger ? this.errorLogger : this.logger;
63
63
  const { module: module2, submodule, method, ...meta } = (args == null ? void 0 : args[0]) || {};
64
64
  logger.log({
65
65
  level,
66
- reqId,
67
66
  message,
67
+ meta,
68
68
  module: module2 || info["module"] || "",
69
69
  submodule: submodule || info["submodule"] || "",
70
70
  method: method || "",
71
- meta
71
+ app,
72
+ reqId
72
73
  });
73
74
  callback(null, true);
74
75
  }
package/lib/transports.js CHANGED
@@ -64,7 +64,7 @@ const getTransports = /* @__PURE__ */ __name((options) => {
64
64
  );
65
65
  const transports = {
66
66
  console: () => Transports.console({
67
- format: import_winston.default.format.combine(import_winston.default.format.colorize(), import_format.colorFormat, format)
67
+ format: import_winston.default.format.combine(format)
68
68
  }),
69
69
  file: () => Transports.file({
70
70
  dirname,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/logger",
3
- "version": "0.18.0-alpha.7",
3
+ "version": "0.18.0-alpha.9",
4
4
  "description": "nocobase logging library",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./lib/index.js",
@@ -18,5 +18,5 @@
18
18
  "winston-daily-rotate-file": "^4.7.1",
19
19
  "winston-transport": "^4.5.0"
20
20
  },
21
- "gitHead": "979a9c59a98c61a2287dd847580746a9b597cbde"
21
+ "gitHead": "34ca0df4eede2e83fc86297b0fe19eba970e2b1b"
22
22
  }
@@ -1,17 +0,0 @@
1
- import { Logger } from 'winston';
2
- import { SystemLoggerOptions } from './system-logger';
3
- export declare const createAppLogger: ({ app, ...options }: SystemLoggerOptions & {
4
- app?: string;
5
- }) => Logger;
6
- export type logMethod = (message: string, meta?: {
7
- module?: string;
8
- submodule?: string;
9
- method?: string;
10
- [key: string]: any;
11
- }) => AppLogger;
12
- export interface AppLogger extends Omit<Logger, 'info' | 'warn' | 'error' | 'debug'> {
13
- info: logMethod;
14
- warn: logMethod;
15
- error: logMethod;
16
- debug: logMethod;
17
- }
package/lib/app-logger.js DELETED
@@ -1,30 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var app_logger_exports = {};
20
- __export(app_logger_exports, {
21
- createAppLogger: () => createAppLogger
22
- });
23
- module.exports = __toCommonJS(app_logger_exports);
24
- var import_system_logger = require("./system-logger");
25
- var import_config = require("./config");
26
- const createAppLogger = /* @__PURE__ */ __name(({ app, ...options }) => (0, import_system_logger.createSystemLogger)({ dirname: (0, import_config.getLoggerFilePath)(app), filename: "system", seperateError: true, ...options }), "createAppLogger");
27
- // Annotate the CommonJS export names for ESM import in node:
28
- 0 && (module.exports = {
29
- createAppLogger
30
- });