181-java-observability-logging

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Java Logging Best Practices

Java日志最佳实践

Implement effective Java logging following standardized frameworks, meaningful log levels, core practices (parameterized logging, exception handling, no sensitive data), flexible configuration, security-conscious logging, monitoring and alerting, and comprehensive logging validation through testing.
What is covered in this Skill?
  • Standardized framework selection: SLF4J facade with Logback or Log4j2
  • Meaningful and consistent log levels: ERROR, WARN, INFO, DEBUG, TRACE
  • Core practices: parameterized logging, proper exception handling, avoiding sensitive data
  • Configuration: environment-specific (logback.xml, log4j2.xml), output formats, log rotation
  • Security: mask sensitive data, control log access, secure transmission, GDPR/HIPAA compliance
  • Log monitoring and alerting: centralized aggregation (ELK, Splunk, Loki), automated alerts
  • Logging validation through testing: assert log messages, verify formats, test levels, measure performance impact
Scope: The reference is organized by examples (good/bad code patterns) for each core area. Apply recommendations based on applicable examples.
遵循标准化框架、有意义的日志级别、核心实践(参数化日志、异常处理、不包含敏感数据)、灵活配置、安全日志、监控与告警,以及通过测试进行全面日志验证,实现高效的Java日志。
本技能涵盖哪些内容?
  • 标准化框架选择:搭配Logback或Log4j2的SLF4J门面
  • 有意义且一致的日志级别:ERROR、WARN、INFO、DEBUG、TRACE
  • 核心实践:参数化日志、恰当的异常处理、避免敏感数据
  • 配置:环境特定配置(logback.xml、log4j2.xml)、输出格式、日志轮转
  • 安全:屏蔽敏感数据、控制日志访问、安全传输、GDPR/HIPAA合规
  • 日志监控与告警:集中式聚合(ELK、Splunk、Loki)、自动化告警
  • 通过测试验证日志:断言日志消息、验证格式、测试级别、衡量性能影响
范围:参考资料按每个核心领域的示例(良好/不良代码模式)组织。根据适用示例应用建议。

Constraints

约束条件

Before applying any logging recommendations, ensure the project compiles. Compilation failure is a blocking condition. After applying improvements, run full verification.
  • MANDATORY: Run
    ./mvnw compile
    or
    mvn compile
    before applying any change
  • SAFETY: If compilation fails, stop immediately — do not proceed until resolved
  • VERIFY: Run
    ./mvnw clean verify
    or
    mvn clean verify
    after applying improvements
  • BEFORE APPLYING: Read the reference for detailed good/bad examples, constraints, and safeguards for each logging pattern
在应用任何日志建议前,确保项目可编译。编译失败是阻塞条件。应用改进后,运行完整验证。
  • 强制要求:在应用任何更改前运行
    ./mvnw compile
    mvn compile
  • 安全要求:如果编译失败,立即停止——问题解决前不要继续
  • 验证要求:应用改进后运行
    ./mvnw clean verify
    mvn clean verify
  • 应用前须知:阅读参考资料,了解每个日志模式的详细良好/不良示例、约束条件和防护措施

When to use this skill

何时使用本技能

  • Improve logging
  • Apply logging
  • Refactor logging
  • Add logging support
  • 改进日志
  • 应用日志
  • 重构日志
  • 添加日志支持

Workflow

工作流程

  1. Compile project before logging changes
Run
./mvnw compile
or
mvn compile
and stop immediately if compilation fails.
  1. Read logging reference and assess current observability
Read
references/181-java-observability-logging.md
and evaluate framework usage, log levels, sensitive-data handling, and config gaps.
  1. Apply logging and observability improvements
Implement selected framework/configuration/practice changes, including secure logging and monitoring integration where applicable.
  1. Verify with full build
Run
./mvnw clean verify
or
mvn clean verify
after applying improvements.
  1. 日志更改前编译项目
运行
./mvnw compile
mvn compile
,如果编译失败立即停止。
  1. 阅读日志参考资料并评估当前可观测性
阅读
references/181-java-observability-logging.md
,评估框架使用情况、日志级别、敏感数据处理和配置缺口。
  1. 应用日志与可观测性改进
实施选定的框架/配置/实践更改,包括适用的安全日志和监控集成。
  1. 通过完整构建验证
应用改进后运行
./mvnw clean verify
mvn clean verify

Reference

参考资料

For detailed guidance, examples, and constraints, see references/181-java-observability-logging.md.
如需详细指导、示例和约束条件,请查看references/181-java-observability-logging.md