7

I am creating a solution for my application log, which have various types of logging (user, application, etc...), wanted to save each type of log in a separate file.

This is possible with log4j or some other API? How could I do that?

If you deem interesting, I edit the question and put the codes, but I do not think it's worth, they are still very basic.

Thanks in advance.

caarlos0
  • 20,020
  • 27
  • 85
  • 160
  • what do you mean with various types of logging? Log Levels (DEBUG, INFO, WARN...) OR different classes in which the log code happens OR do you want different log files depending on the user who just happens to run through the code OR do you want it to be different by thread name? – Angelo Fuchs Dec 12 '11 at 12:25
  • 1
    possible duplicate of [creating-multiple-log-files-of-different-content-with-log4j](http://stackoverflow.com/questions/728295/creating-multiple-log-files-of-different-content-with-log4j) – olly_uk Dec 12 '11 at 12:26
  • @olly_uk I think thats not a duplicate. – caarlos0 Dec 12 '11 at 12:36

2 Answers2

24

Of cource, use different FileAppenders Example from internet:

log4j.rootLogger=DEBUG

# AdminFileAppender - used to log messages in the admin.log file.
log4j.appender.AdminFileAppender=org.apache.log4j.FileAppender

log4j.appender.AdminFileAppender.File=admin.log

log4j.appender.AdminFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.AdminFileAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n

# ReportFileAppender - used to log messages in the report.log file.
log4j.appender.ReportFileAppender=org.apache.log4j.FileAppender

log4j.appender.ReportFileAppender.File=report.log

log4j.appender.ReportFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ReportFileAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n

log4j.logger.com.vaannila.admin=WARN,AdminFileAppender 
log4j.logger.com.vaannila.report=DEBUG,ReportFileAppender 

Now you can log to admin.log Logger.getLogger("com.vaannila.admin").log("To admin log") and to report log Logger.getLogger("com.vaannila.report").log("To report log")

korifey
  • 3,379
  • 17
  • 17
  • I dont understand the part "Logger.getLogger("com.vaannila.admin").log("To admin log")", how log4j will know that i want to log AdminFileAppender, in that example? I try to do something similar here, but it don't work... – caarlos0 Dec 12 '11 at 12:43
  • 1
    log4j.logger.`com.vaannila.admin`=WARN,AdminFileAppender this property says that if you log to `com.vaannila.admin` or `com.vaannila.admin.xxx.yyy.zzz` then use AdminFileAppender (which is FileAppender with file 'admin.log') – korifey Dec 12 '11 at 12:50
  • But this becomes package specific. What if some part of the admin code is in a different package and does not match with com.vaanila.admin instead if it is com.vaanila.util. How do we now log a specific part of code into the admin.log? – Tushar Banne Apr 24 '17 at 18:19
2

Log4j provides for Loggers and Appenders. The way to do this is to have an Appender for each file you want. They set up an appropriate set of Loggers that point to the appropriate Appender(s). Loggers are usually set up using package names. If this will work for you, code in package x goes to file y, just do that. Otherwise, create Loggers per output file and have the code choose the appropriate logger. You can also have Loggers and send the information to multiple Appenders, so you can set these up as appropriate.

See korifey's post for an example of how to set this up.

John B
  • 32,493
  • 6
  • 77
  • 98