【问题标题】:How to log messages in to a file when deployed on remote server部署在远程服务器上时如何将消息记录到文件中
【发布时间】:2017-03-07 15:34:42
【问题描述】:

在我的 spring boot 项目中,我在本地运行项目时使用了 logback.xml,它会将消息记录到指定的文件中,但是当我部署在远程服务器上时,它仍在写入 tomcat 中的 catalina.out 文件。 这是我的代码。 logback.xml

<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- Log message format -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <!-- Setting the root level of logging to INFO -->
  <root level="info">
    <appender-ref ref="FILE" />
  </root>
</configuration>

我在 pom.xml 文件中指定了这些依赖项。

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>   
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-tomcat</artifactId>
             <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

在 application.properties 文件中我指定了日志文件属性。

logging.file=var/log/sampleproject_logging.log

在 application-dev.properties 文件中,我为远程服务器指定了日志文件。

spring.profiles.active = dev

#logging info
#logging.file=var/log/sampleproject/sampleproject_logging.log

当我在本地运行时,它会在项目结构下创建文件夹并记录消息。当部署在远程服务器上时,它仍在登录 catalina.out 文件。如何写入远程服务器上的指定日志文件?我试过 logback-spring.xml 和 logging.config 属性对我没有用。

【问题讨论】:

    标签: java spring logging spring-boot logback


    【解决方案1】:

    您的 logback.xml 中缺少“FILE”附加程序定义

    添加类似的东西

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>yourlogName.log</file>
        <encoder>
            <pattern>%d{yyyy-MM-dd  HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    

    【讨论】:

    • 我也试过这个。我创建了 logback-local.xml 和 logback-dev.xml 并添加了这个 var/log/sampleproject_logging.log%date %level [%thread] %logger{10} [%file:%line] %msg%n
    • 我创建了 application-dev.proper 文件并指定了 logging.config=classpath:logback-dev.xml。当我在本地运行时,它同时显示在控制台和文件中。
    • 但是在远程它没有写入指定的文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    相关资源
    最近更新 更多