有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Spring的TimeBasedRollingPolicy

这是logback。我正在使用的xml代码:

<configuration>
<property resource="C:\Code\pin-ws\pin-war\src\main\resources\META-INF\config\app.properties" scope="context" />
    <appender name="FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.location}\pin-app.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${log.location}\pin-app.%d{yyyy-MM-dd}.log
            </FileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>%date %level [%thread] %X{username} [%file:%line] %msg%n
            </pattern>
        </encoder>
    </appender>
    <root level="DEBUG">
        <appender-ref ref="FILE" />
    </root>
    </configuration>
  1. 我正在尝试从我的应用程序获取日志位置的值。财产。但它不起作用。有人能建议我应该使用什么属性标签吗
  2. 当我硬编码logback中的日志位置时。xml本身,我可以看到日志打印在我的日志文件中。 但即使在这种情况下,也只有pin应用程序。日志将附加值,但pin app.%d{yyyy-MM-dd}。未创建日志。 我使用基于时间的策略是否做错了什么
  3. 好奇的是,我们是否可以在控制台中从xml文件打印值(如本例中的${log.location})

我使用的是简单的Spring 4 MVC,而不是SpringBoot,下面是我使用的slf4j和logback的版本:

<version.slf4j>1.6.4</version.slf4j>
<version.logback>1.0.1</version.logback>
<java-version>1.8</java-version>

共 (1) 个答案

  1. # 1 楼答案

    <property>
    

    有选项“文件”和“资源

    示例

    <property resource="logging.properties"/> <!  Path relavtive to classpath. E.g. file in src/main/resources or within .jar when built  >
    

    或者

    <property file="logging.properties"/> <!  File in root folder of project or folder where your .jar is located  >
    

    对于您描述的情况(使用完全限定路径),它使用“文件””选项

    对于3。问题:是的,documentation声明“变量替换可以发生在配置文件中可以指定值的任何点。”

    示例

    属性文件:

    randomProperty=Hello
    

    回复。xml:

    <pattern>${randomProperty} %date %level [%thread] %X{username} [%file:%line] %msg%n</pattern>
    

    附加提示:

    <configuration debug="true">
    

    将向您展示初始化日志记录时的实际问题