【问题标题】:Logback could not find external property file resourceLogback 找不到外部属性文件资源
【发布时间】:2014-02-06 22:12:58
【问题描述】:

我正在使用 logback 版本 1.0.13。 为了从 logback 读取外部属性资源,我使用了 JNDI 条目(在 Tomcat 上下文中定义):

...
<insertFromJNDI env-entry-name="java:comp/env/app.config.path" as="app.config.path" /> 
<property resource="${app.config.path}" />
....

问题:logback 找不到我的 JNDI 资源:${app.config.path}=file:///D:/temp/application.properties

ERROR in ch.qos.logback.core.joran.action.PropertyAction - Could not find resource [file:///D:/temp/application.properties]

不适用于其他 URL,例如:file://D:/, file:/D:/, file:////D:/,...

有什么建议吗?

【问题讨论】:

    标签: jndi logback


    【解决方案1】:

    正如 Bidit Kumar 所指出的,文件方法也有效。就我而言,我遇到了类似的问题,我的解决方案如下(我应用了解决方案 2)

    解决方案:

    1. <property file="C:\myproject\src\main\resources\com\mycompany\app.properties"/>

    2. &lt;property resource="\com\mycompany\app.properties"/&gt;

    【讨论】:

    • 我认为他谈到了属性文件在项目之外的情况。不在 src/main/resources 文件夹中。我有一个 application.properties 在 tomcat_dir/conf 中的情况。完全困惑如何访问该文件。
    【解决方案2】:

    设置:

    语言:Java

    应用程序: Spring Boot 应用程序

    项目: Maven

    日志记录: Logback

    容器:(开发用 Jetty | 生产用 Tomcat)


    情况:

    在我们的项目中,logback.xml 位于资源的根目录。该文件需要访问特定的应用程序属性。在下面的示例中,我们需要访问的属性是logging.home.dir,它将被存储为LOGGER_HOME

    示例:logback.xml

    <configuration> 
      <property name="LOGGER_HOME" value="${logging.home.dir}" />
    
      <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGGER_HOME}/application.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
          <!-- other setting --> 
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
          <!-- other setting --> 
        </encoder>
      </appender>
    </configuration>
    

    问题:

    通常对于开发,可以使用默认应用程序属性文件application.properties 来保存所有必需的属性,但对于生产,此文件是外部化的。

    发展

    application.properties
    logging.home.dir=temp/logs/
    
    • 对于开发,只要 application.proerties 文件在类路径中可用,就可以使用 &lt;property resource="application.properties" /&gt; 在 logback.xml 中引用它

    生产

    Location: /opt/production/tc8-java8/my_wonderfull_app/application-prod.properties
    application-prod.properties
    logging.home.dir=application/logs/
    
    • 此文件位于应用程序外部。不幸的是,由于该文件不在类路径中,我们无法像在开发中那样加载该文件。

    解决方案:

    要允许 logback 使用外部属性文件,您需要执行以下操作:

    第 1 步: 启动应用程序时,您需要添加一个 VM 选项,指示加载外部属性文件的位置: -Dmy.wonderful.app.external.properties.file=/opt/production/tc8-java8/my_wonderfull_app/application-prod.properties

    第 2 步:您现在可以在 logback 文件中使用 &lt;property file="${my.wonderful.app.external.properties.file}" /&gt; &lt;!-- note this MUST match the VM option name --&gt;

    &lt;property name="LOGGER_HOME" value="${logging.home.dir}" /&gt; &lt;!-- property key from the file --&gt;


    简答:

    应用虚拟机选项

    -Dmy.wonderful.app.external.properties.file=/opt/production/tc8-java8/my_wonderfull_app/application-prod.properties

    Logback.xml

    my.wonderful.app.external.properties.file}" />


    注意 1: 对于 Intellij,可以通过 Run/Debug Configuration > Spring Boot > [Configuration > Environment > ‘VM options:’] 添加 VM 选项

    注意 2: 对于 Tomcat,可以在 /bin/setenv.sh 中添加 VM 选项
    CATALINA_OPTS=-Dmy.wonderful.app.external.properties.file=/opt/production/tc8-java8/my_wonderfull_app/application-prod.properties

    【讨论】:

      【解决方案3】:

      尝试使用 file="${app.config.path}" 而不是 resource="${app.config.path}"

      和 ${app.config.path}= D:/...

      查看 logback 文档了解更多详情:http://logback.qos.ch/manual/configuration.html

      【讨论】:

        【解决方案4】:

        罗德里戈

        这是我的 config.properties 文件:

        logging.path = ./
        logging.file = sync.log
        logging.maxHistory = 5
        logging.app.level = INFO
        logging.libs.level = DEBUG
        

        还有我的 logback.xml 文件:

        <?xml version="1.0" encoding="UTF-8"?>
        <configuration>
            <property file="config.properties" />
        
            <!-- Console -->
            <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
                <encoder>
                     <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{60} - %msg%n</pattern>
                </encoder>
            </appender>
        
            <!-- File -->
            <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
                <file>${logging.path}${logging.file}</file>
                    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                        <fileNamePattern>${logging.path}${logging.file}-%d{yyyy-MM-dd}</fileNamePattern>
                        <maxHistory>${logging.maxHistory}</maxHistory>
                    </rollingPolicy>
                   <encoder>
                       <pattern>%date %level [%thread] %logger{160} [%file:%line] %msg%n</pattern>
                   </encoder>
            </appender>
        
            <logger name="com.amazon" level="${logging.libs.level}"/>
            <logger name="com.amazonaws" level="${logging.libs.level}"/>
            <logger name="org.apache.http" level="${logging.libs.level}"/>
            <logger name="com.zaxxer.hikari" level="${logging.libs.level}"/>
            <logger name="org.flywaydb" level="${logging.libs.level}"/>
        
            <root level="${logging.app.level}">
                <appender-ref ref="FILE" />
                <appender-ref ref="CONSOLE" />
            </root>
        
        </configuration>
        

        【讨论】:

          【解决方案5】:

          SpringApplication 从以下位置的 application.properties 文件中加载属性并将它们添加到 Spring 环境中:

          1. 当前目录的 /config 子目录
          2. 当前目录
          3. 一个类路径 /config 包
          4. 类路径根

          如果你不喜欢application.properties(你希望dev、stage、production环境不同的情况)作为配置文件名,你可以通过指定spring.config.name环境来切换到另一个文件名财产。您还可以使用 spring.config.location 环境属性来引用显式位置

          您可以尝试以下(Eclipse),

          在运行配置中使用适当的值设置环境变量 SPRING_CONFIG_LOCATION 和 SPRING_CONFIG_NAME。

          在 logback.xml 中,添加以下属性

          <property
                  file="${SPRING_CONFIG_LOCATION}${SPRING_CONFIG_NAME}.properties" />
          

          现在您可以访问属性文件中的属性(例如 variablename.xyz),如下所示,

          <property name="MY_PROPERTY" value="${variablename.xyz}" />
          

          【讨论】:

            猜你喜欢
            • 2014-03-16
            • 2014-03-24
            • 1970-01-01
            • 2019-01-22
            • 2018-09-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多