AWS弹性Beanstalk Java应用程序的默认CloudWatch日志位置

huangapple go评论85阅读模式
英文:

AWS Elastic Beanstalk default CloudWatch log location for Java applications

问题

我已在AWS Elastic Beanstalk环境中部署了一个JAR文件,使用负载均衡器和最多2个EC2实例。

我的环境似乎正在使用新的统一CloudWatch代理,而不是传统的CloudWatch日志代理。

在控制台中的环境配置中,我打开了“将实例日志流式传输到CloudWatch Logs”的选项,现在在CloudWatch Logs中,我获得了以下日志组:

  • /aws/elasticbeanstalk/myapp-myenv/var/log/eb-engine.log
  • /aws/elasticbeanstalk/myapp-myenv/var/log/eb-hooks.log
  • /aws/elasticbeanstalk/myapp-myenv/var/log/nginx/access.log
  • /aws/elasticbeanstalk/myapp-myenv/var/log/nginx/error.log
  • /aws/elasticbeanstalk/myapp-myenv/var/log/web.stdout.log

这很好。特别要注意的是Web服务器访问记录位于access.log中,甚至我的JAR文件的标准输出(STDOUT)控制台输出位于web.stdout.log中。到目前为止都还不错。

现在假设我希望我的JAR应用程序生成一个日志文件,并希望CloudWatch Logs自动捕获它。我阅读了弹性Beanstalk EC2日志记录文档,其中提到我可以在我的应用程序中打包特殊的.ebextensions文件,以指示新的日志记录位置:“如果您的应用程序在不是默认配置的位置生成日志,用于您的环境平台”。

短期内,我宁愿不创建.ebextensions文件。关于“…默认配置的位置…部分”的部分引起了我的兴趣。

**弹性Beanstalk Java平台日志是否有默认位置?**换句话说,是否有一些默认位置,比如/var/log/app/,我可以简单地让我的应用程序记录到(例如通过环境变量配置),并且新的统一代理CloudWatch Logs会自动捕获它们,而无需创建.ebextensions文件?(作为次优解,我是否可以通过AWS控制台配置位置?)

英文:

I have deployed a JAR file in an AWS Elastic Beanstalk environment, using a load balancer and maximum 2 EC2 instances.

My environment seems to be using the new unified CloudWatch agent, not the legacy CloudWatch Logs Agent

In the environment configuration in the console I turned on "Instance log streaming to CloudWatch Logs", and now in CloudWatch Logs I get the following groups:

  • /aws/elasticbeanstalk/myapp-myenv/var/log/eb-engine.log
  • /aws/elasticbeanstalk/myapp-myenv/var/log/eb-hooks.log
  • /aws/elasticbeanstalk/myapp-myenv/var/log/nginx/access.log
  • /aws/elasticbeanstalk/myapp-myenv/var/log/nginx/error.log
  • /aws/elasticbeanstalk/myapp-myenv/var/log/web.stdout.log

This is nice. Notably the web server accesses are in access.log, and even the STDOUT console output of my JAR file is in web.stdout.log. So far so good.

Now let's say I want my JAR application to generate a log file and have it picked up automatically in CloudWatch Logs. I've read the Elastic Beanstalk EC2 logging documentation which says that I can bundle special .ebextensions in my application to indicate new logging locations "[i]f your application generates logs in a location that isn't part of the default configuration for your environment's platform".

In the short term I'd rather not create an .ebextensions file. The part about "… a location … part of the default configuration …" intrigues me.

Is there a default location for the Elastic Beanstalk Java platform logs? In other words, is there some default location such as /var/log/app/ that I can simply have my application log to (e.g. via environment variable configuration) and have CloudWatch Logs with the new unified agent automatically pick them up, without needing to create an .ebextensions file? (As a second best solution, is there a way I can configure the location using the AWS console?)

答案1

得分: 4

以下是翻译好的部分:

在新的统一 CloudWatch 代理中,配置非常容易。代理会获取目录中存在的任何配置文件 - /etc/amazon/amazon-cloudwatch-agent/amazon-cloudwatch-agent.d/

