core2hack
core2hack

Reputation: 1

log4j parameterized logging

I want to develop a simple logger which sends logs to a server where all the logs are stored and displayed in graphs, etc.

The logger has to be parameterized since there is a structure given by the developed database for analyzing and displaying the stored logs.

The parameters for the logs are implemented with overloaded methods, for example:

mylog.error(String jobname, String whatever);
mylog.error(String jobname, String whatever, List<String> whatever);

This is how the logs are implemented by a developer when needed.

I wonder if I could use log4j for something like this?

Anyway, my own simple logging works fine, sending the logs in json to my server, where they are stored, etc.

Upvotes: 0

Views: 2619

Answers (2)

Dave Newton
Dave Newton

Reputation: 160191

There are a number of ways to do this.

I tend to prefer a Mapped Diagnostic Context (MDC) (and comparison with NDC) although I wrote my own formatter extension for displaying it as I needed ordered rendering.

Other frameworks have similar capabilities, but a mapped approach allows for some nice headings, which is handy for log parsing tools, or for DB-oriented logging.

Upvotes: 1

Mike Yockey
Mike Yockey

Reputation: 4593

This is how SLF4J works. Decouple logging from your application with SLF4J and you can use the Log4J connector to use it as the underlying implementation. That being said, the creator of SLF4J wrote Logback to be an ideal implementation of the SLF4J interface. If you aren't tied organizationally to Log4J, I suggest you use it.

Upvotes: 1

Related Questions