capint
7/23/2017 - 12:21 PM

Java >> SLF4J >> Tutorial

Java >> SLF4J >> Tutorial

Source: http://www.javavillage.in/slf4j-with-log4j.php

1. Add dependencies
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.13</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.13</version>
		</dependency>
		
2. Add log4j.properties to src/main/resources
  # Root logger option
  log4j.rootLogger=INFO, file, stdout
  	 
  # Direct log messages to stdout
  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} [%t] %-5p:: %m%n
  
  # Direct log messages to a log file
  log4j.appender.file=org.apache.log4j.RollingFileAppender 
  log4j.appender.file.File=D:\\Workspace\\Tutorials\\slf4j\\log\\tutorial.log
  log4j.appender.file.MaxFileSize=10000KB
  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} [%t] %-5p:: %m%n
  
3. Use LoggerFactory to create and use Logger
    private static Logger LOGGER = LoggerFactory.getLogger(SLF4JSample.class);
    public static void main(String[] args) {
        LOGGER.info("Hi, Welcome to SLF4J exmaple");
    }