Simplest way to output "Hello world" using SLF4J

By xngo on February 25, 2019

package net.xngo.tutorial.java.slf4j;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * Simplest way to output "Hello world" using SLF4J.
 * You need slf4j-api-X.jar and a binding jar(e.g. slf4j-simple-X.jar, 
 *          slf4j-log4j12-X.jar, slf4j-jdk14-X.jar, etc)
 * Bindings: http://www.slf4j.org/faq.html#where_is_binding         
 */
public class SLF4JBasic
{
  // Pros/Cons of static Logger: http://slf4j.org/faq.html#declared_static
  final static Logger logger = LoggerFactory.getLogger(SLF4JBasic.class);
 
  public static void main(String[] args)
  {
    logger.info("Hello World");
    System.out.println("Hello World from System.out.");
 
 
    /**
     * You don't configure your logging on SLF4j but through the logger
     * implementation used(i.e. binding used).
     * slf4j-simple-X.jar: http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html
     *    It's either through system property
     *      -Dorg.slf4j.simpleLogger.defaultLogLevel=debug
     *        or
     *      -In simplelogger.properties file in your classpath(http://stackoverflow.com/a/5081386).     
     */
  }
 
}
 
/**************************************************************************
 *            Different logger configuration file sample
 **************************************************************************/
// Reference: http://saltnlight5.blogspot.ca/2013/08/how-to-configure-slf4j-with-different.html
 
/* simplelogger.properties:
      org.slf4j.simpleLogger.defaultLogLevel=debug
      org.slf4j.simpleLogger.showDateTime=true
      org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss.SSS
 */
 
 
/* log4j.properties:
 
 */
 
/* logback.xml: java -Dlogback.configurationFile=/path/to/config.xml
      <!--
        You need slf4j-api-X.jar, logback-classic-X.jar and logback-core-X.jar
 
        Pattern definition:
          http://logback.qos.ch/manual/layouts.html#conversionWord
      -->
      <configuration>
        <appender name="FILE" class="ch.qos.logback.core.FileAppender">
          <file>myApp.log</file>
          <encoder>
            <pattern>%date %level [%thread] %-5level %logger{10} [%file:%line] %msg%n</pattern>
          </encoder>
        </appender>
 
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
          <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
          </encoder>
        </appender>
 
        <root level="debug">
          <appender-ref ref="STDOUT" />
          <appender-ref ref="FILE" />
        </root>
      </configuration> 
*/

Github

  • https://github.com/xuanngo2001/Tutorial/blob/master/src/net/xngo/tutorial/java/slf4j/SLF4JBasic.java

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.