0

I am working on an application that uses a lot of Worker threads to work on content. Each of these Threads is supposed to write to its own log file, following a specific naming convention, mainly dependent on one String value that each thread gets passed in at creation time.

I have found no way of doing this with log4j that does not involve writing all possible appenders to the log4j configuration at compile time. This is hardly acceptable for my application. The project already contains a lot of references to a Logger object which I do not wish to rewrite.

So far I've started writing a class that implements Logger but the cast from MyLogger to Logger does not go through. Changing the references from Logger to MyLogger isn't exactly rocket science but I had hoped to stay very close to log4j in my implementation.

Is there an easier way to do this with log4j? If not - does anyone have a how-to or tutorial on how to write you own logging implementation?

Chris Barlow
  • 3,274
  • 4
  • 31
  • 52
Splort
  • 1

2 Answers2

1

If you switch to you can take advantage of SiftingAppender. It does exactly what you want: separate logs based on some criteria into different appenders, e.g. files. By default it uses (probably a good idea), but you can easily write your own Discriminator.

Note that switching from Log4J to Logback isn't that problematic: log4j-over-slf4j. Basically you can switch without changing a single line of code, still using Log4J API.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
0

You can implement your own Appender that will append log events to different files according to thread. This appender can be registered in Log4J configuration without changing your logging code. This tutorial seems similar to your problem.

Rorick
  • 8,857
  • 3
  • 32
  • 37