@wdyy/skills 0.1.5 → 0.1.6

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.
@@ -4,14 +4,15 @@ services:
4
4
  env_file: ../../.env.production
5
5
  environment:
6
6
  INSTANCE_ID: blue
7
- LOG_DIR: /var/log/application
7
+ HOST: ${BACKEND_LISTEN_HOST}
8
+ PORT: ${BACKEND_CONTAINER_PORT}
8
9
  ports:
9
- - "127.0.0.1:${BLUE_PORT:-3001}:3000"
10
+ - "${BACKEND_BIND_IP}:${BLUE_PORT}:${BACKEND_CONTAINER_PORT}"
10
11
  volumes:
11
- - ${HOST_LOG_DIR:?set HOST_LOG_DIR}/backend/blue:/var/log/application
12
+ - ../../logs:/app/logs
12
13
  restart: unless-stopped
13
14
  healthcheck:
14
- test: ["CMD", "wget", "-qO-", "http://localhost:3000/health"]
15
+ test: ["CMD", "wget", "-qO-", "${BACKEND_CONTAINER_HEALTH_URL}"]
15
16
  interval: 10s
16
17
  timeout: 3s
17
18
  retries: 6
@@ -21,14 +22,15 @@ services:
21
22
  env_file: ../../.env.production
22
23
  environment:
23
24
  INSTANCE_ID: green
24
- LOG_DIR: /var/log/application
25
+ HOST: ${BACKEND_LISTEN_HOST}
26
+ PORT: ${BACKEND_CONTAINER_PORT}
25
27
  ports:
26
- - "127.0.0.1:${GREEN_PORT:-3002}:3000"
28
+ - "${BACKEND_BIND_IP}:${GREEN_PORT}:${BACKEND_CONTAINER_PORT}"
27
29
  volumes:
28
- - ${HOST_LOG_DIR:?set HOST_LOG_DIR}/backend/green:/var/log/application
30
+ - ../../logs:/app/logs
29
31
  restart: unless-stopped
30
32
  healthcheck:
31
- test: ["CMD", "wget", "-qO-", "http://localhost:3000/health"]
33
+ test: ["CMD", "wget", "-qO-", "${BACKEND_CONTAINER_HEALTH_URL}"]
32
34
  interval: 10s
33
35
  timeout: 3s
34
36
  retries: 6
@@ -0,0 +1,15 @@
1
+ .env
2
+ .env.*
3
+ !.env.example
4
+ *.pem
5
+ *.key
6
+ id_rsa
7
+ id_ed25519
8
+ credentials
9
+ credentials.*
10
+ deploy/
11
+ logs/
12
+ .git/
13
+ node_modules/
14
+ venv/
15
+ .pnpm-store/
@@ -1,22 +1,23 @@
1
- include /srv/<project>/deploy/nginx/backend-active.conf;
1
+ include "__DEPLOY_ROOT__/nginx/backend-active.conf";
2
2
 
