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

查看:146
本文介绍了我如何设置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分钟的延迟,因为crons不能被更频繁地运行。
  • 在每个FE机器上的cron将会有设置同步到部署服务器上的特定位置,但是锋线,我不知道有多少这样的FE服务器会存在。

要解决这个问题,我在看的方式来连接的error_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));
};

重启syslog服务:

restart syslog service:

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

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

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); };

重启系统日志服务器:

restart syslog servers:

sudo service syslog-ng restart


应用code更改

现在,在应用程序code 可由此改变。假设每个应用程序都有code这样写到一个单独的文件,你想同样的结构,以反映在中央日志服务器:

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');

新的code应该是这样的:

The new code should look like:

// 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服务器和日志服务器都在相同的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天全站免登陆