找不到我的日志文件(Vaadin + Spring Boot + Maven项目)

时间:2016-02-14 20:34:58

标签: spring spring-boot log4j vaadin vaadin7

将我的项目迁移到Spring Boot后,我无法在任何地方找到我的日志消息。

如上所述here,我已将logging.properties文件放置到此目录:src/main/resources,并尝试将其放置到src/main/java

以下是我的logging.properties的内容:

log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
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=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

这是我的代码,我尝试记录某些内容:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.slf4j.bridge.SLF4JBridgeHandler;

public class BatchService {
    private final Logger logger = Logger.getLogger(BatchService.class.getName());

   //not sure what for it is, but i've both with and without this line
   static {
        SLF4JBridgeHandler.install();
    }
    public List<Batch> getBatchByDate(Date startDate) throws ParseException {
     //...some code, 
     //And yep, I've tried different variants of logging:
            logger.log(Level.ALL, "Where am i");
            logger.log(Level.INFO, "Where am i");
            logger.log(Level.DEBUG, "Where am i");
            logger.log(Level.ERROR, "Where am i");
            logger.info("Where am i");
            logger.debug("Where am i");
            logger.error("Where am i");
            System.out.println("Where am i");
     //some other code
        }
    }
}

这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>ru.my.home.project</groupId>
    <artifactId>themane</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Appl Name</name>
    <description>Appl Name bla-bla</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.vaadin.addons</groupId>
            <artifactId>loginform</artifactId>
            <version>0.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
        </dependency>
        <dependency>
            <groupId>ext.microsoft</groupId>
            <artifactId>mssql-jdbc-driver</artifactId>
            <version>2.1.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-spring-boot-starter</artifactId>
            <version>1.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-bom</artifactId>
                <version>7.5.5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.12.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generatePackage>hello.wsdl</generatePackage>
                    <schemas>
                        <schema>
                            <url>http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl</url>
                        </schema>
                    </schemas>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

结果 - 没有日志文件,控制台中没有我的消息,没有关于无法启动log4j的错误消息。在控制台中,我只能找到关于tomcat start等的标准Spring消息,所有这些美丽的spring徽标和bla-bla-bla。

而且,是的,实际的getBatchByDate调用工作正常 - 我可以在控制台中看到很多mybatis错误。

1 个答案:

答案 0 :(得分:1)

根据Spring boot docs,使用的默认实现是commons-looging。要switch to log4j,您需要进行一些更改,并且由于您使用的是初学者POM,因此最简单的方法就是他们在该页面上建议的内容:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
</dependency>