3
3
  server {
4
4
  location /api/ {
5
- proxy_pass http://backend_active;
5
+ proxy_pass __BACKEND_PROXY_URL__;
6
6
  proxy_set_header X-Trace-Id $request_id;
7
7
  }
8
8
 
9
9
  location /releases/ {
10
- root /srv/<project>;
10
+ root "__DEPLOY_ROOT__";
11
11
  try_files $uri =404;
12
12
  }
13
13
 
14
14
  location / {
15
- root /srv/<project>/current;
15
+ root "__DEPLOY_ROOT__/current";
16
16
  try_files $uri $uri/ /index.html;
17
17
  }
18
18
 
19
19
  location = /index.html {
20
+ root "__DEPLOY_ROOT__/current";
20
21
  add_header Cache-Control "no-cache, no-store, must-revalidate" always;
21
22
  }
22
23
 
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: wdyy-logging-standard
3
- description: 为 NestJS 和 Vue 项目实现前端异常上报、后端统一结构化 JSON 日志、traceId 传递、完整原始入参与 2MB 文件轮转。Use when 编写日志、错误处理、接口日志或审查生产可观测性时。
3
+ description: 为 NestJS 和 Vue 项目实现仅写入项目根目录 ./logs 的结构化 JSON 日志、前端异常上报、traceId 传递、完整原始入参与 2MB 文件轮转。Use when 编写日志、错误处理、接口日志或审查生产可观测性时。
4
4
  ---
5
5
 
6
6
  # 企业日志规范
@@ -19,7 +19,7 @@ description: 为 NestJS 和 Vue 项目实现前端异常上报、后端统一结
19
19
 
20
20
  ## 输入与输出
21
21
 
22
- - 输入:服务名、实例标识、环境、请求上下文、`LOG_DIR`。
22
+ - 输入:服务名、实例标识、环境和请求上下文;应用从项目根目录启动。
23
23
  - 输出:统一 logger、前端异常上报客户端、HTTP 日志中间件、日志文件命名规则和日志验证结果。
24
24
  - 使用 [日志规则](reference/logging-rules.md) 和 [logger 模板](templates/logger.template.ts)。
25
25
 
@@ -31,27 +31,28 @@ description: 为 NestJS 和 Vue 项目实现前端异常上报、后端统一结
31
31
  4. HTTP 请求日志记录完整的原始 `query`(查询参数)和 `body`(请求体);授权请求可记录实际用户、模式、表和权限等入参。服务端返回成功时记录 `result: success`;返回失败时记录 `result: failure`,并在 `response` 中记录完整失败内容。HTTP `statusCode` 为 100–399 时为成功,400–599 时为失败。
32
32
  5. 日志不执行脱敏、数组截断或大对象裁剪;`body` 必须保留完整原始入参。
33
33
  6. Vue 捕获未处理异常和 Promise 拒绝并上报后端;浏览器不得尝试写服务器文件。
34
- 7. 后端直接按 `LOG_DIR/yyyy-mm-dd_hh24-mm-ss.log` 写入,禁止创建子目录。若同一秒需要创建多个文件,依次使用 `yyyy-mm-dd_hh24-mm-ss_1.log`、`yyyy-mm-dd_hh24-mm-ss_2.log` 等递增后缀;既有日志文件不得重命名。最新文件超过 2MB 时创建新文件,并测试轮转。
34
+ 7. 后端只在项目根目录创建 `./logs`,并直接按 `./logs/yyyy-mm-dd_hh24-mm-ss.log` 写入;不得读取 `LOG_DIR`、`HOST_LOG_DIR` 或其他目录配置,不得在 `logs` 中创建任何子目录。使用排他创建避免并发覆盖;同一秒发生冲突时依次使用 `_1`、`_2` 等递增后缀。最新文件超过 2MB 时仍在 `./logs` 创建新文件,既有文件不得移动、重命名或覆盖。
35
35
 
36
36
  ## 禁止事项
37
37
 
38
38
  - 不得将 `message` 仅限制为 `success` 或 `error`。
39
39
  - 不得用 console 输出替代业务日志。
40
+ - 不得将日志写入项目根目录 `./logs` 之外,或按服务、实例、日期、blue/green 颜色创建日志子目录。
40
41
 
41
42
  ## Red Flags
42
43
 
43
44
  - 业务代码直接调用 console,或 `body` 未保留完整原始入参。
44
45
  - 请求日志缺失 traceId、状态码或耗时。
45
- - 同秒创建的日志文件覆盖既有文件,或前端异常未进入统一后端日志。
46
+ - 日志路径可被环境变量改变、生成了日志子目录、同秒文件覆盖既有文件,或前端异常未进入统一后端日志。
46
47
 
47
48
  ## Verification
48
49
 
49
50
  - [ ] 正常、异常和 HTTP 请求日志均为可解析 JSON。
50
51
  - [ ] traceId 跨入口与内部调用可追踪。
51
- - [ ] 完整 HTTP 入参、服务端成功/失败结果映射、失败响应内容与同秒递增命名均有测试覆盖,轮转阈值可验证。
52
+ - [ ] 完整 HTTP 入参、服务端成功/失败结果映射、失败响应内容、固定 `./logs` 路径、同秒递增命名与轮转阈值均有测试覆盖。
52
53
  - [ ] 前端异常上报经过后端身份校验。
53
- - [ ] 日志直接写入持久化挂载的 `LOG_DIR`,容器替换后日志仍保留。
54
- - [ ] `LOG_DIR` 缺失或不可写时服务以明确错误停止。
54
+ - [ ] 日志直接写入项目根目录 `./logs`,且容器替换后日志仍保留。
55
+ - [ ] 项目根目录或 `./logs` 不可写时服务以明确错误停止。
55
56
 
56
57
  ## Common Rationalizations
57
58
 
@@ -1,4 +1,4 @@
1
1
  interface:
2
2
  display_name: "Enterprise Logging Standard"
3
- short_description: "Implement full-context structured logs and frontend error reporting"
4
- default_prompt: "Use $wdyy-logging-standard to implement full-context structured logs, result recording, and frontend error reporting."
3
+ short_description: "Write full-context logs only to project-root ./logs"
4
+ default_prompt: "Use $wdyy-logging-standard to implement full-context structured logs that write only to project-root ./logs."
@@ -3,10 +3,11 @@
3
3
  - 日志为一行 JSON,最小字段见 `logger.template.ts`。
4
4
  - `logger.template.ts` 必须可在未安装 `@types/node` 的项目中独立类型检查;读取环境变量时通过 `globalThis` 声明最小运行时形状,不直接依赖 Node.js 全局类型。
5
5
  - `timestamp` 使用服务器本地时间 `YYYY-MM-DD HH:mm:ss`,精确到秒;不得包含毫秒、`T` 或 `Z`。
6
- - 文件直接写入 `LOG_DIR`,不得创建服务、实例或日期子目录。文件名使用服务器本地时间 `yyyy-mm-dd_hh24-mm-ss.log`;同一秒内创建多个文件时,后续文件依次为 `yyyy-mm-dd_hh24-mm-ss_1.log`、`yyyy-mm-dd_hh24-mm-ss_2.log`。最新日志文件大于 2MB 时先创建新文件再写入,既有日志文件不得重命名或覆盖。
6
+ - 应用必须从项目根目录启动,只允许创建项目根目录 `./logs`,并将文件直接写入该目录;不得读取 `LOG_DIR`、`HOST_LOG_DIR` 或其他目录配置,不得创建服务、实例、日期、blue/green 颜色或其他日志子目录。
7
+ - 文件名使用服务器本地时间 `yyyy-mm-dd_hh24-mm-ss.log`。使用排他创建;同一秒内文件名冲突时依次尝试 `yyyy-mm-dd_hh24-mm-ss_1.log`、`yyyy-mm-dd_hh24-mm-ss_2.log`,不得先检查后覆盖写入。最新日志文件大于 2MB 时在同一 `./logs` 中创建新文件,既有日志文件不得移动、重命名或覆盖。
7
8
  - 日志仅记录关键业务事件、请求和异常;`params`、`query`、`body` 和 `response` 均保留完整原始值,不执行脱敏、数组截断或大对象裁剪。授权请求可记录实际用户、模式、表和权限等入参。
8
9
  - HTTP 日志必须保留 `statusCode`。服务端成功返回时 `result` 为 `success`;失败返回时 `result` 为 `failure`,并在 `response` 中保留完整失败内容。100–399 为成功,400–599 为失败。
9
10
  - HTTP 入口和内部服务调用必须透传 traceId。
10
- - 每条日志必须包含 `instanceId`;通过同秒递增后缀避免多个实例或进程覆盖同一日志文件。
11
+ - 每条日志必须包含 `instanceId`;blue、green 与其他进程共享同一 `./logs`,通过排他创建和同秒递增后缀避免覆盖。
11
12
  - Vue 未处理异常和 Promise 拒绝必须上报后端;后端校验请求身份后写入统一日志。
12
- - 日志目录必须挂载到容器外部持久路径,替换容器不得删除历史日志。
13
+ - 容器必须将宿主机项目根目录 `./logs` 挂载到应用根目录 `/app/logs`,替换容器不得删除历史日志。
@@ -4,7 +4,12 @@ import { tmpdir } from 'node:os';
4
4
  import { join } from 'node:path';
5
5
  import { afterEach, test } from 'node:test';
6
6
  import { spawnSync } from 'node:child_process';
7
- import { createLogFileName } from '../templates/logger.template.ts';
7
+ import {
8
+ createLogFileName,
9
+ createLogFilePath,
10
+ LOG_DIRECTORY,
11
+ MAX_LOG_FILE_SIZE_BYTES,
12
+ } from '../templates/logger.template.ts';
8
13
 
9
14
  const validator = new URL('./validate-log-entry.mjs', import.meta.url);
10
15
  const temporaryDirectories = [];
@@ -34,6 +39,50 @@ test('同一秒日志文件使用递增后缀', () => {
34
39
  );
35
40
  });
