在eclipse上运行控制台应用程序时出错:SLF4J:无法加载类" org.slf4j.impl.StaticLoggerBinder"

时间:2017-11-30 00:00:10

标签: spring eclipse maven slf4j

我是Spring MVC的初学者,尝试在运行以下应用程序时让SLF4j记录器工作

package com.chris.springdemo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.chris.springdemo.dao.OrganizationDao;
import com.chris.springdemo.daoimpl.OrganizationDaoImpl;
import com.chris.springdemo.domain.Organization;

@Component
public class LoggingApp {

    @Autowired
    private OrganizationDao dao;

    @Autowired
    private DaoUtils daoUtils;

    public void actionMethod() {

        // Creating seed data
        daoUtils.createSeedData(dao);

        // List organizations
        List<Organization> orgs = dao.getAllOrganizations();
        daoUtils.printOrganizations(orgs, daoUtils.readOperation);

        // Create a new organization record
        Organization org = new Organization("General electric", 1991, "", 28, "Howdy");
        boolean isCreated = dao.create(org);
        daoUtils.printSuccessFailure(daoUtils.createOperation, isCreated);
        daoUtils.printOrganizations(dao.getAllOrganizations(), daoUtils.readOperation);

        // Get a single organization
        Organization org2 = dao.getOrganization(1);
        daoUtils.printOrganization(org2, "getOrganization");

        // Updating a slogan for an organization
        Organization org3 = dao.getOrganization(2);
        org3.setSlogan("We build **awesome** driving machines!");
        boolean isUpdated = dao.update(org3);
        daoUtils.printSuccessFailure(daoUtils.updateOperation, isUpdated);
        daoUtils.printOrganization(dao.getOrganization(2), daoUtils.updateOperation);

        // Delete an organization
        boolean isDeleted = dao.delete(dao.getOrganization(3));
        daoUtils.printSuccessFailure(daoUtils.deleteOperation, isDeleted);
        daoUtils.printOrganizations(dao.getAllOrganizations(), daoUtils.deleteOperation);

        // Clean up
        dao.cleanup();

        daoUtils.printOrganizationCount(dao.getAllOrganizations(), daoUtils.cleanupOperation);
    }

    public static void main(String[] args) {

        // creating the application context
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cp.xml");
        LoggingApp mainApp = ctx.getBean(LoggingApp.class);
        mainApp.actionMethod();

        // close the application context
        ((ClassPathXmlApplicationContext) ctx).close();
        // Create the bean
        // OrganizationDao dao = (OrganizationDao) ctx.getBean("orgDao");

    }

}

DaoUtils.java包含带Logger的日志语句

package com.chris.springdemo;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.chris.springdemo.dao.OrganizationDao;
import com.chris.springdemo.domain.Organization;

@Service
public class DaoUtils {

    private static Logger LOGGER = LoggerFactory.getLogger("Logging Tester");

    public final String createOperation = "CREATE";
    public final String readOperation = "READ";
    public final String updateOperation = "UPDATE";
    public final String deleteOperation = "DELETE";
    public final String cleanupOperation = "TRUNCATE";

    public void printOrganizations(List<Organization> orgs, String operation){
        LOGGER.info("\n********* printing organizations after " + operation + " operation *********");
        for (Organization org : orgs) {
            LOGGER.info(org.toString());
        }
    }

    public void printOrganization( Organization org, String operation ) {
        LOGGER.info("\n********* printing organization after invoking " + operation + " operation *********\n" + org);
    }

    public void printSuccessFailure(String operation, boolean param){
        if(param)
            LOGGER.info("\nOperation " + operation + " successful");
        else
            LOGGER.info("\nOperation " + operation + " failed");
    }

    public void createSeedData(OrganizationDao dao){
        Organization org1 = new Organization("Amazon", 1994, "65656", 8765, "Work hard, have fun, make history");
        Organization org2 = new Organization("BMW", 1929, "45454", 5501, "We build ultimate Driving machines");
        Organization org3 = new Organization("Google", 1996, "57575", 4567, "Don't be evil");

        List<Organization> orgs = new ArrayList<Organization>();
        orgs.add(0, org1); orgs.add(1, org2); orgs.add(2, org3);
        //int orgCount = orgs.size();
        int createCount = 0;
        for(Organization org : orgs){
            boolean isCreated  = dao.create(org);
            if(isCreated)
                createCount += 1;
        }

        LOGGER.info("Created "+ createCount + " organizations");
    }

    public void printOrganizationCount(List<Organization> orgs, String operation){
        LOGGER.info("\n*********Currently we have " + orgs.size()+ " organizations after " + operation + " operation" + "   *********");

    }

}

我的pom.xml在下面。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.chris</groupId>
    <artifactId>spring-named-jdbc-template-demo-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-dao</artifactId>
            <version>2.0.8</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>


    </dependencies>
</project>

当我运行应用程序时,我收到以下错误

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.

当我点击错误中的网址时,它会告诉我

  

此时会报告此警告消息   无法加载org.slf4j.impl.StaticLoggerBinder类   记忆。当找不到合适的SLF4J绑定时会发生这种情况   在班级路径上。放置一个(也是唯一一个)slf4j-nop.jar   slf4j-simple.jar,slf4j-log4j12.jar,slf4j-jdk14.jar或   类路径上的logback-classic.jar应该可以解决问题。以来   1.6.0从SLF4J版本1.6开始,在没有绑定的情况下,SLF4J将默认为无操作(NOP)记录器实现。

但是我的pom.xml上只有一个SLF4J实现

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

我删除了所有存储库,更新了maven项目,清理了项目等,这似乎都没有解决这个问题。非常感谢任何帮助。

由于 克里斯

0 个答案:

没有答案