使用 PHP 读取系统日志

发布于 2024-10-01 03:23:37 字数 511 浏览 0 评论 0原文

我正在尝试增加我正在开发的 php 项目的管理功能。截至目前,该项目将把所有错误记录到系统日志中。我试图让管理页面能够读取这些错误的系统日志。在终端中我可以输入:

cat /var/log/syslog | grep projectName

但是,如果我尝试使用以下命令运行此命令:

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

或使用:

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

$output 总是显示为空。我有办法做到这一点吗?这是最好的方法吗(IE php 有读取系统日志的本机方法吗)?

编辑: 看来问题是权限问题。我可以授予 www-data 访问系统日志的权限,但由于某种原因,这对我来说似乎不是一个好主意。没有原生的方式来读取日志吗?

I am trying to increase the admin functionality of a php project I working on. As of now the project will log all of its errors to the 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. 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)?

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. Is there no native way to read the log?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄居者 2024-10-08 03:23:37

我最终创建了一个完全独立的日志文件。这解决了所有权限问题

I ended up creating a completely separate log file. This took care of all the permission issues

两仪 2024-10-08 03:23:37

你不能在 php 中打开 /var/log/syslog 来代替执行 shell 命令吗?

Can't you open /var/log/syslog your self in php instead of doing a shell command?

灵芸 2024-10-08 03:23:37

听说过 sudo 吗?如果您关心安全性,这可能是您的最佳选择。但我认为 sudo 路线太复杂了,只是给网络用户一个读取权限,而不是写入读取权限。为了使这成为一场安全音乐会,其他人必须有一种方法来编写读取 syslog 文件的 php 脚本。如果您是唯一可以将 php 脚本上传到您的服务器的人,那么您根本不用担心这一点。对于共享主机来说,这是一个问题,因为任何人现在都可以读取服务器的系统日志。

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. 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. If you are the only one who can upload php scripts to your server, then you should not worry about that at all. For shared hosting this is a problem, of cause as anyone will now be able to read server's syslog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文