36
41
 
42
+ test('日志路径固定在项目根目录的扁平 logs 目录', () => {
43
+ const date = new Date(2026, 6, 28, 9, 5, 3);
44
+
45
+ assert.equal(LOG_DIRECTORY, './logs');
46
+ assert.deepEqual(
47
+ [createLogFilePath(date), createLogFilePath(date, 1), createLogFilePath(date, 2)],
48
+ [
49
+ './logs/2026-07-28_09-05-03.log',
50
+ './logs/2026-07-28_09-05-03_1.log',
51
+ './logs/2026-07-28_09-05-03_2.log',
52
+ ],
53
+ );
54
+ assert.ok(
55
+ [createLogFilePath(date), createLogFilePath(date, 1), createLogFilePath(date, 2)]
56
+ .every((path) => path.split('/').length === 3),
57
+ );
58
+ });
59
+
60
+ test('旧日志目录环境变量不能改变固定路径', () => {
61
+ const previousLogDir = process.env.LOG_DIR;
62
+ const previousHostLogDir = process.env.HOST_LOG_DIR;
63
+ process.env.LOG_DIR = '/tmp/legacy-logs';
64
+ process.env.HOST_LOG_DIR = '/srv/legacy-logs';
65
+
66
+ try {
67
+ assert.equal(
68
+ createLogFilePath(new Date(2026, 6, 28, 9, 5, 3)),
69
+ './logs/2026-07-28_09-05-03.log',
70
+ );
71
+ } finally {
72
+ if (previousLogDir === undefined) delete process.env.LOG_DIR;
73
+ else process.env.LOG_DIR = previousLogDir;
74
+ if (previousHostLogDir === undefined) delete process.env.HOST_LOG_DIR;
75
+ else process.env.HOST_LOG_DIR = previousHostLogDir;
76
+ }
77
+ });
78
+
79
+ test('2MB 轮转阈值使用同一 logs 目录的下一个候选路径', () => {
80
+ const date = new Date(2026, 6, 28, 9, 5, 3);
81
+
82
+ assert.equal(MAX_LOG_FILE_SIZE_BYTES, 2 * 1024 * 1024);
83
+ assert.equal(createLogFilePath(date, 3), './logs/2026-07-28_09-05-03_3.log');
84
+ });
85
+
37
86
  test('蓝绿实例日志必须包含 instanceId', async () => {
38
87
  const result = await validate({
39
88
  timestamp: '2026-07-09 08:00:00',
@@ -24,6 +24,9 @@ export type LogEntry = LogContext & {
24
24
 
25
25
  const pad = (value: number) => String(value).padStart(2, '0');
26
26
 
27
+ export const LOG_DIRECTORY = './logs' as const;
28
+ export const MAX_LOG_FILE_SIZE_BYTES = 2 * 1024 * 1024;
29
+
27
30
  type RuntimeEnvironment = Record<string, string | undefined>;
28
31
 
29
32
  const getRuntimeEnvironment = (): RuntimeEnvironment => {
@@ -44,6 +47,10 @@ export const createLogFileName = (date = new Date(), collisionIndex = 0): string
44
47
  return `${baseName}${collisionIndex > 0 ? `_${collisionIndex}` : ''}.log`;
45
48
  };
46
49
 
50
+ export const createLogFilePath = (date = new Date(), collisionIndex = 0): string => (
51
+ `${LOG_DIRECTORY}/${createLogFileName(date, collisionIndex)}`
52
+ );
53
+
47
54
  const resultForStatusCode = (statusCode: number): LogEntry['result'] => {
48
55
  if (statusCode >= 100 && statusCode <= 399) return 'success';
49
56
  if (statusCode >= 400 && statusCode <= 599) return 'failure';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wdyy/skills",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "企业内部开发 Skill 与项目初始化命令",
5
5
  "type": "module",
6
6
  "bin": {