您可以在该目录中创建自定义配置文件,

{
    "logs": {
        "logs_collected": {
            "files": {
                "collect_list": [
                    {
                        "file_path": "path_to_file/app1.log",
                        "log_group_name": "/app/app.log",
                        "log_stream_name": "{instance_id}"
                    }
                ]
            }
        }
    }
}

您可以像这样拥有多个配置文件。这些日志将与默认日志组一起在 CloudWatch 日志中可用。

注意 - 查看 eb-engine.log,Elastic Beanstalk 在运行来自 .ebextension 的 commandsprebuild 钩子之后,会重新设置统一的 CloudWatch 代理,因此在这些步骤中创建的任何文件都将被删除。因此,我建议您使用 predeploy 钩子来配置此项。

用于执行此操作的示例钩子 -

将此钩子文件存储在 - .platform/hooks/predeploy/cwa_custom_logs.sh
并确保该文件在 Git 中可执行。

#!/bin/sh

filepath="/etc/amazon/amazon-cloudwatch-agent/amazon-cloudwatch-agent.d/custom_logs.json"

# 创建文件
cat > $filepath << 'EOL'
{
    "logs": {
        "logs_collected": {
            "files": {
                "collect_list": [
                    {
                        "file_path": "path_to_file/app1.log",
                        "log_group_name": "/app/app.log",
                        "log_stream_name": "{instance_id}"
                    }
                ]
            }
        }
    }
}
EOL

# 更改文件权限
chmod 000755 $filepath
chown root:root $filepath

# 重新启动 CloudWatch 代理
systemctl restart amazon-cloudwatch-agent.service
英文:

It is very easy to configure this in the new unified CloudWatch agent. The agent picks up any configuration files present in the directory - /etc/amazon/amazon-cloudwatch-agent/amazon-cloudwatch-agent.d/

You can create a custom config file in that directory,

{
    &quot;logs&quot;: {
        &quot;logs_collected&quot;: {
            &quot;files&quot;: {
                &quot;collect_list&quot;: [
                    {
                        &quot;file_path&quot;: &quot;path_to_file/app1.log&quot;,
                        &quot;log_group_name&quot;: &quot;/app/app.log&quot;,
                        &quot;log_stream_name&quot;: &quot;{instance_id}&quot;
                    }
                ]
            }
        }
    }
}

You can have multiple config files like this. These logs will be available in CloudWatch Logs along with default log groups.

Note - Looking at eb-engine.log, Elastic Beanstalk freshly sets up the unified cloudwatch agent after running commands from .ebextension and prebuild hooks for every deplyment, so any files created in those steps are being deleted. So I suggest you use predeploy hooks to configure this.

A sample hook to do this -

Store the hook file at - .platform/hooks/predeploy/cwa_custom_logs.sh
and make sure the file is executable in git.

#!/bin/sh

filepath=&quot;/etc/amazon/amazon-cloudwatch-agent/amazon-cloudwatch-agent.d/custom_logs.json&quot;

# Create the file
cat &gt; $filepath &lt;&lt; &#39;EOL&#39;
{
    &quot;logs&quot;: {
        &quot;logs_collected&quot;: {
            &quot;files&quot;: {
                &quot;collect_list&quot;: [
                    {
                        &quot;file_path&quot;: &quot;path_to_file/app1.log&quot;,
                        &quot;log_group_name&quot;: &quot;/app/app.log&quot;,
                        &quot;log_stream_name&quot;: &quot;{instance_id}&quot;
                    }
                ]
            }
        }
    }
}
EOL

# Change file permissions
chmod 000755 $filepath
chown root:root $filepath

# Restart cloudwatch agent
systemctl restart amazon-cloudwatch-agent.service

答案2

得分: 0

我不这么认为。你在问题中提到的日志文件是硬编码到配置文件 /opt/aws/amazon-cloudwatch-agent/etc/beanstalk.json 中的:

