181-java-observability-logging
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseJava 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 or
./mvnw compilebefore applying any changemvn compile - SAFETY: If compilation fails, stop immediately — do not proceed until resolved
- VERIFY: Run or
./mvnw clean verifyafter applying improvementsmvn clean verify - BEFORE APPLYING: Read the reference for detailed good/bad examples, constraints, and safeguards for each logging pattern
在应用任何日志建议前,确保项目可编译。编译失败是阻塞条件。应用改进后,运行完整验证。
- 强制要求:在应用任何更改前运行或
./mvnw compilemvn compile - 安全要求:如果编译失败,立即停止——问题解决前不要继续
- 验证要求:应用改进后运行或
./mvnw clean verifymvn clean verify - 应用前须知:阅读参考资料,了解每个日志模式的详细良好/不良示例、约束条件和防护措施
When to use this skill
何时使用本技能
- Improve logging
- Apply logging
- Refactor logging
- Add logging support
- 改进日志
- 应用日志
- 重构日志
- 添加日志支持
Workflow
工作流程
- Compile project before logging changes
Run or and stop immediately if compilation fails.
./mvnw compilemvn compile- Read logging reference and assess current observability
Read and evaluate framework usage, log levels, sensitive-data handling, and config gaps.
references/181-java-observability-logging.md- Apply logging and observability improvements
Implement selected framework/configuration/practice changes, including secure logging and monitoring integration where applicable.
- Verify with full build
Run or after applying improvements.
./mvnw clean verifymvn clean verify- 日志更改前编译项目
运行或,如果编译失败立即停止。
./mvnw compilemvn compile- 阅读日志参考资料并评估当前可观测性
阅读,评估框架使用情况、日志级别、敏感数据处理和配置缺口。
references/181-java-observability-logging.md- 应用日志与可观测性改进
实施选定的框架/配置/实践更改,包括适用的安全日志和监控集成。
- 通过完整构建验证
应用改进后运行或
./mvnw clean verifymvn clean verifyReference
参考资料
For detailed guidance, examples, and constraints, see references/181-java-observability-logging.md.
如需详细指导、示例和约束条件,请查看references/181-java-observability-logging.md。