0

I would like to explain my problem directly with example. Please add comments if some other information is also required.

I have a small application(SpringBoot+Java), and it uses ThreadPool for executing several Tasks. So lets say there are 2 Threads in my pool. Right now, log4j is used for logging. It writes all threads' logs into single file and all 2 threads' log data is interleaved.

So each thread works like -

[Thread-1][08:15:00] entered into application
[Thread-1][08:15:00] entered method A
[Thread-1][08:15:05] exit method A
[Thread-1][08:15:10] entered method B
[Thread-1][08:15:15] exit method B
[Thread-1][08:15:30] exit from application

[Thread-2][08:15:01] entered into application
[Thread-2][08:15:01] entered method A
[Thread-2][08:15:04] exit method A
[Thread-2][08:15:11] entered method B
[Thread-2][08:15:12] exit method B
[Thread-2][08:15:22] exit from application

but my log file has these entries on basis of timestamp-

[Thread-1][08:15:00] entered into application
[Thread-1][08:15:00] entered method A
[Thread-2][08:15:01] entered into application
[Thread-2][08:15:01] entered method A
[Thread-2][08:15:04] exit method A
[Thread-1][08:15:05] exit method A
[Thread-1][08:15:10] entered method B
[Thread-2][08:15:11] entered method B
[Thread-2][08:15:12] exit method B
[Thread-2][08:15:14] exit from application
[Thread-1][08:15:15] exit method B
[Thread-1][08:15:30] exit from application

I want my log file to print thread wise execution, and not timestamp wise. I want my log file to print each thread completely before other thread starts. How could i achieve this.

I have already searched and found below article as the closest, but i dont want different log files- How to log multiple threads in different log files?

I need to output all log data into single log file grouped by thread name, and sorted by thread start time.

anubhs
  • 566
  • 1
  • 8
  • 26
  • Please dont scold me, that for only 2 threads, i need to change my framework. I am here giving example by 2 Threads, my real application has more than 10 Threads, and debugging any particular case is a pain if i read the logs. – anubhs Mar 26 '19 at 20:44
  • 1
    Wouldn't it be easier to use some tool for logs reading? A simple unix `grep` would do the work just fine – Jakub Licznerski Mar 26 '19 at 20:49
  • 1
    It can be tricky and costly to get the log output to get grouped by threads -- think about it, you'd need to keep everything in memory before a given threshold is reached, after which you could write the log entries separately. Could you consider using a dedicated log viewer that enables you to filter or group by threads? There are lots of good log viewer alternatives that do this. – Mick Mnemonic Mar 26 '19 at 20:51
  • yes @Jakub, but i want to transform the logs into something else, and I could not do that until i have the threadwise data. I can perform string operations on a .log file, but, i wont prefer it on run time. – anubhs Mar 26 '19 at 20:53
  • @MickMnemonic can you suggest some log viewer tool which would help in my case? – anubhs Mar 26 '19 at 20:56
  • [Otro's Log Viewer](https://github.com/otros-systems/otroslogviewer) has plenty of features for filtering etc. If you have a lot of log data, [GrayLog](https://www.graylog.org/products/open-source) is a really sophisticated tool for statistical analysis and anomaly detection. – Mick Mnemonic Mar 26 '19 at 21:10

1 Answers1

0

As I guess that separating logs in console appender won't work I'd suggest defining separate FileAppenders for each Thread just like it's described here

Jakub Licznerski
  • 1,008
  • 1
  • 17
  • 33