Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple log files with log4j

Tags:

java

log4j

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.

like image 887
caarlos0 Avatar asked Dec 12 '11 12:12

caarlos0


People also ask

How do you create a new log file for each time the application runs log4j?

File with system properties name and some prefix text if you want. This will create a log file with current date time, something like this Log4jDemoApp-dd-MM-yyyy-hh-mm-ss. log every time we run the application. It will create the new file because for every run we are setting current date stamp in system properties.

Where is the log4j properties file located?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file.

What is log4j root logger?

The rootlogger is always the logger configured in the log4j. properties file, so every child logger used in the application inherits the configuration of the rootlogger . The logging levels are (from smaller to greater) : ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF .

What is additivity in log4j?

Additivity is set to true by default, that is children inherit the appenders of their ancestors by default. If this variable is set to false then the appenders found in the ancestors of this logger are not used.


1 Answers

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")

like image 125
korifey Avatar answered Oct 12 '22 07:10

korifey