我们使用的是tnginx(支持日志抽样、直接输出到log服务器等)详细安装配置见另一篇博文,
http://michaelkang.blog.51cto.com/1553154/1359106
调用方式可以看一下的文章;
调用cronolog
http://tengine.taobao.org/document_cn/http_log_cn.html
#cronolog官网
http://cronolog.org/
软件下载链接(稳定版)
wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
1,安装cronolog
tar zxvf cronolog-1.6.2.tar.gz
cd cronolog-1.6.2
mkdir -p /usr/local/cronolog
./configure --prefix=/usr/local/cronolog
make
make install
ln -s /usr/local/cronolog/sbin/cronolog /usr/bin/
2,创建一个命名管道
mkfifo /data/access_log_pipe
3,配置cronolog,按需配置:
精确到分钟
nohup cat /data/access_log_pipe | /usr/local/cronolog/sbin/cronolog /data/logs/%Y%m%d/access_%Y%m%d%H%M.log &
精确到小时
nohup cat /data/access_log_pipe | /usr/local/cronolog/sbin/cronolog /data/logs/%Y%m%d/access_%Y%m%d%H.log &
注意: cronolog必须在nginx启动前启动。如果nginx先启动了,可以reload实现。
4:相关应用方式测试;
#####################################################################
4.1日志直接写入文:100/1抽样测试
测试脚本:
for i in $(seq 1000);do echo --$i---;curl http://192.168.2.151/lua;done
nginx配置:
server {
listen 80;
server_name localhost;
access_log /data/logs/access_2014021203.log main ratio=0.01;
结果统计:
[root@computer logs]# wc -l access_2014021203.log #按照分钟切割日志
10 access_2014021203.log
####################################################################
4.2 日志写入 管道文件 抽样测试
测试脚本:
for i in $(seq 1000);do echo --$i---;curl http://192.168.2.151/lua;done
nginx配置:
server {
listen 80;
server_name localhost;
access_log /data/access_log_pipe main ratio=0.01;
[root@computer 20140212]# wc -l access_2014021203.log
10 access_2014021203.log
4.3 测试结论;
nginx结合cronolog能够按照需求灵活的切割日志,nginx之前简单总结啦一下有三种日志切割方式,详见:
http://michaelkang.blog.51cto.com/1553154/1359062
cronolog对日志切割算比较灵活的,就是管道文件不是以服务的方式启动的,放置在后台比较担心稳定性。
下面是整理的将cronolog启动文件添加到nginx启动文件内容:
5.将cronolog启动文件嵌入nginx启动文件,保证在nginx启动之前启动管道文件。
###########nginx启动配置文件内置cronolog启动
[root@computer ~]# more /etc/init.d/nginx.cronolog
#!/bin/sh
#
# Comments to support chkconfig
#
# chkconfig: 2345 77 77
# Description: Startup script for nginx webserver
#
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
#日志存放位置
if [ ! -d "/data/logs" ]; then
mkdir -p "/data/logs"
fi
#生成管道文件位置
if [ ! -d "/usr/local/nginx/pipe/" ]; then
mkdir -p "/usr/local/nginx/pipe/"
fi
#cronolog相关变量
LOGPATH=/data/logs
PIPEPATH=/usr/local/nginx/pipe/
ERROE_LOG_PIPE=/usr/local/nginx/pipe/error_log_pipe
ACCESS_LOG_PIPE=/usr/local/nginx/pipe/access_log_pipe
CRONOLOG=/usr/local/cronolog/sbin/cronolog
d_mkfifo(){
#判断管道文件属性、建立管道文件
if [ ! -p "$ERROE_LOG_PIPE" ]; then
/bin/rm "$ERROE_LOG_PIPE"
mkfifo "$ERROE_LOG_PIPE"
fi
if [ ! -p "$ACCESS_LOG_PIPE" ]; then
/bin/rm "$ACCESS_LOG_PIPE"
mkfifo "$ACCESS_LOG_PIPE"
fi
}
cronolog_start()
{
#ps -ef|grep wireless|grep -v grep |awk '{print $2}'|xargs kill -9
nohup cat "$ACCESS_LOG_PIPE" | $CRONOLOG $LOGPATH/%Y%m%d/Access_%Y%m%d%H.log &
nohup cat "$ERROE_LOG_PIPE" | $CRONOLOG $LOGPATH/%Y%m%d/Error_%Y%m%d%H.log &
}
cronolog_kill()
{
ps -ef|grep data/logs|grep -v grep |awk '{print $2}'|xargs kill -9
ps -ef|grep nginx/pipe|grep -v grep |awk '{print $2}'|xargs kill -9
}
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
d_start() {
d_mkfifo
cronolog_start
$DAEMON -c $CONFIGFILE || echo -n " already running"
}
d_stop() {
# kill -QUIT `cat $PIDFILE` || echo -n " not running"
cronolog_kill
killall nginx || echo -n " not running"
}
d_reload() {
kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
status)
pgrep -x $NAME > /dev/null && echo $NAME \(pid `pgrep $NAME`\) is running... || echo "$NAME is stoped."
;;
test|-t)
$DAEMON -t
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 2
d_start
echo "...done!"
;;
*)
echo "Usage: $SERVICENAME {start|stop|restart|reload|status|check}"
exit 3
;;
esac
exit 0