简体   繁体   English

如何将 logback 文件路径概括为 windows/linux 操作系统?

[英]How to generalize logback files paths to both windows/linux OS?

I have a java app.我有一个 java 应用程序。 My development enviourment is a Windows OS and my production OS is linux.我的开发环境是 Windows 操作系统,我的生产操作系统是 linux。 The app needs to read/write some files in the OS, among these - log files (I'm using logback lib).该应用程序需要读取/写入操作系统中的一些文件,其中包括日志文件(我正在使用 logback lib)。 In my windows dev enviourment, I configured the log path to be at absolute position: C://logger/my-app.log在我的 windows 开发环境中,我将日志路径配置为绝对 position:C://logger/my-app.log

Following is the relevant logback.xml config file code:以下是相关的 logback.xml 配置文件代码:

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>C://logger/my-app.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>C://logger/my-app-%d{yyyy-MM-dd}.log</fileNamePattern>
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
        <charset>UTF-8</charset>
        <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

But, as mintioned, my prod enviourment is a linux OS.但是,正如所提到的,我的产品环境是 linux 操作系统。 I want to create the file at absolute path also in the linux machine, with a single config xml file.我也想在 linux 机器中创建绝对路径的文件,使用单个配置 xml 文件。 I don't want the file to be created in the context of the deployed jar file but in some absolute path.我不希望在已部署的 jar 文件的上下文中创建该文件,而是在某个绝对路径中创建该文件。 How can it be done?怎么做到呢?

To make the web application portable and the log file should be generated irrespective of any OS.为了使 web 应用程序具有可移植性,并且无论任何操作系统都应生成日志文件。 You have to use user.home environment variable property.您必须使用user.home环境变量属性。 I provide below the logback snippet which will work on both Windows and Linux.我在 logback 片段下方提供了该片段,它适用于 Windows 和 Linux。

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${user.home}/logs/my-app.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>C://logger/my-app-%d{yyyy-MM-dd}.log</fileNamePattern>
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
        <charset>UTF-8</charset>
        <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM