Home » Java » java – Retrieving previous n lines of log4j specific to one logger class-Exceptionshub

java – Retrieving previous n lines of log4j specific to one logger class-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

We implemented a small routine that in case of specific errors we get an email to let us know something happened. It’s pretty easy to include some error information in the email but we would like to include what was the previous logged informations when it happened.

At first I tried to retrieve the last lines from the file where the log4j are saved. The problem is that many other threads are working at the same time and I never get relevant informations. Either the buffer doesn’t have time to write the logs or something else is faster at writing something else.

Is it possible to add an appender to a specific log4j at runtime to be able to retrieve only those specific logs and only if needed ?

I would like to be able to still log with the usual commands log.error, log.warn… but I want to be able to retrieve the log content in a try/catch situation. something like a memory only buffer appender?
Would that work for all the children loggers ?

Class 1 - call Class 2
Class 2 - call Class 3
Class 3 try/catch an error
log.getLogs returns all logged informations from Class 1..3

Or am I dreaming here ?

We are using Jira and in logs in files, so I guess no query would work.

How to&Answers: