0

I am writing a web application where the user (among many other things) can build and run their own scripts. The scripts are run on the backend, where they may or may not generate erros - such errors are written to the system log at the moment.

It would be beneficial for the user to see these kind of log events via the web application itself, so I was thinking to pipe & filter them from log4j somehow (before the events hit the disk), so that I have programmatic access to them. Elasticsearch has something that resembles this - when no results are found, it shows the errors that have happened in the given timeframe.

I guess I could just abstract away logging, and make sure all the events are sent to log4j and to the client side as well, but I am hoping there is a better way, without having to touch the logger calls (or having to read from the log files).

Looked at the log4j2 website, but did not find anything useful there - any ideas how to achieve something like this?

3 Answers 3

2

To intercept logging events you can insert your part in three points:

  • after log4j: Let Log4j log to something you can easily "query" in your application. There are various Appenders available that could be useful for this. Logging to a database is one option, you could log to standard out and redirect that into your application, so you get some kind of stream

  • using an appender: You can implement your own appender, configure it with log4j and do whatever you want in there.

  • before log4j: Wrap every call to a Log4j Logger to an implementation of your own and do your logic in there before or after actually logging your event.

The appender approach seems to be the most elegant: It allows using the log4j configuration options to control what shows up in your frontend, and what not, and since the appender is configured in the same file makes it less likely somebody disables the logging due to not realizing it is important for the user. But it requires the most knowledge about log4j internals.

1
  • Agree with the appender approach being most elegant. I did a few trials, and it seems to be working nicely.
    – sfThomas
    Jan 19, 2016 at 5:55
1

One possible solution might be to use a piped input stream.

// pipes output in the output stream to the input stream
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(in);

// create custom l4j appender
Appender customAppender = new WriterAppender(
                                  new PatternLayout("%-5p %d [%t][%F:%L] : %m%n"), out);

//use any logger:
logger.addAppender(customAppender);
//or the root logger: Logger.getRootLogger().addAppender(customAppender);

// write a test message to log
logger.error("test");

// printing entries in piped input stream
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
String line;
while (bufferedReader.ready() && (line = bufferedReader.readLine()) != null) {
    System.out.println("log entry: " + line);
}

Resulting output:

log entry: ERROR 2015-11-24 11:58:18,366 [main][Main.java:52] : test

Of cause you can use filters for the appender (or define your own filters). For example to filter the log level range:

LevelRangeFilter levelRangeFilter = new LevelRangeFilter();
levelRangeFilter.setLevelMin(Level.INFO);
levelRangeFilter.setLevelMax(Level.ERROR);
customAppender.addFilter(levelRangeFilter);
0

I would go with logging to database since you don't want to parse the logs. One suggestion could be log4j_logging_database.

This would of course mean that you write the queries and format the logging to make it parse-able from sql and also architect a way to present these to the client side.

You can find a tutorial here. There is a logging level so you can filter out errors and important staff depending on your configuration. Don't forget to add indexes to the table columns of interest as logging can get massive after some time.

Best Regards,

Thanos

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.