Multiple log files with log4j

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

Is this possible with log4j or some other API? How can i do this?

If you find it interesting, I will edit the question and put the codes, but I don’t think it’s worth it, they are still very simple.

Thanks in advance.

+6
source share
2 answers

In the course, use different FileAppenders. Example from the 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 

You can now log in to admin.log Logger.getLogger("com.vaannila.admin").log("To admin log") and report the log Logger.getLogger("com.vaannila.report").log("To report log")

+22
source

Log4j provides Loggers and Appenders. The way to do this is to have an appender for every file you want. They created an appropriate set of registrars that point to their respective Appender (s). Loggers are usually configured using package names. If this works for you, the code in package x will go to file y, just do it. Otherwise, create Loggers for each output file and indicate that the code selects the appropriate logger. You can also have Loggers and send information to multiple Appenders, so you can set them accordingly.

See the korifey post for an example of how to fix this.

+2
source

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


All Articles