简体   繁体   English

使用 Logback 的 AWS Elastic Beanstalk 应用程序日志记录

[英]AWS Elastic Beanstalk Application Logging with Logback

EDIT: Added Image (The problem looks like log files are not written)编辑:添加图像(问题看起来像未写入日志文件)

I am running an spring mvc (NOT springboot) WAR file on AWS Elastic Beanstalk on a 'Tomcat 8.5 with Corretto 11 running on 64bit Amazon Linux 2/4.1.3' environment.我在 AWS Elastic Beanstalk 上运行 spring mvc(不是 springboot)WAR 文件,该文件位于“Tomcat 8.5 和 Corretto 11 在 64 位 Amazon Linux 2/4.1.3”环境上运行。

Everything is working as expected EXCEPT application logs that I write with slf4j/logback.除了我用 slf4j/logback 编写的应用程序日志外,一切都按预期工作。

The following is my logback.xml configuration file以下是我的logback.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE xml>
<configuration debug="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                %d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <appender name="APPLOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/var/log/java.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>/var/log/java_%d{yyyy-MM-dd}_%i.log</FileNamePattern>
            <!-- keep 14 days' worth of history -->
            <maxHistory>14</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- or whenever the File size reaches 10MB -->
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} [%thread][%X{remoteHost}] %-5level %c{1} - %msg%n</pattern>
            <!-- <pattern>%d %-5p %c{1} - %m%n</pattern> -->
        </encoder>
    </appender>
    <logger name="com.personal.service.layer.mongodb" level="INFO"
        additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="APPLOG" />
    </logger>

    <logger name="org.springframework" level="INFO"
        additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="APPLOG" />
    </logger>

    <logger name="spring.web" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="APPLOG" />
    </logger>

    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="APPLOG" />
    </root>

</configuration>

After searching google/stackoverflow, I have added an ebextensions folder with a config file to include my logs with the tail logs and bundle as follows:搜索 google/stackoverflow 后,我添加了一个带有配置文件的 ebextensions 文件夹,以将我的日志包含在尾部日志和捆绑包中,如下所示:

files:
  "/opt/elasticbeanstalk/tasks/bundlelogs.d/applogs.conf" :
    mode: "000755"
    owner: root
    group: root
    content: |
      /var/log/*.log

  "/opt/elasticbeanstalk/tasks/taillogs.d/applogs.conf" :
    mode: "000755"
    owner: root
    group: root
    content: |
      /var/log/*.log

After some research online, I have tried some other combinations.经过网上的一些研究,我尝试了一些其他的组合。 For example, I have tried to set the log file in logback.xml as /var/app/current/logs/applog.log and in the conf files used /var/log/current/logs/*.log , instead of what is shown above, but they did not work either.例如,我尝试将 logback.xml 中的日志文件设置为/var/app/current/logs/applog.log并在使用的conf文件中/var/log/current/logs/*.log ,而不是什么如上所示,但它们也不起作用。 So I feel like I am missing something about logging to AWS with Logback.所以我觉得我错过了一些关于使用 Logback 登录到 AWS 的信息。

I am also open to (working) suggestions about logging with other something else (log4j, java logging, etc).我也愿意接受(工作)关于使用其他东西进行日志记录的建议(log4j、java 日志记录等)。

EDIT: Finally figured out the aws/eb mess, and was able to ssh to the environment.编辑:终于弄清楚了aws / eb混乱,并且能够 ssh 到环境中。 I do not see java.log created under the folder var/log (or any other folder to the matter).我没有看到在 var/log 文件夹(或任何其他相关文件夹)下创建的 java.log 。

Why would logback not be writing the logs?为什么 logback 不写日志? Is that logpath /var/log/java.log not usable?那个 logpath /var/log/java.log不可用吗?

在此处输入图像描述

This is working for me now, thanks to the accepted answer in Logback with Elastic Beanstalk , which I asked as well.这现在对我有用,这要感谢Logback with Elastic Beanstalk中接受的答案,我也问过这个答案。

I want combine what I learned from both questions here because this question is about reading logs where as that question is about writing logs, and it would be nice to have complete answer in one place.我想在这里结合我从这两个问题中学到的东西,因为这个问题是关于阅读日志的,而这个问题是关于写日志的,在一个地方有完整的答案会很好。

The following are the changes I made to the logback.xml and ebextensions config file as mentioned in the question:以下是我对问题中提到的 logback.xml 和 ebextensions 配置文件所做的更改:

(1) In logback.xml, update <file>/var/log/java.log</file> to <file>/var/log/tomcat/java.log</file> (1) 在logback.xml中,更新<file>/var/log/java.log</file><file>/var/log/tomcat/java.log</file>

(2) In the ebextensions config file, update /var/log/*.log to /var/log/tomcat/*.log (this is optional, it will work even if it is left as /var/log/*.log ) (2) 在 ebextensions 配置文件中,将/var/log/*.log更新为 /var/log/tomcat/*. /var/log/tomcat/*.log (这是可选的,即使保留为/var/log/*.log

The reason why it was not working in /var/log folder is because of the permissions to the folder.它在/var/log文件夹中不起作用的原因是因为该文件夹的权限。 When I ssh to the folder, I found that the permissions to that folder is limited to root user only.当我 ssh 到该文件夹时,我发现该文件夹的权限仅限于root用户。

在此处输入图像描述

However, when the app is running with Tomcat, it is running as the user tomcat which does not have permission to write to that folder.但是,当应用程序与 Tomcat 一起运行时,它以无权写入该文件夹的用户tomcat运行。 However, the user tomcat has permission to write to the /var/log/tomcat folder.但是,用户tomcat有权写入/var/log/tomcat文件夹。

在此处输入图像描述

The another folder I found which the tomcat user has access to is the folder created for the app /var/app .我发现tomcat用户可以访问的另一个文件夹是为应用程序/var/app创建的文件夹。 So if the log file location is set inside that folder in logback.xml, it would work as well (it would also require updating the ebextensions config file to point to that location).因此,如果日志文件位置设置在 logback.xml 中的该文件夹内,它也可以正常工作(还需要更新 ebextensions 配置文件以指向该位置)。

It took me a couple of days to finally figure this out, hope it will help others as well.我花了几天时间才终于弄清楚这一点,希望它也能对其他人有所帮助。

PS I took out the ConsoleAppender from my logback.xml, it was doing nothing. PS 我从 logback.xml 中取出ConsoleAppender ,它什么也没做。

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

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