1. 程式人生 > >SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”

SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”

在使用logback 的時候  使用的例項

java程式碼:

package com.eastcom.my.study.first.logback;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogbackDemo {

	private static Logger logger = LoggerFactory.getLogger(com.eastcom.my.study.first.logback.LogbackDemo.class);

	public static void main(String[] args) {

		logger.trace("======trace");
		logger.debug("======debug");
		logger.info("======info");
		logger.warn("======warn");
		logger.error("======error");

		String name = "Aub";
		String message = "3Q";
		String[] fruits = { "apple", "banana" };

		// logback提供的可以使用變數的列印方式,結果為"Hello,Aub!"
		logger.info("Hello,{}!", name);
		// 可以有多個引數,結果為“Hello,Aub! 3Q!”
		logger.info("Hello,{}!   {}!", name, message);
		// 可以傳入一個數組,結果為"Fruit: apple,banana"
		logger.info("Fruit:  {},{}", fruits);

		System.out.println("hello world ");

	}

}

引入的jar包

        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.2.3</version>
</dependency>
    
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>


總是不能正常輸出日誌:

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.
hello world 

報錯 
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

添加了依賴包

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.25</version>
</dependency>


需要去掉  <scope>test</scope>

正常情況下輸出:

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <!-- encoders are assigned the type
                    ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
          <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="warn">
      <appender-ref ref="STDOUT" />
  </root>

</configuration>


over

如果加入logback.xml 檔案

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <!-- encoders are assigned the type
                    ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
          <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="warn">
      <appender-ref ref="STDOUT" />
  </root>

</configuration>

其配置檔案的路徑在src/logback.xml

在程式碼中需要從新載入配置檔案

java程式碼

package com.eastcom.my.study.first.logback;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;

public class LogbackDemo {

	private static Logger logger = LoggerFactory.getLogger(com.eastcom.my.study.first.logback.LogbackDemo.class);

	public static void main(String[] args) {

		LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
		JoranConfigurator configurator = new JoranConfigurator();
		configurator.setContext(lc);
		lc.reset();
		try {
			configurator.doConfigure("D:/newworkspace/my-study-first/src/logback.xml");
		} catch (JoranException e) {
			e.printStackTrace();
		}
		StatusPrinter.printInCaseOfErrorsOrWarnings(lc);

		System.out.println("===================");
		logger.debug("Hello {}", "debug message");

		logger.trace("======trace");
		logger.debug("======debug");
		logger.info("======info");
		logger.warn("======warn");
		logger.error("======error");

		String name = "Aub";
		String message = "3Q";
		String[] fruits = { "apple", "banana" };

		// logback提供的可以使用變數的列印方式,結果為"Hello,Aub!"
		logger.info("Hello,{}!", name);
		// 可以有多個引數,結果為“Hello,Aub! 3Q!”
		logger.info("Hello,{}!   {}!", name, message);
		// 可以傳入一個數組,結果為"Fruit: apple,banana"
		logger.info("Fruit:  {},{}", fruits);

		System.out.println("hello world ");

	}

}


可以看到只獲得warn的日誌輸出

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/softinstall/java/apache-maven-3/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/softinstall/java/apache-maven-3/repository/org/slf4j/slf4j-simple/1.7.25/slf4j-simple-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
===================
13:34:46.762 [main] WARN  c.e.m.s.first.logback.LogbackDemo - ======warn
13:34:46.773 [main] ERROR c.e.m.s.first.logback.LogbackDemo - ======error
hello world 


over 

在Linux下打包執行  指定配置檔案的位置,可也使用  -Dlogback.configurationFile=/data/myzjodc-test/conf/logback/logback.xml 

java -Dlogback.configurationFile=/data/myzjodc-test/conf/logback/logback.xml -cp .:./my-study-first-0.0.1-SNAPSHOT-jar-with-dependencies.jar  com.eastcom.my.study.first.logback.LogbackDemo2