user6023611
user6023611

Reputation:

Spring-boot actuator - trace to file

In spring boot we have trace endpoint. Is it possible to redirect these entries into file ? I was trying to find this in internet, but no effects.

Upvotes: 2

Views: 1836

Answers (2)

Encryption
Encryption

Reputation: 1897

I'm going to add a more complete solution similar to what I ended up doing, but no matter what you end up needing to create a CustomTraceRepository class that implements HttpTraceRepository because Spring Framework handles calling the add method from somewhere in the FilterChains, when its enabled.

@Slf4j
@Component
@RequiredArgsConstructor
public class CustomHttpTraceRepository implements HttpTraceRepository {

private final ObjectMapper objectMapper;

AtomicReference<HttpTrace> lastTrace = new AtomicReference<>();

@Override
public void add(HttpTrace trace) {
    // Leverage the parameter Trace object
    lastTrace.set(trace);
    try {
        log.info(objectMapper.writeValueAsString(lastTrace.get()));
    } catch (JsonProcessingException e) {
        // do nothing
    }
}

@Override
public List<HttpTrace> findAll() {
    synchronized (this.contents) {
        return new ArrayList<>(); // Do not use / leave return empty list
    }
}

}

Upvotes: 0

Ario
Ario

Reputation: 1940

First you can Implement your own class from TraceRepository. Then save it to a file with Logger. You can find a production ready sample here which is a way to save actuator to file per any request.

Upvotes: 1

Related Questions