开发者社区> 问答> 正文

XML配置中的spring log4j无法创建日志信息文件

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN" monitorInterval="30">
        <Properties>
            <Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
            <Property name="APP_LOG_ROOT">/home/sangamnath/Desktop</Property>
        </Properties>
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT" follow="true">
                <PatternLayout pattern="${LOG_PATTERN}" />
            </Console>

            <RollingFile name="appLog"
                fileName="${APP_LOG_ROOT}/SpringBoot2App/application.log"
                filePattern="${APP_LOG_ROOT}/SpringBoot2App/application-%d{yyyy-MM-dd}-%i.log">
                <PatternLayout pattern="${LOG_PATTERN}" />
                <Policies>
                    <SizeBasedTriggeringPolicy size="19500KB" />
                </Policies>
                <DefaultRolloverStrategy max="1" />
            </RollingFile>

        </Appenders>
        <Loggers>

            <Logger name="com.howtodoinjava.app" additivity="false">
                <AppenderRef ref="appLog" />
                <AppenderRef ref="Console" />
            </Logger>

            <Root level="debug">
                <AppenderRef ref="Console" />
            </Root>
        </Loggers>
    </Configuration>




public class BatchApplication {

    public static void main(String[] args) {
            ApplicationContext ctx = SpringApplication.run(BatchApplication.class, args);
            //logger.info("Application started");


        logger.debug("This is a debug message");
        logger.info("This is an info message");
        logger.warn("This is a warn message");
        logger.error("This is an error message");
        logger.fatal("This is a fatal message");


    }

}

}


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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.shi</groupId>
    <artifactId>rkjobs</artifactId>
    <version>0.0.1</version>
    <name>batch</name>
    <description>drkjobs</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-log4j2</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>

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




    </dependencies>

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <build.profile.id>dev</build.profile.id>
                <profileActive>dev</profileActive>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <build.profile.id>test</build.profile.id>
                <profileActive>test</profileActive>
            </properties>
        </profile>
        <profile>
            <id>uat</id>
            <properties>
                <build.profile.id>uat</build.profile.id>
                <profileActive>uat</profileActive>
            </properties>
        </profile>
        <profile>
            <id>local</id>
            <properties>
                <build.profile.id>local</build.profile.id>
                <profileActive>local</profileActive>
            </properties>
        </profile>
    </profiles>

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

</project>

XML配置中的spring log4j无法创建用于日志信息的文件。

日志消息能够在控制台中打印。但是在文件中,无法在所需的文件夹中创建打印日志文件

问题来源:Stack Overflow

展开
收起
montos 2020-03-23 10:59:33 1145 0
1 条回答
写回答
取消 提交回答
  • Your logback.xml file should be in this format : 
    
        <?xml version="1.0" encoding="UTF-8"?>
        <configuration>
    
            <property name="DEV_HOME" value="logs/demo.log" />
    
            <appender name="FILE-AUDIT"
                class="ch.qos.logback.core.rolling.RollingFileAppender">
                <file>${DEV_HOME}/debug.log</file>
                <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                    <Pattern>
                        %d{yyyy-MM-dd HH:mm:ss}- %t - %msg%n
                    </Pattern>
                </encoder>
    
                <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                    <!-- rollover daily -->
                    <fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
                    </fileNamePattern>
                    <timeBasedFileNamingAndTriggeringPolicy
                        class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                        <maxFileSize>100KB</maxFileSize>
                    </timeBasedFileNamingAndTriggeringPolicy>
                </rollingPolicy>
    
            </appender>
            <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
                <encoder>
                    <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -
                        %msg%n</pattern>
                </encoder>
            </appender>
    
            <logger name="com.demo" level="debug" additivity="false">
                <appender-ref ref="FILE-AUDIT" />
            </logger>
    
            <root level="info">
                <appender-ref ref="STDOUT" />
            </root>
    
        </configuration>
    
    And the properties should be mentioned in application.properties file if its an spring boot application : 
    
    
        logging.level.org.springframework.web: INFO
        logging.level.org.hibernate:TRACE
        endpoints.shutdown.enabled=true
    
    And Logger can be defined in every class where the logs needs to be included in this way : 
    
        private static final Logger logger = LoggerFactory.getLogger(demo.class);
    

    回答来源:Stack Overflow

    2020-03-23 11:00:03
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PostgresChina2018_赖思超_PostgreSQL10_hash索引的WAL日志修改版final 立即下载
Kubernetes下日志实时采集、存储与计算实践 立即下载
日志数据采集与分析对接 立即下载