Cratylus
Cratylus

Reputation: 54084

Best way to interact with application logs output from log4j (while also are being updated by application)

May be it is simpler than I think but I am confused on the following:

I want to be able to present to a user (in a graphical interface) the logs produced by Log4j.

I could just read the files as it is and present it, but I was wondering if there is a standard way to do it to so as to also get any updates that happen at the same time from the other parts of the application that log concurrently.

The log4j files could be multiple i.e. rolling appender

Also the presentation could be while there is no logging happening.
I.e. view of logs up to date

UPDATE:
I am constraint to Java 6

Upvotes: 1

Views: 323

Answers (7)

Nadav
Nadav

Reputation: 1

Try XpoLog log4j/log4net connector. It parses the data automaticly and has predefined set of dashboards for it:

Follow the below steps

  1. Download and install XpoLog from here

  2. Add the log4j data using the log4j data connector from here and

  3. deploy the log4j app here

Upvotes: 0

Scott
Scott

Reputation: 1736

If you write your own simple appender and have your application include that appender in your log4j configuration, your appender will be called whenever events are written to other appenders, and you can choose to display the event messages, timestamps, etc. in a UI.

Upvotes: 0

Svilen
Svilen

Reputation: 1447

Maybe you could think about more "OS-level" solution. I don't know if you are using win or linux, but on linux there is this realy nice command "tail". So you could use ProcessBuilder to create OS process which goes something like "tail -f yourLogFile.txt". And then read the OutputStream of the returned Process. Reading the stream will block waiting for new output from the process to be available, and will immediately unblock when such is available, giving you immediate feedback and possibility to read the latest changes of the log file.

However, you might have problems shutting this process down from Java. You should be able to send SIGTERM signal to it if you know the process id. Or you could start a different process which could lookup the id of the "tail" process and kill it via "kill" command or something similar.

Also I am not sure if there is similar tool available on windows, if this is your platform.

Upvotes: 0

anubhava
anubhava

Reputation: 785376

Fro the official documentation of log4j:

Is there a way to get log4j to automatically reload a configuration file if it changes?

Yes. Both the DOMConfigurator and the PropertyConfigurator support automatic reloading
through the configureAndWatch method. See the API documentation for more details.

For the on-demand reload of log4j config using GUI I would suggest expose it via a servlet in your J2EE application so that whole file can be edited in a web page (text area may be) and once saved you can overwrite your existing log4j file and reload the log4j config.

Upvotes: 0

Paul McKenzie
Paul McKenzie

Reputation: 20094

Perhaps add a database appender (JDBCAppender) and present the log entries from that?

Upvotes: 0

Peter Szanto
Peter Szanto

Reputation: 7722

Have you tried the following tools :

Chainsaw

Xpolog

Upvotes: 0

Shivan Dragon
Shivan Dragon

Reputation: 15219

You can use Java 7's NIO2 libraries to get notified when one of multiple files get's modified in a directory, and reread & display it:

http://blogs.oracle.com/thejavatutorials/entry/watching_a_directory_for_changes

Upvotes: 1

Related Questions