简体   繁体   English

使用PHP读取系统日志

[英]Using PHP to read the syslog

I am trying to increase the admin functionality of a php project I working on. 我正在尝试增加我正在工作的php项目的管理功能。 As of now the project will log all of its errors to the syslog. 到目前为止,该项目会将其所有错误记录到syslog中。 I am trying to give the admin page the ability to read the syslog for these errors. 我正在尝试使管理员页面能够读取这些错误的系统日志。 in the terminal I can type: 在终端中,我可以输入:

cat /var/log/syslog | grep projectName

However ever if I try to run this command with: 但是,如果我尝试使用以下命令运行此命令:

$output=shell_exec('cat /var/log/syslog | grep projectName');

or with : 或搭配:

$output=`cat /var/log/syslog | grep projectName`;

$output always comes up as empty. $ output总是显示为空。 Is there a way I can do this? 有办法吗? Is this even the best way (IE does php have a native way to read the syslog)? 这是否是最好的方法(IE php是否具有读取syslog的本机方法)?

EDIT: it seems that the issue is a permissions one. 编辑:似乎问题是权限之一。 I could give www-data permission to access syslog, but for some reason that doesn't seem like a good idea to me. 我可以授予www-data访问syslog的权限,但是由于某种原因,这对我来说似乎不是一个好主意。 Is there no native way to read the log? 没有本地方法可以读取日志?

I ended up creating a completely separate log file. 我最终创建了一个完全独立的日志文件。 This took care of all the permission issues 这解决了所有权限问题

您不能在php中打开/ var / log / syslog自己,而不是执行shell命令吗?

Ever hear of sudo? 听说过须藤吗? It may be your best choice if you concerned with security. 如果您关注安全性,这可能是您的最佳选择。 But I think going the sudo route is too complicated, just give a read premission to web user, it's not a write a read premission. 但是我认为使用sudo路由太复杂了,只给Web用户一个读权限,而不是一个写读权限。 In order for this to be a security concert there must be a way for someone else to write a php script that reads the syslog file. 为了使这成为一场安全音乐会,必须有一种方法让其他人编写一个读取syslog文件的php脚本。 If you are the only one who can upload php scripts to your server, then you should not worry about that at all. 如果您是唯一可以将php脚本上传到您的服务器的人,那么您根本不必担心。 For shared hosting this is a problem, of cause as anyone will now be able to read server's syslog. 对于共享主机,这是一个问题,因为任何人现在都可以读取服务器的系统日志。

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

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