Skip to content

feat(logging): add zap-based structured logging with CLI/output channels - #8

Merged
qicz merged 2 commits into
acmestack:mainfrom
qicz:feature/logging
Aug 14, 2026
Merged

qicz merged 2 commits into
acmestack:mainfrom
qicz:feature/logging

Conversation

@qicz

@qicz qicz commented Aug 14, 2026

Copy link
Copy Markdown
Member

Introduce internal/logging as the single logging facility, replacing ad-hoc fmt/os output with a structured pipeline and a separate user-facing CLI channel.

Logging package (internal/logging, 7 files split by responsibility):

  • logging.go: Logger wrapper over zap SugaredLogger; level methods take key/value pairs (no zap.String field construction); WithName tags a package-level logger resolved through Get so later Setup still applies
  • setup.go: default logger lifecycle (Get/Setup) with goroutine-safe swap
  • config.go: Config struct plus rotation defaults (100MB/5 backups/30d/compress)
  • encoder.go: zap core construction (text vs json) with AddCallerSkip(1)
  • rotate.go: lumberjack-based size rotation with gzip + backup retention
  • level.go: log-level string parsing with sane fallback
  • cli.go: CLIPrintf/CLIPrintln/CLIPrint user-facing stdout channel, never redirected by --log-file

Config precedence: CLI flag > env var (GPI_LOG_LEVEL/GPI_LOG_FILE/
GPI_LOG_FORMAT) > config logging: section > defaults (stdout/info/text).

Package-level named logger convention: every package declares
var logger = logging.WithName("module")
at the top of its same-name go file; all per-type Log fields and logging.Get() call sites are removed project-wide (provisioner, serve, jobs, server, metacache, backend docker/local/existing, cloud aws/aliyun, cli, gpilet). WithName resolves through Get on each call so the package logger follows Setup even when the var is initialized before configuration.

Key-path diagnostics added:

  • cloud aws/aliyun RunInstances decision logging (reuse running / resume stopped / create instances) at Info
  • backend docker/local/existing lifecycle (launch/teardown/stop/start)
  • optimizer candidate collection and placement choice at Debug
  • metacache fetch success/failure at Debug
  • /healthz requests skipped from request logging to avoid probe noise

CLI output migrated from fmt to logging.CLIPrintf/CLIPrintln across all cli subcommands; gpilet status uses CLIPrint. Diagnostic-only paths keep the zap channel (e.g. serve teardown warnings).

server middleware split from one middlewares.go into per-middleware files: middleware.go (interface + chain), cors.go, security.go, logging.go; dropped the unused unixMillis helper.

Docs: AGENTS.md Go conventions section (import grouping in 3 blocks via goimports -local, logging conventions, package file layout), README logging section, logging: example in examples/gpi-config.yaml, MEMORY.md v22.

What / 变更内容

Why / 为什么

How verified / 测试

  • make build passes (incl. go generate) / 通过(含 go generate)
  • go vet ./... passes / 通过
  • go test ./... passes / 通过
  • Cross-compile passes / 交叉编译通过(GOOS=linux/darwin GOARCH=amd64/arm64)
  • Handler-level tests added/updated for API changes / 涉及 HTTP API 时已补充/更新测试
  • Docs updated with version + changelog / 涉及文档时已更新(含版本号与变更记录)

Related / 关联

Notes / 其他

qicz added 2 commits August 15, 2026 02:18
Introduce internal/logging as the single logging facility, replacing ad-hoc
fmt/os output with a structured pipeline and a separate user-facing CLI
channel.

Logging package (internal/logging, 7 files split by responsibility):
- logging.go: Logger wrapper over zap SugaredLogger; level methods take
  key/value pairs (no zap.String field construction); WithName tags a
  package-level logger resolved through Get so later Setup still applies
- setup.go: default logger lifecycle (Get/Setup) with goroutine-safe swap
- config.go: Config struct plus rotation defaults (100MB/5 backups/30d/compress)
- encoder.go: zap core construction (text vs json) with AddCallerSkip(1)
- rotate.go: lumberjack-based size rotation with gzip + backup retention
- level.go: log-level string parsing with sane fallback
- cli.go: CLIPrintf/CLIPrintln/CLIPrint user-facing stdout channel, never
  redirected by --log-file

Config precedence: CLI flag > env var (GPI_LOG_LEVEL/GPI_LOG_FILE/
GPI_LOG_FORMAT) > config logging: section > defaults (stdout/info/text).

Package-level named logger convention: every package declares
  var logger = logging.WithName("module")
at the top of its same-name go file; all per-type Log fields and
logging.Get() call sites are removed project-wide (provisioner, serve,
jobs, server, metacache, backend docker/local/existing, cloud aws/aliyun,
cli, gpilet). WithName resolves through Get on each call so the package
logger follows Setup even when the var is initialized before configuration.

Key-path diagnostics added:
- cloud aws/aliyun RunInstances decision logging (reuse running / resume
  stopped / create instances) at Info
- backend docker/local/existing lifecycle (launch/teardown/stop/start)
- optimizer candidate collection and placement choice at Debug
- metacache fetch success/failure at Debug
- /healthz requests skipped from request logging to avoid probe noise

CLI output migrated from fmt to logging.CLIPrintf/CLIPrintln across all
cli subcommands; gpilet status uses CLIPrint. Diagnostic-only paths keep
the zap channel (e.g. serve teardown warnings).

server middleware split from one middlewares.go into per-middleware files:
middleware.go (interface + chain), cors.go, security.go, logging.go;
dropped the unused unixMillis helper.

Docs: AGENTS.md Go conventions section (import grouping in 3 blocks via
goimports -local, logging conventions, package file layout), README logging
section, logging: example in examples/gpi-config.yaml, MEMORY.md v22.
Merge branch 'main' of github.com:acmestack/gpi into feature/logging.

Upstream added the zh/en document bilingualization (docs/zh + docs/en,
root README.md/README.en.md) which bumped MEMORY.md to v22; this branch's
logging work also used v22. Resolve by keeping both records and bumping
the file to v23: v23 = logging system decisions, v22 = bilingualization.

The other upstream changes (docs/zh|en translations, .github/CLA.md and
RELEASE_NOTES.md sentence-level bilingualization, AGENTS.md bilingual
conventions) merge cleanly alongside the Go conventions section.
@qicz
qicz merged commit 700dd7a into acmestack:main Aug 14, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant