@mango-power/node-kit 0.0.6 → 0.1.0

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/README.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  Shared node toolkit package for MP services.
4
4
 
5
+ ## Logger 约定(createLogger)
6
+
7
+ - **参数顺序**:先消息(string),后 meta(object)。例如 `logger.info("aggregate for site", { siteId, deviceCount })`。
8
+ - **输出**:`[ISO时间] [service] 消息 <meta 单行 JSON>`。meta 默认紧凑模式(`JSON.stringify`),设置环境变量 `LOG_COMPACT=false` 可改为多行对象输出。
9
+
5
10
  ## Included modules
6
11
 
7
12
  - `createLogger`
@@ -36,6 +41,6 @@ registry=https://registry.npmjs.org/
36
41
  ## Install from other projects
37
42
 
38
43
  ```bash
39
- npm i @mango-power/node-kit@^0.0.6
44
+ npm i @mango-power/node-kit@^0.1.0
40
45
  ```
41
46
 
package/dist/logger.js CHANGED
@@ -35,16 +35,31 @@ function normalizeErrorRecord(record) {
35
35
  function prefix(service) {
36
36
  return `[${new Date().toISOString()}] [${service}]`;
37
37
  }
38
+ /** 默认紧凑模式:meta 对象以 JSON.stringify 单行输出,可通过 LOG_COMPACT=false 关闭 */
39
+ function resolveCompact() {
40
+ const v = process.env.LOG_COMPACT;
41
+ if (v === "0" || v === "false")
42
+ return false;
43
+ return true;
44
+ }
45
+ const logCompact = resolveCompact();
46
+ function serializeMeta(obj) {
47
+ const normalized = normalizeErrorRecord(obj);
48
+ return logCompact ? JSON.stringify(normalized) : normalized;
49
+ }
38
50
  function normalizeArgs(service, args) {
39
51
  if (args.length === 0) {
40
52
  return [prefix(service)];
41
53
  }
42
54
  const [first, ...rest] = args;
43
55
  if (typeof first === "string") {
44
- return [`${prefix(service)} ${first}`, ...rest];
56
+ const head = `${prefix(service)} ${first}`;
57
+ if (rest.length === 0)
58
+ return [head];
59
+ return [head, ...rest.map((x) => (x && typeof x === "object" && !Array.isArray(x) ? serializeMeta(x) : x))];
45
60
  }
46
61
  if (first && typeof first === "object" && !Array.isArray(first)) {
47
- return [prefix(service), normalizeErrorRecord(first), ...rest];
62
+ return [prefix(service), serializeMeta(first), ...rest];
48
63
  }
49
64
  return [prefix(service), first, ...rest];
50
65
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mango-power/node-kit",
3
- "version": "0.0.6",
3
+ "version": "0.1.0",
4
4
  "description": "Shared node toolkit for mp services",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",