我正在为多个用户构建一个 LAMP 服务器以使用 mod_userdir 练习 Web 开发,并编写了脚本来分解访问和错误日志以记录用户主目录中的目录。
access_log.sh 脚本工作正常(日志行同时写入主服务器日志文件和用户日志文件),但 error_log.sh 仅更新主日志文件。这是error_log.sh:
#!/bin/bash
IFS='
'
userlogfile=error.log
syslogfile=/var/log/apache2/cs-web_error.log
homedirs=`ls -d /home/*`
while read logline; do
echo -e ${logline} >> ${syslogfile}
#echo -e "writing ${logline} to ${syslogfile}" #TESTING
for homedir in $homedirs; do
userlogpath="${homedir}/logs/${userlogfile}"
if echo $logline | grep ${homedir} > /dev/null; then
echo -e ${logline} >> ${userlogpath}
#echo -e "writing ${logline} to ${userlogpath}" #TESTING
fi
done
done
Apache 的日志记录配置是:
CustomLog "| ${APACHE_LOG_DIR}/access_log.sh" common
ErrorLog "| ${APACHE_LOG_DIR}/error_log.sh"
如果我取消注释 #TESTING 语句,则在通过脚本传递错误日志时会得到如下输出:
writing [Fri Sep 06 12:15:11 2013] [error] [client 193.61.230.178] PHP Parse error:
syntax error, unexpected 'select' (T_STRING) in /home/ginny/htdocs/test.php on line 12
to /var/log/apache2/cs-web_error.log
writing [Fri Sep 06 12:15:11 2013] [error] [client 193.61.230.178] PHP Parse error:
syntax error, unexpected 'select' (T_STRING) in /home/ginny/htdocs/test.php on line 12
to /home/ginny/logs/error.log
...这表明 grep 语句正在工作。此外,像这样手动运行,文件被创建并写入:
root@cs-web:~# ls -l /home/ginny/logs/
total 16
-rw-r--r-- 1 root root 2213 Sep 6 12:53 access.log
-rw-r--r-- 1 root root 9984 Sep 6 12:49 error.log
我正在努力理解的是为什么这适用于访问日志,而不适用于错误日志 - 唯一的区别是:
root@cs-web:/var/log/apache2# diff access_log.sh error_log.sh
6,7c6,7
< userlogfile=access.log
< syslogfile=/var/log/apache2/cs-web_access.log
---
> userlogfile=error.log
> syslogfile=/var/log/apache2/cs-web_error.log
12a13
> #echo -e "writing ${logline} to ${syslogfile}"
15,17c16,17
< homedir=${homedir#/home/}
< userlogpath="/home/${homedir}/logs/${userlogfile}"
< if echo $logline | egrep "/(~|users/)${homedir}/" > /dev/null; then
---
> userlogpath="${homedir}/logs/${userlogfile}"
> if echo $logline | grep ${homedir} > /dev/null; then
18a19
> #echo -e "writing ${logline} to ${userlogpath}"
收到的建议很感激。