Multiple ways to solve SLF4J: Defaulting to no-operation (NOP) logger implementation error

1. Reproduce the error


Today, I am writing and using Quartzthe method of executing timed tasks, as shown in the following code:

public class QuartzTest {
    
    

  public static void main(String[] args) throws SchedulerException {
    
    
      // 1、创建Scheduler(调度器)
      SchedulerFactory schedulerFactory = new StdSchedulerFactory();
      Scheduler scheduler = schedulerFactory.getScheduler();
      // 2、创建JobDetail实例,并与SimpleJob类绑定
      JobDetail jobDetail = JobBuilder.newJob(SimpleJob.class)
              .withIdentity("job-1", "job-group").build();
      // 3、构建Trigger(触发器),定义执行频率和时长
      CronTrigger cronTrigger = TriggerBuilder.newTrigger()
              .withIdentity("trigger-1", "trigger-group")
              .startNow()  //立即生效
              .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ? "))
              .build();

      //4、执行
      scheduler.scheduleJob(jobDetail, cronTrigger);
      scheduler.start();
  }

class SimpleJob implements Job {
    
    
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
    
    
      // 创建一个事件,下面仅创建一个输出语句作演示
      final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      System.out.println(
          Thread.currentThread().getName() + "--" + simpleDateFormat.format(new Date()));
    }
  }
}

When starting, the following error is reported:

insert image description here

Right nowSLF4J: Defaulting to no-operation (NOP) logger implementation

2. Analysis errors


First, find this sentence in the picture above SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details..

Translate it into Chinese yes SLF4J:有关更多详细信息,请参阅 http://www.slf4j.org/codes.html#StaticLoggerBinder。.

So, click on the link https://www.slf4j.org/codes.html#StaticLoggerBinder , as shown in the figure below:

insert image description here
However, this is all in English, so we might as well translate it into Chinese, as follows:

Could not load class org.slf4j.impl.StaticLoggerBinder

This warning message is reported when a org.slf4j.impl.StaticLoggerBinderclass cannot be loaded into memory. SLF4JThis happens when an appropriate binding cannot be found on the classpath . Putting one (and only one) on the classpath slf4j-nop.jar slf4j-simple.jar,slf4j-log4j12.jar,slf4j-jdk14.jar或logback-classic.jarshould do the trick.

Note that slf4j-apiversions 2.0.xand higher use ServiceLoaderthe mechanism. Targeted slf4j-api 2.xbackends (such as logback 1.3and later) are not org.slf4j.impl.StaticLoggerBindershipped with it. If placing a oriented slf4j-api 2.0.xlogging backend, it needs to be on the classpath slf4j-api-2.x.jar. See also the related FAQ entry. Since

version 1, will default to a no-op( ) logger implementation in the absence of a binding . If you are responsible for packaging your application and don't care about logging, putting this on the application's classpath will get rid of this warning message. Note that embedded components such as libraries or frameworks should not declare a dependency on any bindings, but only on . When a library declares a compile-time dependency on a binding, it imposes that binding on the end user, thereby negating the purpose of .1.6.0SLF4J1.6SLF4JNOP

slf4j-nop.jarSLF4Jslf4j-apiSLF4JSLF4J

slf4j-log4j12.jarIn general, this dependency is missing .

So, looking for my project, there is indeed a missing slf4j-log4j12.jardependency.

3. Fix bugs


Since my project lacks slf4j-log4j12.jardependencies, pom.xmlI can add the following in :

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>2.0.7</version>
    <scope>compile</scope>
</dependency>

Restart the project and it will work fine.

4. Other ways to resolve this error


Because my mistake is missing slf4j-log4j12dependencies in the project, pom.xmljust add the dependencies in .

If you have added slf4j-log4j12dependencies to the project, but still report an error, you can refer to the following solutions.

Check scopeif range() is set to test, as shown in the following code:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>2.0.7</version>
    <scope>test</scope>
</dependency>

insert image description here

If scope ( scope) is set to test, that is to say only in the test environment, it can be used.

And I mainrun it directly in the method, so it does not belong to the test environment, so this package is equivalent to not adding dependencies.

The scope ( ) can be scopemodified compileas shown in the following figure:

insert image description here

Guess you like

Origin blog.csdn.net/lvoelife/article/details/130017688