简体   繁体   中英

can not write log in separate file using Log4J and spring boot application in tomcat server

I am new to the spring-boot applications and want to manage log properties to write logs to a separate file. But my logs print only in 'Catalina.out' file.

Here is some part of pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
    .
    .
    .
    <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-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    .
    .
    .

here is the log4j2-spring.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
<Properties>
    <Property name="log-path">${catalina.base}/logs</Property>
</Properties>
<Appenders>
    <Console name="Console-Appender" target="SYSTEM_OUT">
        <PatternLayout>
            <pattern>
                [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
            </pattern>>
        </PatternLayout>
    </Console>
    <File name="App-File-Appender" fileName="${log-path}/app_log.log" >
        <PatternLayout>
            <pattern>
                [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
            </pattern>
        </PatternLayout>
    </File>
    <File name="SpringBoot-File-Appender" fileName="${log-path}/springboot_log.log" >
        <PatternLayout>
            <pattern>
                [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
            </pattern>
        </PatternLayout>
    </File>
</Appenders>
<Loggers>
    <Logger name="org.springframework.web" level="info" additivity="false">
        <AppenderRef ref="SpringBoot-File-Appender"/>
        <AppenderRef ref="Console-Appender"/>
    </Logger>
    <Logger name="guru.springframework.blog.controllers" level="info" additivity="false">
        <AppenderRef ref="App-File-Appender"/>
        <AppenderRef ref="Console-Appender"/>
     </Logger>
    <Root>
        <AppenderRef ref="Console-Appender"/>
    </Root>
</Loggers>

My spring-boot class:

@SpringBootApplication
 public class MyApplication {

public static void main(String[] args) {
    SpringApplication.run(MyApplication.class, args);

}

I wonder why my log file doesn't create logs didn't write in '>${catalina.base}/logs/logfile.

I also test putting this line in 'application.properties' :

logging.file=../logs/mylog.log

You should write your application root package name ie the package contains all subpackages and classes of your application in Logger element name attribute -

In below lines -

<Logger name="guru.springframework.blog.controllers" level="info" additivity="false">
        <AppenderRef ref="App-File-Appender"/>
        <AppenderRef ref="Console-Appender"/>
     </Logger>

Replace guru.springframework.blog.controllers with your application root package name.

Alternatively, you can add App-File-Appender in Root Logger -

<Root>
    <AppenderRef ref="App-File-Appender"/>
    <AppenderRef ref="Console-Appender"/>
</Root>

But, the general convention of using RootLogger is to log error messages of the application dependencies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM