English 中文(简体)
SpringBootApplication withlog4j2在开端犯错误
原标题:SpringBootApplication with log4j2 throws error on start

我把我的原木4j2.yml与泉水boot的特性混为一谈:

Properties:
    property:
      - name: LOGSTASH_HOST
        value: ${spring:logstash.server.host}
      - name: LOGSTASH_PORT
        value: ${spring:logstash.server.port}
      - name: APPLICATION_NAME
        value: ${lower:${spring:spring.application.name:-spring}}

If I start my application I get a bunch of error messages saying:

ERROR StatusConsoleListener Resolver failed to lookup spring:logstash.server.host java.lang.IllegalStateException: Unable to obtain Spring Environment from LoggerContext

以及

ERROR StatusConsoleListener Unable to write to stream TCP:localhost:9800 for appender LOGSTASH org.apache.logging.log4j.core.appender.AppenderLoggingException: Error writing to TCP:localhost:9800: socket not available at org.apache.logging.log4j.core.net.TcpSocketManager.write(TcpSocketManager.java:214)

It can not find the serverhost until springboot starts which takes a few seconds, after that the logging works fine 以及 no more error messages.

How could I configure my log4J2 to start after or when springboot starts 以及 don t spam error messages.

I ve试图排除 spring开端 logging的 logging木:

<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-log4j2</artifactId>
        </dependency>

or I ve tried fallback hosts 以及 ports:

Properties:
  property:
    - name: LOGSTASH_HOST
      value: "${sys:LOGSTASH_HOST:-${env:LOGSTASH_HOST:-localhost}}" # Prefer system property, then env variable, fallback to  localhost 
    - name: LOGSTASH_PORT
      value: "${sys:LOGSTASH_PORT:-${env:LOGSTASH_PORT:-5044}}" # Prefer system property, then env variable, fallback to  5044 
    - name: APPLICATION_NAME
      value: "${sys:spring.application.name:-${env:spring_application_name:-default-application}}" # System property, env variable, fallback
问题回答

Try renaming your log4j2.yml file to log4j2-spring.yml This is one possibility described in the documentation since 3.0.0: https://docs.spring.io/spring-boot/docs/3.2.4/reference/html/features.html#features.logging.log4j2-extensions

由于标准标识4j2.xml配置文件装上太早,你不能使用延期文件。 你们要么使用标志4j2-后代。 xml或界定伐木。 没收财产。





相关问题
WAF FILE could not EXPORT due to no WEB PROJECT option found

I was trying to create a war file on spring tool suite 4 (windows version). But as I was about to export the it to the local file system, I couldn t find the web project (wiseai-console) option of the ...

Combine multiple cb.literal() into 1 Expression

I am working on a Spring Boot project that uses PostgreSQL. I want to create a advanced search feature where user can search through the jsonb column that I have in many of my tables. Somewhere in my ...

Error using criteriaBuilder.max() for LocalDate

I am trying to get max date using criteria builder in a subquery. But I am getting this error Required type: Expression Provided: Expression Subquery<LocalDate> subRoot = criteriaQuery....

热门标签