2016年12月22日 星期四

Simple Logger Facade SLF4J Java 8 on Linux

Main Reference
http://www.slf4j.org/manual.html
http://ifeve.com/slf4j-manual/

Confirm your JDK
[ricky@thrift2 SimpleLoggingFacade]$ javac -version
javac 1.8.0_111

Download it to ~/SimpleLoggingFacade
wget http://www.slf4j.org/dist/slf4j-1.7.22.tar.gz
tar zxvf slf4j-1.7.22.tar.gz

HelloWorld.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}

Error 1 : org.slf4j does not exist
[ricky@thrift2 SimpleLoggingFacade]$ javac HelloWorld.java
HelloWorld.java:1: error: package org.slf4j does not exist
import org.slf4j.Logger;
                ^
HelloWorld.java:2: error: package org.slf4j does not exist
import org.slf4j.LoggerFactory;
                ^
HelloWorld.java:6: error: cannot find symbol
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    ^
  symbol:   class Logger
  location: class HelloWorld
HelloWorld.java:6: error: cannot find symbol
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
                    ^
  symbol:   variable LoggerFactory
  location: class HelloWorld
4 errors

Solution 1: org.slf4j does not exist
javac -cp ".:./slf4j-1.7.22/slf4j-api-1.7.22.jar" HelloWorld.java

Error 2: Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
[ricky@thrift2 SimpleLoggingFacade]$ java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at HelloWorld.main(HelloWorld.java:6)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

Solution 2 : Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
[ricky@thrift2 SimpleLoggingFacade]$ java -cp ".:./slf4j-1.7.22/slf4j-api-1.7.22.jar" HelloWorld
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Error 3: Failed to load class "org.slf4j.impl.StaticLoggerBinder"
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"

Solution 3: Failed to load class "org.slf4j.impl.StaticLoggerBinder"
java -cp ".:./slf4j-1.7.22/slf4j-api-1.7.22.jar:./slf4j-1.7.22/slf4j-simple-1.7.22.jar" HelloWorld

[main] INFO HelloWorld - Hello World

Solution 3 using simple:
java -cp ".:./slf4j-1.7.22/slf4j-api-1.7.22.jar:./slf4j-1.7.22/slf4j-simple-1.7.22.jar" HelloWorld
[main] INFO HelloWorld - Hello World

Solution 3 using jdk14:
java -cp ".:./slf4j-1.7.22/slf4j-api-1.7.22.jar:./slf4j-1.7.22/slf4j-jdk14-1.7.22.jar" HelloWorld

Dec 22, 2016 3:52:36 PM HelloWorld main
INFO: Hello World

Solution 3 using nop:
java -cp ".:./slf4j-1.7.22/slf4j-api-1.7.22.jar:./slf4j-1.7.22/slf4j-nop-1.7.22.jar" HelloWorld

Solution 3 using JCL:
wget http://apache.website-solution.net//commons/logging/binaries/commons-logging-1.2-bin.tar.gz

tar zxvf commons-logging-1.2-bin.tar.gz
java -cp ".:./slf4j-1.7.22/slf4j-api-1.7.22.jar:./slf4j-1.7.22/slf4j-jcl-1.7.22.jar:./commons-logging-1.2/commons-logging-1.2.jar" HelloWorld
Dec 22, 2016 3:51:22 PM HelloWorld info
INFO: Hello World

Solution 3 using log4j 1.2:
echo "Need to download log4j-1.2.17.tar.gz"
echo "Do not download apache-log4j-2.7-bin.tar.gz"
wget http://apache.01link.hk/logging/log4j/1.2.17/log4j-1.2.17.tar.gz

tar zxvf log4j-1.2.17.tar.gz

java -cp ".:./slf4j-1.7.22/slf4j-api-1.7.22.jar:./slf4j-1.7.22/slf4j-log4j12-1.7.22.jar:./apache-log4j-1.2.17/log4j-1.2.17.jar" HelloWorld

Error 4 : WARN No appenders could be found for logger (HelloWorld)
log4j:WARN No appenders could be found for logger (HelloWorld).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Solution 4 : WARN No appenders could be found for logger (HelloWorld)
vi log4j.properties
log4j.rootLogger=debug, 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=%t %-5p %c{2} - %m%n

Solution 3 using log4j 1.2 alternative (simpler):

mkdir lib
cp ./slf4j-1.7.22/slf4j-api-1.7.22.jar ./lib/
cp ./slf4j-1.7.22/slf4j-log4j12-1.7.22.jar ./lib/
wget http://apache.01link.hk/logging/log4j/1.2.17/log4j-1.2.17.tar.gz
tar zxvf log4j-1.2.17.tar.gz
cp ./apache-log4j-1.2.17/log4j-1.2.17.jar ./lib/
java -cp ".:./lib/*" HelloWorld

a
sdas

沒有留言:

張貼留言

2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...