简体   繁体   English

将PHP syslog发送到特定的Linux日志文件

[英]Sending PHP syslog to a specific Linux log file

I am running Centos/Apache with PHP 5.5.14, and am using syslog to write to a log file as follows: 我正在使用PHP 5.5.14运行Centos / Apache,并且正在使用syslog如下写入日志文件:

syslog(LOG_INFO,'Some message');

The logs are being written to /var/log/messages and look like Aug 10 15:48:16 devserver httpd: Some message , however, the log file is also cluttered with a bunch of logs that look like Aug 10 15:48:21 devserver kernel: usb 1-1.2: USB disconnect, device number 83 . 日志正在写入/var/log/messages ,看起来像Aug 10 15:48:16 devserver httpd: Some message ,但是,日志文件中也Aug 10 15:48:16 devserver httpd: Some message日志,看起来像Aug 10 15:48:21 devserver kernel: usb 1-1.2: USB disconnect, device number 83

How do make PHP sent logs to its own dedicated log file? 如何使PHP将日志发送到其专用日志文件?

Before calling the function syslog , you should call openlog and specify the “syslog tag”. 在调用syslog函数之前,您应该调用openlog并指定“ syslog标签”。 With this framework, the messages are tagged and you can use the system logger (rsyslog, syslog-ng, etc.) to send these messages to a specific log file, send them to a specific log server, send an email for emergency messages, etc. 在此框架下,消息将被标记,您可以使用系统记录器(rsyslog,syslog-ng等)将这些消息发送到特定的日志文件,将它们发送到特定的日志服务器,发送紧急消息的电子邮件,等等

This blog post “Using syslog for your php applications” gives more details on how to setup the whole thing. 这篇博客文章“为您的php应用程序使用syslog”提供了有关如何设置整个组件的更多详细信息。

Syslog dumps data into a singular log file. Syslog将数据转储到单个日志文件中。 A lot of system services, even the kernel as you pointed out, dumps to syslog, and by extension, the same log file. 许多系统服务,甚至是您所指出的内核,都转储到syslog中,并扩展到相同的日志文件。

If you want to take advantage of your own personal log file, you'll need to use filesystem IO functions. 如果要利用自己的个人日志文件,则需要使用文件系统IO功能。

Example: 例:

<?php

function writeLog($message) {
  $handle = fopen('/var/log/project1/myproject1.log', 'a');
  $line   = '[' . date('Y-m-d H:i:s T') . '] ' . $message . "\n";
  fwrite($handle, $line);
  fclose($handle);
}

You should take care to make sure you don't fopen() and then subsequently fclose() a lot, since that will severely slow down your code. 您应该注意不要先fopen() ,然后再关闭fclose() ,因为这会严重降低代码速度。 You should only fopen() when you need to write/read, and then leave the file handle open until your PHP code exits (or let PHP do garbage collection for you). 仅在需要写入/读取时才应使用fopen() ,然后将文件句柄保持打开状态,直到PHP代码退出(或让PHP为您进行垃圾回收)为止。

You'll also need to make sure your web server has sufficient privileges to write to the /var/log/project1/ directory, or at the very least, append/write access to the filename specified. 您还需要确保您的Web服务器具有足够的特权来写入/var/log/project1/目录,或者至少具有对指定文件名的附加/写入访问权限。 This includes making sure SELinux is happy if you have it enabled. 这包括确保SELinux在启用时感到高兴。

As a bonus, you could throw your custom log file into logrotate daemon's config and have your logs automatically rotated, compressed, archived, and whatnot. 另外,您可以将自定义日志文件放入logrotate守护程序的配置中,并使日志自动旋转,压缩,存档等。

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

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