180-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
  • 改进日志系统
  • 应用日志方案
  • 重构日志代码
  • 添加日志支持

Reference

参考文档

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