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.
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.
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.
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 .
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With