[英]AWS Elastic Beanstalk default CloudWatch log location for Java applications
I have deployed a JAR file in an AWS Elastic Beanstalk environment, using a load balancer and maximum 2 EC2 instances.我在 AWS Elastic Beanstalk 环境中部署了一个 JAR 文件,使用负载均衡器和最多 2 个 EC2 实例。
My environment seems to be using the new unified CloudWatch agent , not the legacy CloudWatch Logs Agent我的环境似乎正在使用新的统一 CloudWatch 代理,而不是旧版 CloudWatch Logs 代理
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:在控制台的环境配置中,我打开了“实例日志流式传输到 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
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
.值得注意的是 web 服务器访问在access.log
中,甚至我的 JAR 文件的 STDOUT 控制台 output 也在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.现在假设我希望我的 JAR 应用程序生成一个日志文件并在 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".我已经阅读了Elastic Beanstalk EC2 日志记录文档,其中说我可以在我的应用程序中捆绑特殊的.ebextensions
以指示新的日志记录位置“[i]如果您的应用程序在不属于您的默认配置的位置生成日志环境的平台”。
In the short term I'd rather not create an .ebextensions
file.在短期内,我宁愿不创建.ebextensions
文件。 The part about "… a location … part of the default configuration …" intrigues me.关于“……位置……默认配置的一部分……”的部分让我很感兴趣。
Is there a default location for the Elastic Beanstalk Java platform logs? Elastic Beanstalk Java 平台日志是否有默认位置? In other words, is there some default location such as /var/log/app/
that I can simply have my application log to (eg via environment variable configuration) and have CloudWatch Logs with the new unified agent automatically pick them up, without needing to create an .ebextensions
file?换句话说,是否有一些默认位置,例如/var/log/app/
我可以简单地让我的应用程序登录到(例如通过环境变量配置)并让 CloudWatch Logs 和新的统一代理自动获取它们,而不需要创建.ebextensions
文件? (As a second best solution, is there a way I can configure the location using the AWS console?) (作为第二好的解决方案,有没有一种方法可以使用 AWS 控制台配置位置?)
It is very easy to configure this in the new unified CloudWatch agent.在新的统一 CloudWatch 代理中配置它非常容易。 The agent picks up any configuration files present in the directory - /etc/amazon/amazon-cloudwatch-agent/amazon-cloudwatch-agent.d/
代理选取目录中存在的任何配置文件 - /etc/amazon/amazon-cloudwatch-agent/amazon-cloudwatch-agent.d/
You can create a custom config file in that directory,您可以在该目录中创建自定义配置文件,
{
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "path_to_file/app1.log",
"log_group_name": "/app/app.log",
"log_stream_name": "{instance_id}"
}
]
}
}
}
}
You can have multiple config files like this.您可以有多个这样的配置文件。 These logs will be available in CloudWatch Logs along with default log groups.这些日志将与默认日志组一起在 CloudWatch Logs 中提供。
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.注意 -查看 eb-engine.log,Elastic Beanstalk 在为每个部署运行来自prebuild
和预构建挂钩的commands
后重新设置了统一的 cloudwatch 代理,因此在这些步骤中创建的所有文件都将被删除。 So I suggest you use predeploy
hooks to configure this.所以我建议你使用predeploy
hooks来配置这个。
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.将挂钩文件存储在 - .platform/hooks/predeploy/cwa_custom_logs.sh
并确保该文件在 git 中可执行。
#!/bin/sh
filepath="/etc/amazon/amazon-cloudwatch-agent/amazon-cloudwatch-agent.d/custom_logs.json"
# Create the file
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
# Change file permissions
chmod 000755 $filepath
chown root:root $filepath
# Restart cloudwatch agent
systemctl restart amazon-cloudwatch-agent.service
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
:您在问题中提到的日志文件被硬编码到配置文件/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}"
}
]
}
}
}
}
The aws:elasticbeanstalk:cloudwatch:logs does not have any option to add extra log files. aws:elasticbeanstalk:cloudwatch:logs没有任何选项可以添加额外的日志文件。
It seems to me that you would have to customize the config files through extensions
config files.在我看来,您必须通过extensions
配置文件自定义配置文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.