Record log entries using Apache Log4j 2

I searched StackOverflow and the web for examples of using Apache Log4j 2 to parse existing log files. I read that Apache has a “Chainsaw” subproject, which is a tool for viewing log files, and that it should also have an API that could be used to independently analyze log entries. However, I could not find good evidence on this subject, and I cannot find Chainsaw in Apache Log4j 2.

What I would like would be something like the following (pseudocode / API):

String existingLogEntry = "2014-01-20 14:48:00,000 [thread-0] DEBUG (MyClass.java) - Some message"; String logPattern = "<substitute this with Log4j pattern that matches the above log entry>"; LogEntry entry = Parser.parse(logPattern, existingLogEntry); System.out.println("Date: " + entry.getDate()); System.out.println("Time: " + entry.getTime()); System.out.println("Thread: " + entry.getThread()); System.out.println("Log level: " + entry.getLogLevel()); System.out.println("Class: " + entry.getClassName()); System.out.println("Message: " + entry.getMessage()); 

So I need to know:

  • What dependencies to include in my Maven pom.xml file?
  • How to build a simple example that can use the Log4j template and an existing log entry based on this template, and then use the API to extract relevant information?

It should also be possible to extract custom values ​​/ MDC / Markes.

+6
source share

Source: https://habr.com/ru/post/981282/


All Articles