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

huangapple go评论77阅读模式
英文:

spring log4j in XML configuration not able to create the file for log information

问题

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>

BatchApplication.java:

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 and profiles... -->
    <!-- Omitted for brevity -->
</project>

Regarding the issue you mentioned about the log message being able to print in the console but not being able to create the log file in the required folder, it seems like you might have a configuration or permission issue in your application. You might want to check if the specified log file directory exists and if the application has the necessary permissions to write to that directory.

英文:

log4j2.xml

 &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;Configuration status=&quot;WARN&quot; monitorInterval=&quot;30&quot;&gt;
&lt;Properties&gt;
&lt;Property name=&quot;LOG_PATTERN&quot;&gt;%d{yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSSZ} %p %m%n&lt;/Property&gt;
&lt;Property name=&quot;APP_LOG_ROOT&quot;&gt;/home/sangamnath/Desktop&lt;/Property&gt;
&lt;/Properties&gt;
&lt;Appenders&gt;
&lt;Console name=&quot;Console&quot; target=&quot;SYSTEM_OUT&quot; follow=&quot;true&quot;&gt;
&lt;PatternLayout pattern=&quot;${LOG_PATTERN}&quot; /&gt;
&lt;/Console&gt;
&lt;RollingFile name=&quot;appLog&quot;
fileName=&quot;${APP_LOG_ROOT}/SpringBoot2App/application.log&quot;
filePattern=&quot;${APP_LOG_ROOT}/SpringBoot2App/application-%d{yyyy-MM-dd}-%i.log&quot;&gt;
&lt;PatternLayout pattern=&quot;${LOG_PATTERN}&quot; /&gt;
&lt;Policies&gt;
&lt;SizeBasedTriggeringPolicy size=&quot;19500KB&quot; /&gt;
&lt;/Policies&gt;
&lt;DefaultRolloverStrategy max=&quot;1&quot; /&gt;
&lt;/RollingFile&gt;
&lt;/Appenders&gt;
&lt;Loggers&gt;
&lt;Logger name=&quot;com.howtodoinjava.app&quot; additivity=&quot;false&quot;&gt;
&lt;AppenderRef ref=&quot;appLog&quot; /&gt;
&lt;AppenderRef ref=&quot;Console&quot; /&gt;
&lt;/Logger&gt;
&lt;Root level=&quot;debug&quot;&gt;
&lt;AppenderRef ref=&quot;Console&quot; /&gt;
&lt;/Root&gt;
&lt;/Loggers&gt;
&lt;/Configuration&gt;
public class BatchApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(BatchApplication.class, args);
//logger.info(&quot;Application started&quot;);
logger.debug(&quot;This is a debug message&quot;);
logger.info(&quot;This is an info message&quot;);
logger.warn(&quot;This is a warn message&quot;);
logger.error(&quot;This is an error message&quot;);
logger.fatal(&quot;This is a fatal message&quot;);
}
}
}
Pom.xml
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.1.4.RELEASE&lt;/version&gt;
&lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.shi&lt;/groupId&gt;
&lt;artifactId&gt;rkjobs&lt;/artifactId&gt;
&lt;version&gt;0.0.1&lt;/version&gt;
&lt;name&gt;batch&lt;/name&gt;
&lt;description&gt;drkjobs&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;11&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-batch&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-logging&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-log4j2&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-mail&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;mysql&lt;/groupId&gt;
&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.kafka&lt;/groupId&gt;
&lt;artifactId&gt;spring-kafka&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.batch&lt;/groupId&gt;
&lt;artifactId&gt;spring-batch-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;profiles&gt;
&lt;profile&gt;
&lt;id&gt;dev&lt;/id&gt;
&lt;activation&gt;
&lt;activeByDefault&gt;true&lt;/activeByDefault&gt;
&lt;/activation&gt;
&lt;properties&gt;
&lt;build.profile.id&gt;dev&lt;/build.profile.id&gt;
&lt;profileActive&gt;dev&lt;/profileActive&gt;
&lt;/properties&gt;
&lt;/profile&gt;
&lt;profile&gt;
&lt;id&gt;test&lt;/id&gt;
&lt;properties&gt;
&lt;build.profile.id&gt;test&lt;/build.profile.id&gt;
&lt;profileActive&gt;test&lt;/profileActive&gt;
&lt;/properties&gt;
&lt;/profile&gt;
&lt;profile&gt;
&lt;id&gt;uat&lt;/id&gt;
&lt;properties&gt;
&lt;build.profile.id&gt;uat&lt;/build.profile.id&gt;
&lt;profileActive&gt;uat&lt;/profileActive&gt;
&lt;/properties&gt;
&lt;/profile&gt;
&lt;profile&gt;
&lt;id&gt;local&lt;/id&gt;
&lt;properties&gt;
&lt;build.profile.id&gt;local&lt;/build.profile.id&gt;
&lt;profileActive&gt;local&lt;/profileActive&gt;
&lt;/properties&gt;
&lt;/profile&gt;
&lt;/profiles&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

spring log4j in XML configuration not able to create the file for log information.

the log message is able to print in the console. but In file not able to create in the required folder the printing log file there

the log message is able to print in the console. but In file not able to create in the required folder the printing log file there

答案1

得分: 1

你的logback.xml文件应该以这种格式编写:

<?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>
如果是Spring Boot应用程序,则应在application.properties文件中提及属性:
logging.level.org.springframework.web: INFO
logging.level.org.hibernate:TRACE
endpoints.shutdown.enabled=true
在需要包含日志的每个类中可以按以下方式定义Logger

private static final Logger logger = LoggerFactory.getLogger(demo.class);
英文:
Your logback.xml file should be in this format : 
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration&gt;
&lt;property name=&quot;DEV_HOME&quot; value=&quot;logs/demo.log&quot; /&gt;
&lt;appender name=&quot;FILE-AUDIT&quot;
class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;
&lt;file&gt;${DEV_HOME}/debug.log&lt;/file&gt;
&lt;encoder class=&quot;ch.qos.logback.classic.encoder.PatternLayoutEncoder&quot;&gt;
&lt;Pattern&gt;
%d{yyyy-MM-dd HH:mm:ss}- %t - %msg%n
&lt;/Pattern&gt;
&lt;/encoder&gt;
&lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.TimeBasedRollingPolicy&quot;&gt;
&lt;!-- rollover daily --&gt;
&lt;fileNamePattern&gt;${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
&lt;/fileNamePattern&gt;
&lt;timeBasedFileNamingAndTriggeringPolicy
class=&quot;ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP&quot;&gt;
&lt;maxFileSize&gt;100KB&lt;/maxFileSize&gt;
&lt;/timeBasedFileNamingAndTriggeringPolicy&gt;
&lt;/rollingPolicy&gt;
&lt;/appender&gt;
&lt;appender name=&quot;STDOUT&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
&lt;encoder&gt;
&lt;pattern&gt;%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -
%msg%n&lt;/pattern&gt;
&lt;/encoder&gt;
&lt;/appender&gt;
&lt;logger name=&quot;com.demo&quot; level=&quot;debug&quot; additivity=&quot;false&quot;&gt;
&lt;appender-ref ref=&quot;FILE-AUDIT&quot; /&gt;
&lt;/logger&gt;
&lt;root level=&quot;info&quot;&gt;
&lt;appender-ref ref=&quot;STDOUT&quot; /&gt;
&lt;/root&gt;
&lt;/configuration&gt;
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);

huangapple
  • 本文由 发表于 2020年3月16日 13:57:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/60700997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定