{
        "logs": {
                "logs_collected": {
                        "files": {
                                "collect_list": [
                                        {
                                                "file_path": "/var/log/eb-engine.log",
                                                "log_group_name": "/aws/elasticbeanstalk/A03cBeanstalkApplicationMybeanstalka-env/var/log/eb-engine.log",
                                                "log_stream_name": "{instance_id}"
                                        },
                                        {
                                                "file_path": "/var/log/eb-hooks.log",
                                                "log_group_name": "/aws/elasticbeanstalk/A03cBeanstalkApplicationMybeanstalka-env/var/log/eb-hooks.log",
                                                "log_stream_name": "{instance_id}"
                                        },
                                        {
                                                "file_path": "/var/log/nginx/access.log",
                                                "log_group_name": "/aws/elasticbeanstalk/A03cBeanstalkApplicationMybeanstalka-env/var/log/nginx/access.log",
                                                "log_stream_name": "{instance_id}"
                                        },
                                        {
                                                "file_path": "/var/log/nginx/error.log",
                                                "log_group_name": "/aws/elasticbeanstalk/A03cBeanstalkApplicationMybeanstalka-env/var/log/nginx/error.log",
                                                "log_stream_name": "{instance_id}"
                                        },
                                        {
                                                "file_path": "/var/log/web.stdout.log",
                                                "log_group_name": "/aws/elasticbeanstalk/A03cBeanstalkApplicationMybeanstalka-env/var/log/web.stdout.log",
                                                "log_stream_name": "{instance_id}"
                                        }
                                ]
                        }
                }
        }
}

aws:elasticbeanstalk:cloudwatch:logs 并没有任何选项可以添加额外的日志文件。

在我看来,你可能需要通过 extensions 配置文件来自定义配置文件。

英文:

I don't think so. The log files you mentioned in the question are hard coded into config file /opt/aws/amazon-cloudwatch-agent/etc/beanstalk.json:

{
        &quot;logs&quot;: {
                &quot;logs_collected&quot;: {
                        &quot;files&quot;: {
                                &quot;collect_list&quot;: [
                                        {
                                                &quot;file_path&quot;: &quot;/var/log/eb-engine.log&quot;,
                                                &quot;log_group_name&quot;: &quot;/aws/elasticbeanstalk/A03cBeanstalkApplicationMybeanstalka-env/var/log/eb-engine.log&quot;,
                                                &quot;log_stream_name&quot;: &quot;{instance_id}&quot;
                                        },
                                        {
                                                &quot;file_path&quot;: &quot;/var/log/eb-hooks.log&quot;,
                                                &quot;log_group_name&quot;: &quot;/aws/elasticbeanstalk/A03cBeanstalkApplicationMybeanstalka-env/var/log/eb-hooks.log&quot;,
                                                &quot;log_stream_name&quot;: &quot;{instance_id}&quot;
                                        },
                                        {
                                                &quot;file_path&quot;: &quot;/var/log/nginx/access.log&quot;,
                                                &quot;log_group_name&quot;: &quot;/aws/elasticbeanstalk/A03cBeanstalkApplicationMybeanstalka-env/var/log/nginx/access.log&quot;,
                                                &quot;log_stream_name&quot;: &quot;{instance_id}&quot;
                                        },
                                        {
                                                &quot;file_path&quot;: &quot;/var/log/nginx/error.log&quot;,
                                                &quot;log_group_name&quot;: &quot;/aws/elasticbeanstalk/A03cBeanstalkApplicationMybeanstalka-env/var/log/nginx/error.log&quot;,
                                                &quot;log_stream_name&quot;: &quot;{instance_id}&quot;
                                        },
                                        {
                                                &quot;file_path&quot;: &quot;/var/log/web.stdout.log&quot;,
                                                &quot;log_group_name&quot;: &quot;/aws/elasticbeanstalk/A03cBeanstalkApplicationMybeanstalka-env/var/log/web.stdout.log&quot;,
                                                &quot;log_stream_name&quot;: &quot;{instance_id}&quot;
                                        }
                                ]
                        }
                }
        }
}

The aws:elasticbeanstalk:cloudwatch:logs does not have any option to add extra log files.

It seems to me that you would have to customize the config files through extensions config files.

huangapple
  • 本文由 发表于 2020年9月4日 10:58:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63734196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定