减少AWS EC2的日志旋转频率[英] Reducing log rotation frequency for AWS EC2

本文是小编为大家收集整理的关于减少AWS EC2的日志旋转频率的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个Java应用程序在Elastic Beanstalk上运行,并将日志旋转到S3设置并正常工作.但是,我发现我的catalina.out日志文件只有长达15分钟的时间才能滚动到新文件,这使得在同一天更加乏味(从S3和UNZIP获取GZ,而不仅仅是SSH-ing和ssh-ing和读取日志文件).

有什么方法可以通过tomcat/log4j或Elastic-Beanstalk控制台进行配置?我相信这是一个AWS,因为我们在dotcloud上部署了相同的应用程序,并且每天保留一个日志文件.

推荐答案

截至今天,并非真的.您唯一能做的就是修改file/etc/logrotate.conf.conf.conf.elasticbeanstalk在您的应用程序正在运行的EC2实例中.

更改日志的大小,应该将更多日志信息放在一个文件中:

/var/log/tomcat6/catalina.out /var/log/tomcat6/monitor_catalina.log /var/log/tomcat6/tail_catalina.log {
    size 20M <-- change to the file size that you want
    missingok
    rotate 2
    compress
    notifempty
    copytruncate
    dateext
    dateformat -%s
    lastaction
        /bin/chown tomcat:elasticbeanstalk /var/log/tomcat6/*gz; /bin/chmod 664 /var/log/tomcat6/*gz
    endscript
}

还有另一篇文章在这里有更多信息:

使用弹性beanstalk /p>

其他推荐答案

即使这个问题已经旧且解决了,我希望以下内容能帮助更多像我这样挣扎整个概念的人.

日志旋转方式实际工作

我相信关于日志旋转实际工作方式的解释可能会更有帮助,因为技术细节肯定会随着时间而变化,并且在平台之间有所不同(在我的示例中,我使用PHP).

  1. Elastic Beanstalk日志旋转是通过logrotate完成的,并在/etc目录中以logrotate.elasticbeanstalk.hourly/logrotate.elasticbeanstalk.<service name>.conf的形式具有配置文件.从路径中可以看出,每小时表示应每小时旋转日志.请注意,此文件实际上不是 旋转日志,它只是一组logrotate日志特定的选项,在旋转实际执行时被执行.
  2. 旋转实际上是通过cron进行的,该服务允许您安排何时运行某些脚本.在这种情况下,如果您检查/etc/cron.hourly目录,您会看到cron.logrotate.elasticbeanstalk.<service name>.conf之类的文件(不要让.conf扩展欺骗您,这确实是可执行的,请注意顶部的#!bin/sh).当cron执行此脚本时,它将为指定的logrotate conf文件运行logrotate.
  3. 实际决定在什么时间cron运行小时作业的文件为/etc/cron.d/0hourly.决定在何时每天/每周/每月工作的时间为/etc/anacrontab.附带说明,我相信/etc/logrotate.d目录中的所有logrotate conf文件每天执行,如/etc/cron.daily/logrotate中指定.
  4. .

您的选项

使用此基本信息,您应该能够了解如何将旋转频率更改为喜好.例如:

  • 如果您只是在乎间隔,而不是特定的旋转时间,则可以将旋转脚本移至适当的/etc/cron.<interval>目录:

    mv /etc/cron.hourly/cron.logrotate.elasticbeanstalk.healthd.conf /etc/cron.daily/
    
  • 如果您想要更多的控件,请将脚本从cron目录中拉出,因此它不再以默认间隔执行:

    mv /etc/cron.hourly/cron.logrotate.elasticbeanstalk.healthd.conf /etc/
    

    然后,在/etc/cron.d目录中创建一个自定义cron作业,指定何时运行脚本.示例:

    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=""
    HOME=/
    05 3 * * * root /etc/cron.logrotate.elasticbeanstalk.healthd.conf
    

    每天每天上午3:05运行一次.

    注意:如果您决定创建自己的脚本,请确保它具有执行权限,否则cron将无法运行它.

对我来说,修改这些文件仅作为a ePloy平台挂钩.但是,如果您不更改默认的AWS日志旋转/CRON作业,而只是添加您自己的AWS,则可以在.ebextensions内的配置文件中安全地执行此操作.

本文地址:https://www.itbaoku.cn/post/1575031.html

问题描述

I have a java app running on elastic beanstalk, with the log rotation to S3 set up and working fine. However, I find that my catalina.out log file only exists for up to 15 minutes before rolling to a new file, which makes debugging problems on the same day more tedious (get gz from s3 and unzip, instead of just ssh-ing and reading the log file).

Is there any way to configure this through tomcat/log4j or the elastic-beanstalk console? I believe it's an AWS thing, because we had the same app deployed on Dotcloud and it kept a single log file per day.

推荐答案

As of Today, not really. The only thing that you can do is modify the file /etc/logrotate.conf.elasticbeanstalk in the EC2 instance where your app is running.

Change the size of the log and that should put more log info in one file:

/var/log/tomcat6/catalina.out /var/log/tomcat6/monitor_catalina.log /var/log/tomcat6/tail_catalina.log {
    size 20M <-- change to the file size that you want
    missingok
    rotate 2
    compress
    notifempty
    copytruncate
    dateext
    dateformat -%s
    lastaction
        /bin/chown tomcat:elasticbeanstalk /var/log/tomcat6/*gz; /bin/chmod 664 /var/log/tomcat6/*gz
    endscript
}

There's another post that has more info here:

Rotating S3 Logging using log4j with Elastic Beanstalk

其他推荐答案

Even though this question is old and resolved, I hope the following helps more people like me who struggled with the whole concept.

How log rotation actually works

I believe an explanation on how log rotation actually works might be more helpful, as the technical details will surely change over time and differ across platforms (in my examples I use php).

  1. Elastic Beanstalk log rotations are done through logrotate and have configuration files inside the /etc directory in the form of logrotate.elasticbeanstalk.hourly/logrotate.elasticbeanstalk.<service name>.conf. As you can tell from the path, hourly indicates that logs should be rotated hourly. Note that this file doesn't actually rotate the logs, it's just a set of logrotate log-specific options that get executed when the rotation is actually performed.
  2. Rotation actually happen through cron, a service that allows you to schedule when to run certain scripts. In this case, if you check the /etc/cron.hourly directory you will see files like cron.logrotate.elasticbeanstalk.<service name>.conf (don't let the .conf extension fool you, it's really an executable, note the #!bin/sh at the top). When cron executes this script, it will run logrotate for the specified logrotate conf file.
  3. The file that actually dictates at what time cron runs hourly jobs is /etc/cron.d/0hourly. The file that dictates at what time it runs daily/weekly/monthly jobs is /etc/anacrontab. As a side note, I believe that all logrotate conf files inside the /etc/logrotate.d directory get executed daily, as specified in /etc/cron.daily/logrotate.

Your options

With this basic info, you should be able to understand how to change the frequency of rotation to your liking. For instance:

  • If you just care about the interval and not the specific rotation time, you can simply move the rotation script to the appropriate /etc/cron.<interval> directory:

    mv /etc/cron.hourly/cron.logrotate.elasticbeanstalk.healthd.conf /etc/cron.daily/
    
  • If you want more control, pull the script out of the cron directory so it stops getting executed at default intervals:

    mv /etc/cron.hourly/cron.logrotate.elasticbeanstalk.healthd.conf /etc/
    

    Then, create a custom cron job inside the /etc/cron.d directory, specifying when to run the script. Example:

    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=""
    HOME=/
    05 3 * * * root /etc/cron.logrotate.elasticbeanstalk.healthd.conf
    

    which will run once a day, every day at 3:05am.

    Note: if you decide to create your own script, make sure it has execute permission, otherwise cron won't be able to run it.

For me, modifying these files only worked as a predeploy platform hook. However, if you're not changing the default AWS log rotations/cron jobs but just adding your own, you can safely do that in a config file inside .ebextensions.