dhalia
dhalia

Reputation: 421

Java + spring - central way of persisting historical events to a database log

I have a small app using the following technology stack - java, spring, resteasy, hibernate, oracle db. This app is a type of translator - it takes incoming HTTP messages from a source, processes them, and forwards them to a destination.

I want to keep a persistent database log of all the messages that have passed through this app with columns such as message_type, date_received, date_processed, status, error_message (if there was an error), etc.

What is the best way to have a central place that "chronicles" these messages? I want this to happen on successful processing, as well as on failed messages without having to put code in too many places. I want something clean, maybe annotation-based even.. since I'm using spring.

Upvotes: 0

Views: 594

Answers (2)

Ashoka
Ashoka

Reputation: 941

This can be achieved by making use of the logger extensions.

For example if you are using log4j there are appenders that help you write to different resources. https://logging.apache.org/log4j/2.x/manual/appenders.html

In your case you need to add a DatabaseAppender that will log to your defined table in the database. The flexible configuration in Spring & AOP can be wired to your spring bean.

Upvotes: 0

techPackets
techPackets

Reputation: 4504

You can use AOP & apply it on the methods processing your input messages. Write a separate advice for sending the log messages to the database. This approach would be clean. You don't have to litter your logging code across the app, just write it in a class & apply on the methods at runtime.

Upvotes: 3

Related Questions