Search code examples
springspring-bootlog4jvaadinvaadin7

Can't find my log files (Vaadin + Spring Boot + Maven project)


After migrating my project to Spring Boot I can't find my log messages anywhere.

As it mentioned here, I've placed logging.properties file to this dir: src/main/resources and also tried to place it to src/main/java.

Here is a content of my 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

Here is my code, where I try to log something:

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
        }
    }
}

Here is my 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>

And as a result - there is no log file, there is no my messanges in the console, there is no even error messanges about inability to initiate log4j. In the console I can only find standard Spring messages about tomcat start and so on, all these beautiful spring logos, and bla-bla-bla.

And, yep, the actual getBatchByDate call works fine - I can see a lot of mybatis errors in the console.


Solution

  • According to the Spring boot docs, the default implementation used is commons-looging. In order to switch to log4j, you need make some changes, and since you're using starter POMs the simplest way is what they already suggest on that page:

    <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>