升级的laravel从5.4到5.6。 Laravel从5.6版开始删除了$ app-> configureMonologUsing
aws的教程不再适用。 https://aws.amazon.com/tw/blogs/developer/php-application-logging-with-amazon-cloudwatch-logs-and-monolog/
任何人都可以告诉我在$ app-> configureMonologUsing中迁移逻辑的位置?
感谢
答案 0 :(得分:7)
使用以下命令安装最新版本的CloudWatch处理程序库:
composer require maxbanton/cwh
您可以在custom
中添加一个config/logging.php
频道,例如:
'cloudwatch' => [
'driver' => 'custom',
'via' => \App\Logging\CloudWatchLoggerFactory::class,
'sdk' => [
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'version' => 'latest',
'credentials' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY')
]
],
'retention' => env('CLOUDWATCH_LOG_RETENTION',7),
'level' => env('CLOUDWATCH_LOG_LEVEL','error')
],
和工厂类App/Logging/CloudWatchLoggerFactory.php
如下:
<?php
namespace App\Logging;
use Aws\CloudWatchLogs\CloudWatchLogsClient;
use Maxbanton\Cwh\Handler\CloudWatch;
use Monolog\Logger;
class CloudWatchLoggerFactory
{
/**
* Create a custom Monolog instance.
*
* @param array $config
* @return \Monolog\Logger
*/
public function __invoke(array $config)
{
$sdkParams = $config["sdk"];
$tags = $config["tags"] ?? [ ];
$name = $config["name"] ?? 'cloudwatch';
// Instantiate AWS SDK CloudWatch Logs Client
$client = new CloudWatchLogsClient($sdkParams);
// Log group name, will be created if none
$groupName = config('app.name') . '-' . config('app.env');
// Log stream name, will be created if none
$streamName = config('app.hostname');
// Days to keep logs, 14 by default. Set to `null` to allow indefinite retention.
$retentionDays = $config["retention"];
// Instantiate handler (tags are optional)
$handler = new CloudWatch($client, $groupName, $streamName, $retentionDays, 10000, $tags);
// Create a log channel
$logger = new Logger($name);
// Set handler
$logger->pushHandler($handler);
return $logger;
}
}
答案 1 :(得分:1)
如果您在ECS上使用Laravel。
在.env
文件中添加此LOG_CHANNEL=stderr
它将自动登录到CloudWatch。
答案 2 :(得分:1)
如果您在AWS EC2实例上运行并记录大量信息/调试消息,则实时发送日志会减慢您的应用程序响应时间。相反,您可以让CloudWatch代理查看您的laravel.log,以每5秒发送一次新的日志条目。 CloudWatch代理可以运送您的所有系统日志和任何系统指标(例如CPU),以便您创建云监视警报。
答案 3 :(得分:1)
我想分享一下我为Laravel和Nginx日志实现Cloudwatch的经验。本指南将与使用Elastic Beanstalk的Multicontainer Docker环境相关。我的方法更多地与流日志相关,而不是手动添加日志。
以下是步骤:
1。
添加.ebextensions/1_cloudwatch.config
文件,其内容如下:
option_settings:
- namespace: aws:elasticbeanstalk:cloudwatch:logs
option_name: StreamLogs
value: true
这将打开默认实例日志流(根据docs)。
2。
添加.ebextensions/2_logs_streamtocloudwatch_linux.config
文件,其内容如下:
###################################################################################################
#### The following file installs and configures the AWS CloudWatch Logs agent to push logs to a Log
#### Group in CloudWatch Logs.
####
#### The configuration below sets the logs to be pushed, the Log Group name to push the logs to and
#### the Log Stream name as the instance id. The following files are examples of logs that will be
#### streamed to CloudWatch Logs in near real time:
####
####
#### You can then access the CloudWatch Logs by accessing the AWS CloudWatch Console and clicking
#### the "Logs" link on the left. The Log Group name will follow this format:
####
#### /aws/elasticbeanstalk/<environment name>/<full log name path>
####
#### Please note this configuration can be used additionally to the "Log Streaming" feature:
#### http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/AWSHowTo.cloudwatchlogs.html
###################################################################################################
packages:
yum:
awslogs: []
files:
"/etc/awslogs/awscli.conf" :
mode: "000600"
owner: root
group: root
content: |
[plugins]
cwlogs = cwlogs
[default]
region = `{"Ref":"AWS::Region"}`
"/etc/awslogs/awslogs.conf" :
mode: "000600"
owner: root
group: root
content: |
[general]
state_file = /var/lib/awslogs/agent-state
"/etc/awslogs/config/logs.conf" :
mode: "000600"
owner: root
group: root
content: |
[/var/log/nginx/error]
log_group_name = `{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/nginx-error"]]}`
log_stream_name = logs
timestamp_format = '[%d/%b/%Y:%H:%M:%S %z]'
file = /var/log/nginx/error.log
buffer_duration = 5000
use_gzip_http_content_encoding = true
[/var/log/nginx/access]
log_group_name = `{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/nginx-access"]]}`
log_stream_name = logs
timestamp_format = '[%d/%b/%Y:%H:%M:%S %z]'
file = /var/log/nginx/access.log
buffer_duration = 5000
use_gzip_http_content_encoding = true
[/var/log/nginx/laravel]
datetime_format = %Y-%m-%d %H:%M:%S
log_group_name = `{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/laravel"]]}`
log_stream_name = logs
timestamp_format = '[%Y-%m-%d %H:%M:%S]'
file = /var/log/laravel/laravel-*.log
buffer_duration = 5000
use_gzip_http_content_encoding = true
multi_line_start_pattern = {datetime_format}
commands:
"01":
command: chkconfig awslogs on
"02":
command: service awslogs restart
该脚本由Amazon提供,这是源文件-https://github.com/awsdocs/elastic-beanstalk-samples/blob/master/configuration-files/aws-provided/instance-configuration/logs-streamtocloudwatch-linux.config
这里最有趣的部分是这个:
[/var/log/laravel]
datetime_format = %Y-%m-%d %H:%M:%S
log_group_name = `{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/laravel"]]}`
log_stream_name = logs
timestamp_format = '[%Y-%m-%d %H:%M:%S]'
file = /var/log/laravel/laravel-*.log
buffer_duration = 5000
use_gzip_http_content_encoding = true
multi_line_start_pattern = {datetime_format}
这些是我们用于Laravel日志记录的参数。这里没什么特别的。
production-laravel
您可以在CloudWatch Agent参考-https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AgentReference.html
中了解有关这些参数的更多信息。3。
我们需要更新Dockerrun.aws.json
。
在您的volumes
部分,您需要添加以下项目:
"volumes": [
...
{
"name": "awseb-logs-nginx-proxy",
"host": {
"sourcePath": "/var/log/nginx"
}
},
{
"name": "laravel-logs",
"host": {
"sourcePath": "/var/log/laravel"
}
}
...
]
第二,我们需要更新containerDefinitions
部分,并为所需的容器添加mountPoints
,例如:
"containerDefinitions": [
...
{
"name": "laravel-app",
"image": "laravel-image",
"hostname": "laravel",
"essential": true,
"memory": 256,
"mountPoints": [
{
"sourceVolume": "laravel-logs",
"containerPath": "/var/www/storage/logs"
}
]
},
{
"name": "nginx",
"image": "nginx-image",
"hostname": "nginx",
"essential": true,
"memory": 128,
"volumesFrom": [
{
"sourceContainer": "app"
}
],
"portMappings": [
{
"hostPort": 80,
"containerPort": 80
}
],
"mountPoints": [
{
"sourceVolume": "awseb-logs-nginx-proxy",
"containerPath": "/var/log/nginx"
}
],
"links": ["laravel-app"]
}
...
]
结果,您的Dockerrun.aws.json
文件可能看起来像这样:
{
"AWSEBDockerrunVersion": 2,
"volumes": [
{
"name": "awseb-logs-nginx-proxy",
"host": {
"sourcePath": "/var/log/nginx"
}
},
{
"name": "laravel-logs",
"host": {
"sourcePath": "/var/log/laravel"
}
}
],
"containerDefinitions": [
{
"name": "laravel-app",
"image": "laravel-image",
"hostname": "laravel",
"essential": true,
"memory": 256,
"mountPoints": [
{
"sourceVolume": "laravel-logs",
"containerPath": "/var/www/storage/logs"
}
]
},
{
"name": "nginx",
"image": "nginx-image",
"hostname": "nginx",
"essential": true,
"memory": 128,
"volumesFrom": [
{
"sourceContainer": "app"
}
],
"portMappings": [
{
"hostPort": 80,
"containerPort": 80
}
],
"mountPoints": [
{
"sourceVolume": "awseb-logs-nginx-proxy",
"containerPath": "/var/log/nginx"
}
],
"links": ["laravel-app"]
}
]
}
4。
您需要检查是否允许您的IAM实例配置文件角色将日志添加到CloudWatch。默认情况下,角色的名称为aws-elasticbeanstalk-ec2-role
,但是您可以在环境的“配置”页面的“安全性”部分下对其进行检查。
这是您需要添加的策略(source):
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams"
],
"Resource": [
"arn:aws:logs:*:*:*"
]
}
]
}
或者您也可以添加CloudWatchLogsFullAccess
策略以用于测试。
结果,您将收到以下日志组: