If you use eclipse and tomcat for your development, the following method should work
Declare the following dependency in your pom.xml
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
Create a log4j.properties file and put it in the resources folder ("src / main / resources")
# Root logger option log4j.rootLogger=DEBUG, stdout, file # Redirect log messages to console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # Redirect log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender #outputs to Tomcat home log4j.appender.file.File=${catalina.home}/logs/mylogfile.log log4j.appender.file.MaxFileSize=5MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
And then in your class create an instance of the registrar
private static final Logger logger = Logger.getLogger(WelcomeController.class);
after that you can use the recorder anywhere
logger.debug("this will appear as a log in mylogfile.log!");
source share