Sho*_*hoe 2 php error-handling logging
我正在尝试创建一个简单的错误日志,记下php错误,以防这些错误无法报告给管理员(使用数据库连接).我写了这个:
function errorLog($array)
{
$time = date("F j, Y, g:i a");
$ip = $_SERVER['REMOTE_ADDR'];
$no = $array['e-no'];
$string = $array['e-string'];
$file = $array['e-file'];
$line = $array['e-line'];
$context = $array['e-context'];
$string = "TIME {$time} IP {$ip} ERROR {$no} | {$string} | {$file} | {$line} | {$context}";
if ($handler = fopen('log/log-file.txt', 'a'))
{
fwrite($handler, $string);
fclose($handler);
}
else
{
exit('Fatal Error #0002');
}
}
Run Code Online (Sandbox Code Playgroud)
开始编辑
功能已成为
function errorLog($array)
{
$time = date("F j, Y, g:i a");
$ip = $_SERVER['REMOTE_ADDR'];
$type = $array['e-type'];
$string = $array['e-string'];
$file = $array['e-file'];
$line = $array['e-line'];
$context = $array['e-context']
$string = "ON {$time} IP {$ip} ERROR {$type} | {$string} | {$file} | {$line} | {$context}";
if (!error_log($string, 3, 'log/log-file.txt')) { exit('Fatal Error #0002'); }
}
Run Code Online (Sandbox Code Playgroud)
结束编辑
但是,当我运行它时,我得到了
Warning: fopen(log/log-file.txt) [function.fopen]: failed to open stream: Permission denied in myfile.php on line 17
Run Code Online (Sandbox Code Playgroud)
我得到这是一个服务器权限问题,但我正在使用localhost(Mac OS X上的Xampp),我想管理这些权限.我怎样才能将日志/只能写入?有没有更好的方法来做我想做的事情?
我得到这是一个服务器权限问题,但我正在使用localhost(Mac OS X上的Xampp),我想管理这些权限.我怎样才能将日志/只能写入?有没有更好的方法来做我想做的事情?
您可能需要签出set_error_handler,以确保通过自定义函数自动发送所有错误.无论如何,这不是问题的根源,您的问题实际上是服务器上的权限.现在,要做的第一件事就是确定PHP运行的用户.这可以非常简单地确定,您只需运行此脚本:
<?php
echo `whoami`;
Run Code Online (Sandbox Code Playgroud)
对于你的Mac,这可能是'www-data'.如果你想改变它(可能),你应该深入研究Apache MPM ITK的工作原理.如果运行脚本的'www-data'没问题,则应该对日志目录赋予'www-data'访问权限.首先,转到父目录并发出命令'ls -laF',以查看有关日志目录的详细信息.假设输出如下:
rwxr-xr-x 2浆果浆果4096 2011-01-18 16:53日志/
在这种情况下,我(用户"浆果")将是所有者.我主要在的组(即"berry")是拥有该目录的组.现在,您可以通过rwx rx rx告诉所有者(第一个rwx)可以读取,写入和执行.第二个(组)可以读取和执行.其他人(其他人)可以阅读和执行.
"www-data"可能不属于"浆果"组.要解决此问题,您需要发出命令:chgrp -R www-data log/,后跟chmod -R g+x log/.这意味着:将拥有组更改为"www-data",并给予组(g)执行权限(+ x),在目录"log /"上递归执行(-R).
祝好运.
| 归档时间: |
|
| 查看次数: |
13516 次 |
| 最近记录: |