- Notifications
You must be signed in to change notification settings - Fork50
Overview
Thecf-java-logging-support library helps you instrument your Java application to produce powerful, structured logs.It is designed for cloud environments where centralized log analysis is essential.The library is purpose-built for applications on SAP BTP, integrating seamlessly with the SAP Cloud Logging service and SAP Application Logging service for SAP BTP.
The library serves two main purposes:
- Emit structured application logs: It formats your application's log messages as JSON, enriching them with important context.
- Generate automated request logs: It captures detailed performance metrics for incoming HTTP requests, also in a structured JSON format.
By providing a standard, opinionated log format, the library makes it easy to parse, search, and visualize your logs in an analysis tool.It also automatically handles one of the biggest challenges in microservices:request tracing through acorrelation_id.
While the format is standardized, you can easily add your own data.See the guide onAdding Custom Fields for details.
The library generates two distinct but correlated types of log messages. For readability, the examples below are formatted, but the library emits each JSON object as a single line.
These are the logs your application code generates using SLF4J.The library's encoder wraps these messages in a JSON structure, adding valuable context.
For example, a simple log statement in your code...
logger.info("User checkout process initiated.");
...is transformed into a rich JSON log message like this:
{"msg":"User checkout process initiated.","level":"INFO","written_at":"2024-10-26T10:30:05.123Z","written_ts":"1730005805123000000","logger":"com.company.service.CheckoutService","thread":"http-nio-8080-exec-5","type":"log","correlation_id":"c1b2a3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"}You'll recognize standard fields likemsg andlevel, but you'll also see powerful additions like thecorrelation_id that links this message to a specific incoming request.
For every incoming HTTP request, the library's instrumentation can automatically generate a single log line summarizing the entire transaction.
{"type":"request","level":"INFO","direction":"IN","method":"POST","request":"/api/v1/checkout","response_status":202,"response_time_ms":152.75,"response_size_b":256,"remote_ip":"redacted","remote_host":"redacted","correlation_id":"c1b2a3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d","written_at":"2024-10-26T10:30:05.280Z","written_ts":"1730005805280000000"}This log captures key metrics likeresponse_time_ms andresponse_status.It shares the samecorrelation_id as the application logs, allowing you to easily find all logs related to this specific request.
Secure by Default For security and data privacy, sensitive connection details like remote_ip and remote_host are redacted by default. Configuration to enable these fields is available but must be done explicitly.
You may notice that both log messages contain acorrelation_id field with the same value.This is the library's most powerful feature for debugging distributed systems.
When an incoming request is processed, the library's instrumentation injects a unique correlation ID into the logging context (MDC).All application logs generated during the processing of that request will automatically include this ID.This makes it simple to filter and find every log line—across multiple microservices—that was part of a single business transaction.
Now that you understand the concepts, you can start configuring your application:
- To set up JSON-formatted application logs, see:Writing Application Logs
- To enable automated request logs and correlation IDs, see:Request Instrumentation