如何设置 PHP 日志记录以转到远程服务器? [英] How do I set up PHP Logging to go to a remote server?

查看:57
本文介绍了如何设置 PHP 日志记录以转到远程服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将进行部署设置,其中我们将拥有许多服务器,其中大部分会在流量上升时自动添加到负载平衡器中.这种设置的问题在于,如果单个开发人员需要拖尾日志来解决某些问题,他将不得不在每台服务器上打开一个控制台,由于开发人员通常不知道我们可能有多少台服务器,这使得情况变得复杂有当时的作用.

We are going to a deployment setup where we will have many servers, most of which auto-added to the load balancer when the traffic goes up. The trouble with this setup is that if an individual developer needs to tail logs to troubleshoot something, he will have to open a console on each server, which is made complicated by the fact that the developer often doesn't know HOW many servers we might have in function at that time.

如果开发人员可以在一台服务器上找到所有日志——比如我们的部署服务器,那么故障排除就会变得更容易.

If the developer can find all the logs in one server - say our deploy server, then the troubleshooting becomes easier.

为此,我正在考虑使用 cron 设置从每台 FE 机器到部署服务器的推送,这将复制我们部署服务器上的日志.这种方法有两个问题:

Towards this, I was thinking of setting up a push from each FE machine to the deploy server using a cron which will replicate the logs on our deploy server. There are two problems with this approach:

  • 有 1 分钟的延迟,因为 cron 不能更频繁地运行.
  • 必须将每台 FE 机器上的 cron 设置为同步到部署服务器上的特定位置,但在此之前,我不知道将存在多少这样的 FE 服务器.

为了解决这个问题,我正在寻找一种连接 error_log 或 PEAR Log 的方法,将日志直接发送到我们的部署服务器,它将实时记录到它的本地位置/var/log/..

To solve this problem, I am looking at a way to connect error_log, or PEAR Log to send the logs directly to our deploy server, which will log it in real time to it's local locations at /var/log/..

有人知道我该如何配置吗?或者也许是完成此任务的服务?

Anybody knows how I can configure this? Or perhaps a service which accomplishes this?

我们的服务器都是 Ubuntu 10.04 LTS,我们在 AWS 的 EC2 实例上运行这些服务器.我们的 PHP 版本是 5.3.

Our servers are all Ubuntu 10.04 LTS and we are running these servers on EC2 instances in AWS. Our PHP version is 5.3.

推荐答案

@MichelFeldheim 的答案是起源,但我们对其进行了改进,以处理写入多个日志文件的多个应用程序.

The answer from @MichelFeldheim was the genesis, but we improved upon it to handle multiple applications writing to multiple log files.

中央日志服务器

中央日志服务器中,安装 syslog-ng 并进行如下配置:

In the central logging server, install syslog-ng and configure it thus:

sudo apt-get install syslog-ng

将以下内容添加到/etc/syslog-ng/syslog-ng.conf:

add the following to /etc/syslog-ng/syslog-ng.conf:

destination d_php { file("$PROGRAM" owner(www-data) group(www-data) perm(0644)); };
filter f_php { program("^/var/log/"); };
log { source(s_all); filter(f_php); destination(d_php); flags(final); };

source s_all {
        # ....
        # .... LET THE PREVIOUS CONTENT STAY - add the following line
        tcp(port(5140) keep_alive(yes));
};

重启系统日志服务:

sudo service syslog-ng restart

<小时>

在 FE 服务器上

在每个 FE 服务器上,安装 syslog-ng 并进行如下配置:

On each of the FE Servers, install syslog-ng and configure it thus:

sudo apt-get install syslog-ng

将以下内容添加到每个 FE 服务器上的/etc/syslog-ng/syslog-ng.conf:

add the following to /etc/syslog-ng/syslog-ng.conf on each of the FE servers:

destination php { tcp("log.example.com" port(5140)); };
log { source(s_all); filter(f_php); destination(php); };
filter f_php { facility(user); };

重启系统日志服务器:

sudo service syslog-ng restart

<小时>

应用程序代码更改:

现在,可以更改应用程序代码.假设每个应用程序都有这样的代码写入单独的文件,并且您希望在中央日志服务器中反映相同的结构:

Now, the application code can be changed thus. Suppose each of the application have code like this writing to a separate file and you want the same structure to be reflected in the central log server:

// PREVIOUS CODE: using PEAR Log
include '/usr/share/php/Log.php';
$log = Log::singleton('file', '/var/log/nginx/xxx.log', '', array(), PEAR_LOG_INFO);
// PREVIOUS CODE: Using error_log
ini_set('error_log' , '/var/log/nginx/xxx.log');

新代码应如下所示:

// NEW CODE: using PEAR Log
include '/usr/share/php/Log.php';
$log = Log::singleton('syslog', LOG_USER, '/var/log/nginx/xxx.log', array(), PEAR_LOG_INFO);
// NEW CODE: Using error_log
ini_set(‘error_log’, ‘syslog’);
openlog('/var/log/nginx/xxx.log', LOG_NDELAY, LOG_USER);

如果您的 FE 服务器和 Logging 服务器都在同一个 EC2 安全组内,则无需打开端口,因为在组内,所有端口都可以自由访问,只要服务正在侦听它.

If your FE servers and the Logging servers are all within the same EC2 security group, then there is no need to open the ports, since within the groups, all ports can be accessed freely, so long as a service is listening to it.

这种方法允许您的每个应用程序、模块决定是否需要中央日志记录.

This approach allows your each of your apps, modules to decide whether they want central logging or not.

这篇关于如何设置 PHP 日志记录以转到远程服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