AWS Elastic Beanstalk Application Logging with Logback

ぃ、小莉子 提交于 2020-12-16 07:01:41

问题


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.

Everything is working as expected EXCEPT application logs that I write with slf4j/logback.

The following is my logback.xml configuration file

<?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:

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. So I feel like I am missing something about logging to AWS with Logback.

I am also open to (working) suggestions about logging with other something else (log4j, java logging, etc).

EDIT: Finally figured out the aws/eb mess, and was able to ssh to the environment. I do not see java.log created under the folder var/log (or any other folder to the matter).

Why would logback not be writing the logs? Is that logpath /var/log/java.log not usable?


回答1:


This is working for me now, thanks to the accepted answer in Logback with Elastic Beanstalk, which I asked as well.

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:

(1) In logback.xml, update <file>/var/log/java.log</file> to <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)

The reason why it was not working in /var/log folder is because of the permissions to the folder. When I ssh to the folder, I found that the permissions to that folder is limited to root user only.

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. However, the user tomcat has permission to write to the /var/log/tomcat folder.

The another folder I found which the tomcat user has access to is the folder created for the app /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).

It took me a couple of days to finally figure this out, hope it will help others as well.

P.S. I took out the ConsoleAppender from my logback.xml, it was doing nothing.



来源:https://stackoverflow.com/questions/65017777/aws-elastic-beanstalk-application-logging-with-logback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!