apache 下 lua完善网站记录日志

完善网站记录日志

目的

网站日志一直是运维的重要工具,比如一个会员的轨迹追踪,请求和响应内容等,apache,nginx等的默认配置明显不够使用,code写日志那就是灾难,很久以前用lua在nginx上实现一个,今天记录apache下lua实现。老年码农,不分享

nginx简单记录

access_by_lua_file
body_filter_by_lua_file
log_by_lua_file

apache

原理同nginx
开启mod_lua
设置省略…
lua源码:

require "apache2"
require "string"
function input_filter(r)
    local time=os.time()
	local input = '时间:'..os.date('%Y-%m-%d %H:%M:%S',time)..' 路径:'..r.uri..' 参数:'
	local reg = ''
	local isformdata=nil
	coroutine.yield()
	while bucket do
		reg = reg..bucket 
		coroutine.yield(bucket) 
	end
	for k,v in string.gmatch(reg,'%s+name="([%a_%-%w]+)"%s+([%a_%-%w]+)%s+') do
		input=input..k.."="..v.."&";
		isformdata=1
	end
	if isformdata ==nil then
		input=input..reg
	end
	input=input..r.args
	logger(input)
	return apache2.OK
end
function output_filter(r)
	local input =' 结果'
	coroutine.yield() 
	while bucket do 
		input = input..bucket 
		coroutine.yield(bucket) 
	end
	local time=os.time()
	logger(input..'[时间:'..os.date('%Y-%m-%d %H:%M:%S',time).."]\n")
end
function logger(msg)
	local file = io.open('D:/worker/public/secret.log', 'a')
	if file then
		file:write(string.format("%s",msg))
		file:close()
		return true
	end
	return false
end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值