0

Is it possible to group logs in Spring java, currently we have a lot of errors, and want to group logs on a key. This is in a multithreaded environment

I can do the following:

public class LogGroup {
    private static final Logger logger = LoggerFactory.getLogger(LogGroup.class);
    private List<String> logMessages = new ArrayList<>();

    public void addLogMessage(String message) {
        logMessages.add(message);
    }

    public void printGroupedLogs() {
        StringBuilder sb = new StringBuilder();
        for (String message : logMessages) {
            sb.append(message).append(System.lineSeparator());
        }
        logger.info(sb.toString());

        // Clear log messages after printing
        logMessages.clear();
    }
}

And use that to add to a list and print and clear at the end.

user1555190
  • 2,803
  • 8
  • 47
  • 80
  • 1
    There is no guarantee this will actually group your messages. Instead, use a context (MDC) parameter, or possibly the thread name. – Jorn Jul 27 '23 at 13:35
  • Please refer to this thread that might answer your question. Would recommend to use logback, https://stackoverflow.com/questions/36875541/process-id-in-logback-logging-pattern?rq=3 – gcpdev-guy Jul 27 '23 at 14:08

1 Answers1

0

A log framework normally sends logs to a log server, something running in a separate thread or even process. This enables the log server to run on a lower priority, and to flush all logs even in an application is OOM or forced killed. Building your own log queue may cause you problem when your application malfunctions, and that may be when you need the loggs the most! This implementation will also remove a lot of useful things in the logger, like parameterised logging using varargs, attaching stack traces etc.

I would recommend that you try to markup each log message with some group ID, and then use that group ID to group the messages at read-time rather than at insert time.

If the grouping is per thread or other context dependent value, use MDC to attach the value to each log message. If the grouping is per class or method, use java class methods.

Depending on what tool you use to visualise the logs, most tools today supports JSON structured logging. To use that, you add a log appender in spring to have each log message as a JSON object. Then you put your Group ID in its separate JSON property, that will make your group filtering more robust compared to free text filtering.

(To write a log appender is not hard, but maybe a bit out of context for this answer. For logback, you can search for open source code containing "extends EncoderBase" to find several examples. Then add it to logback.xml under configuration/appender/encoder

Andreas Lundgren
  • 12,043
  • 3
  • 22
  • 44