简体   繁体   English

从 EC2 实例登录到 CloudWatch

[英]Logging to CloudWatch from EC2 instances

My EC2 servers are currently hosting a website that logs each registered user's activity under their own separate log file on the local EC2 instance, say username.log.我的 EC2 服务器目前正在托管一个网站,该网站将每个注册用户的活动记录在本地 EC2 实例上他们自己的单独日志文件下,例如 username.log。 I'm trying to figure out a way to push log events for these to CloudWatch using the PHP SDK without slowing the application down, AND while still being able to maintain a separate log file for each registered member of my website.我试图找出一种方法,使用 PHP SDK 将这些日志事件推送到 CloudWatch,而不会减慢应用程序的速度,同时仍然能够为我网站的每个注册成员维护一个单独的日志文件。

I can't for the life of me figure this out:我一生都无法弄清楚:

OPTION 1: How can I log to CloudWatch asynchronously using the CloudWatch SDK?选项 1:如何使用 CloudWatch SDK异步登录 CloudWatch? My PHP application is behaving VERY sluggishly, since each log line takes roughly 100ms to push directly to CloudWatch.我的 PHP 应用程序的行为非常缓慢,因为每个日志行大约需要 100 毫秒才能直接推送到 CloudWatch。 Code sample is below.代码示例如下。

OPTION 2: Alternatively, how could I configure an installed CloudWatch Agent on EC2 to simply OBSERVE all of my log files, which would basically upload them asynchronously to CloudWatch for me in a separate process?选项 2:或者,我如何在 EC2 上配置已安装的 CloudWatch 代理以简单地观察我的所有日​​志文件,这基本上会在单独的过程中为我将它们异步上传到 CloudWatch? The CloudWatch EC2 Logging Agent requires a static "configuration file" ( AWS documentation ) on your server which, to my knowledge, needs to lists out all of your log files ("log streams") in advance, which I won't be able to predict at the time of server startup. CloudWatch EC2 日志代理需要您服务器上的静态“配置文件”( AWS 文档),据我所知,它需要提前列出您的所有日志文件(“日志流”),而我无法做到在服务器启动时进行预测。 Is there any way around this (ie, simply observe ALL log files in a directory)?有没有办法解决这个问题(即,只需观察目录中的所有日志文件)? Config file sample is below.配置文件示例如下。

All ideas are welcome here, but I don't want my solution to simply be "throw all your logs into a single file, so that your log names are always predictable".这里欢迎所有想法,但我不希望我的解决方案只是“将所有日志放入一个文件中,以便您的日志名称始终是可预测的”。

Thanks in advance!!!提前致谢!!!


OPTION 1: Logging via SDK (takes ~100ms / logEvent):选项 1:通过 SDK 记录(大约需要 100 毫秒 / logEvent):

// Configuration to use for the CloudWatch client
$sharedConfig = [
    'region'  => 'us-east-1',
    'version' => 'latest',
    'http'    => [
        'verify' => false
    ]
];

// Create a CloudWatch client
$cwClient = new Aws\CloudWatchLogs\CloudWatchLogsClient($sharedConfig);

// DESCRIBE ANY EXISTING LOG STREAMS / FILES
$create_new_stream = true;
$next_sequence_id = "0";
$result = $cwClient->describeLogStreams([
        'Descending' => true,
        'logGroupName' => 'user_logs',
        'LogStreamNamePrefix' => $stream,
]);
// Iterate through the results, looking for a stream that already exists with the intended name
// This is so that we can get the next sequence id ('uploadSequenceToken'), so we can add a line to an existing log file
foreach ($result->get("logStreams") as $stream_temp) {
    if ($stream_temp['logStreamName'] == $stream) {
        $create_new_stream = false;
        if (array_key_exists('uploadSequenceToken', $stream_temp)) {
            $next_sequence_id = $stream_temp['uploadSequenceToken'];
        }
        break;
    }
}   

// CREATE A NEW LOG STREAM / FILE IF NECESSARY
if ($create_new_stream) { 
    $result = $cwClient->createLogStream([
        'logGroupName' => 'user_logs',
        'logStreamName' => $stream,
    ]);
}

// PUSH A LINE TO THE LOG *** This step ALONE takes 70-100ms!!! ***

$result = $cwClient->putLogEvents([
    'logGroupName' => 'user_logs',
    'logStreamName' => $stream,
    'logEvents' => [
        [
            'timestamp' => round(microtime(true) * 1000),
            'message' => $msg,
        ],
    ],
    'sequenceToken' => $next_sequence_id
]);

OPTION 2: Logging via CloudWatch Installed Agent (note that config file below only allows hardcoded, predermined log names as far as I know):选项 2:通过 CloudWatch 安装的代理记录(请注意,据我所知,下面的配置文件只允许硬编码的、预先确定的日志名称):

[general]
state_file = /var/awslogs/state/agent-state  

[applog]
file = /var/www/html/logs/applog.log
log_group_name = PP
log_stream_name = applog.log
datetime_format = %Y-%m-%d %H:%M:%S

Looks like we have some good news now... not sure if it's too late!看起来我们现在有一些好消息……不确定是否为时已晚!

CloudWatch Log Configuration CloudWatch 日志配置

So to answer the doubt,所以回答这个疑惑,

Is there any way around this (ie, simply observe ALL log files in a directory)?有没有办法解决这个问题(即,只需观察目录中的所有日志文件)?

yes, we can mention log files and file paths using wild cards, which can help you in having some flexibility in configuring from where the logs are fetched and pushed to the log streams.是的,我们可以使用通配符提及日志文件和文件路径,这可以帮助您灵活地配置从何处获取日志并将其推送到日志流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM