网站建设 中企动力洛阳分公司,做电商网站的步骤,wordpress公共课,网站建设远程培训需求描述
日志每天会以天为单位产生一个日志#xff0c;不清理的话会越来越多。这里写一个Lua自定定时清理日志目录下的日志文件。 依赖安装
安装 lfs 模块
yum install luarocks
yum install lua-develluarocks install luafilesystem
创建模拟旧文件
创建了一个1月的旧…需求描述
日志每天会以天为单位产生一个日志不清理的话会越来越多。这里写一个Lua自定定时清理日志目录下的日志文件。 依赖安装
安装 lfs 模块
yum install luarocks
yum install lua-develluarocks install luafilesystem
创建模拟旧文件
创建了一个1月的旧文件
[rootiZbp1xxxxxxxxxq7ioZ logs]# ll
total 24
-rw-r--r-- 1 app app 5124 Jan 16 15:30 access_api_.json_2024-01-16
-rw-r--r-- 1 app app 8540 Aug 16 2024 access_api_.json_2024-08-16
-rw-r--r-- 1 app app 0 Aug 14 2024 api-error.log
-rw-r--r-- 1 root root 747 Jan 16 16:41 luatest.lua 配置文件及脚本
检查配置文件后开始重启中间的Lua脚本会24小时执行一次检查目录中是否有180天的日志文件。
user root;
worker_processes 6;
events {worker_connections 30000;
}
http {
# lua环境变量
lua_package_cpath /usr/lib64/lua/5.1/?.so;;;###init_worker_by_lua_block {local lfs require lfslocal function clean_old_logs(premature)if premature thenreturnend-- 日志目录路径local log_path /data/logs/-- 当前时间local current_time os.time()-- 定义保留天数local retain_days 180local retain_seconds retain_days * 24 * 60 * 60-- 遍历日志目录for file in lfs.dir(log_path) do-- 检查文件名是否符合日志格式if file:match(access_api.json_%d%d%d%d%-%d%d%-%d%d) then-- 获取文件的完整路径local file_path log_path .. file-- 获取文件的属性local attr lfs.attributes(file_path)if attr thenlocal file_age current_time - attr.modificationngx.log(ngx.NOTICE, File: , file_path, Age: , file_age, Retain seconds: , retain_seconds)-- 如果文件超过30天则删除if file_age retain_seconds thenlocal success, err os.remove(file_path)if success thenngx.log(ngx.NOTICE, Deleted old log file: , file_path)elsengx.log(ngx.ERR, Failed to delete log file: , file_path, Error: , err)endendelsengx.log(ngx.ERR, Failed to get attributes for file: , file_path)endendendend-- 设置定时器每86400秒即24小时执行一次local ok, err ngx.timer.every(86400, clean_old_logs)if not ok thenngx.log(ngx.ERR, Failed to create timer: , err)end}###include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;###日志格式log_format json escapejson {timestamp:$time_iso8601,remote_addr: $remote_addr, referer: $http_referer, request: $request, status: $status, bytes: $body_bytes_sent, agent: $http_user_agent, x_forwarded: $http_x_forwarded_for, up_addr: $upstream_addr,up_host: $upstream_http_host,up_resp_time: $upstream_response_time,request_time: $request_time,request_GlobalId: $http_GlobalId,response_GlobalId: $sent_http_GlobalIdresponse_body: $resp_body,request_body: $request_body };###日志按天分割map $time_iso8601 $logdate{~^(?ymd\d{4}-\d{2}-\d{2}) $ymd;default date-not-found;}###charset utf-8 ;gzip on;# 后端IP地址upstream api-prod {server 10.66.66.86:8501 max_fails5 fail_timeout30s;server 10.66.66.88:8501 max_fails5 fail_timeout30s;}server {listen 80 ;listen 7309 ;server_name api.xxxxxx.cn api-test.xxxxxx.cn;charset utf-8 ;#日志配置lua_need_request_body on;set $resp_body ;body_filter_by_lua local resp_body string.sub(ngx.arg[1], 1, 1000)ngx.ctx.buffered (ngx.ctx.buffered or ) .. resp_bodyif ngx.arg[2] thenngx.var.resp_body ngx.ctx.bufferedend;location / {proxy_pass http://api-prod;# 屏蔽 SLBHealthCheck 和 Blackbox Exporter/0.21.1 的 404 请求日志if ($http_user_agent ~* (SLBHealthCheck|Blackbox Exporter/0.21.1)) {access_log off;return 200;}}access_log /data/logs/access_api.json_$logdate json;error_log /data/logs/api-error.log error;
}
}验证
180天之前的日志文件没有了。
[rootiZbpxxxxxxxxxxxoZ logs]# ll /data/logs
total 16
-rw-r--r-- 1 root root 8540 Aug 16 16:37 access_api.json_2024-08-16
-rw-r--r-- 1 root root 0 Aug 14 16:36 api-error.log
-rw-r--r-- 1 root root 747 Jan 16 2024 luatest.lua 文章转载自: http://www.morning.pyxtn.cn.gov.cn.pyxtn.cn http://www.morning.rgfx.cn.gov.cn.rgfx.cn http://www.morning.kldtf.cn.gov.cn.kldtf.cn http://www.morning.guangda11.cn.gov.cn.guangda11.cn http://www.morning.rlcqx.cn.gov.cn.rlcqx.cn http://www.morning.zcwwb.cn.gov.cn.zcwwb.cn http://www.morning.kjgrg.cn.gov.cn.kjgrg.cn http://www.morning.tsqrc.cn.gov.cn.tsqrc.cn http://www.morning.qzpsk.cn.gov.cn.qzpsk.cn http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn http://www.morning.jzxqj.cn.gov.cn.jzxqj.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.wynqg.cn.gov.cn.wynqg.cn http://www.morning.mnkhk.cn.gov.cn.mnkhk.cn http://www.morning.hcsqznn.cn.gov.cn.hcsqznn.cn http://www.morning.brscd.cn.gov.cn.brscd.cn http://www.morning.crhd.cn.gov.cn.crhd.cn http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn http://www.morning.qdlnw.cn.gov.cn.qdlnw.cn http://www.morning.jjhng.cn.gov.cn.jjhng.cn http://www.morning.ljglc.cn.gov.cn.ljglc.cn http://www.morning.lrzst.cn.gov.cn.lrzst.cn http://www.morning.pznhn.cn.gov.cn.pznhn.cn http://www.morning.tzmjc.cn.gov.cn.tzmjc.cn http://www.morning.wpmlp.cn.gov.cn.wpmlp.cn http://www.morning.qgcfb.cn.gov.cn.qgcfb.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.znpyw.cn.gov.cn.znpyw.cn http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.yqpck.cn.gov.cn.yqpck.cn http://www.morning.fy974.cn.gov.cn.fy974.cn http://www.morning.gtbjf.cn.gov.cn.gtbjf.cn http://www.morning.gccdr.cn.gov.cn.gccdr.cn http://www.morning.wnqbf.cn.gov.cn.wnqbf.cn http://www.morning.nkhdt.cn.gov.cn.nkhdt.cn http://www.morning.cyhlq.cn.gov.cn.cyhlq.cn http://www.morning.wnzgm.cn.gov.cn.wnzgm.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.rttp.cn.gov.cn.rttp.cn http://www.morning.mjmtm.cn.gov.cn.mjmtm.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn http://www.morning.qxjck.cn.gov.cn.qxjck.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn http://www.morning.rhgtc.cn.gov.cn.rhgtc.cn http://www.morning.tqklh.cn.gov.cn.tqklh.cn http://www.morning.xwrhk.cn.gov.cn.xwrhk.cn http://www.morning.kwjyt.cn.gov.cn.kwjyt.cn http://www.morning.tcxzn.cn.gov.cn.tcxzn.cn http://www.morning.htjwz.cn.gov.cn.htjwz.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.kqyyq.cn.gov.cn.kqyyq.cn http://www.morning.ffmx.cn.gov.cn.ffmx.cn http://www.morning.pjtw.cn.gov.cn.pjtw.cn http://www.morning.zpfqh.cn.gov.cn.zpfqh.cn http://www.morning.chxsn.cn.gov.cn.chxsn.cn http://www.morning.c7496.cn.gov.cn.c7496.cn http://www.morning.xbhpm.cn.gov.cn.xbhpm.cn http://www.morning.rlkgc.cn.gov.cn.rlkgc.cn http://www.morning.ykxnp.cn.gov.cn.ykxnp.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.qnxtz.cn.gov.cn.qnxtz.cn http://www.morning.txqgd.cn.gov.cn.txqgd.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.dxrbp.cn.gov.cn.dxrbp.cn http://www.morning.kczkq.cn.gov.cn.kczkq.cn http://www.morning.ahscrl.com.gov.cn.ahscrl.com http://www.morning.kwqqs.cn.gov.cn.kwqqs.cn http://www.morning.npxht.cn.gov.cn.npxht.cn http://www.morning.ruifund.com.gov.cn.ruifund.com http://www.morning.ghgck.cn.gov.cn.ghgck.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.gsksm.cn.gov.cn.gsksm.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.nkjkh.cn.gov.cn.nkjkh.cn http://www.morning.zmtrk.cn.gov.cn.zmtrk.cn http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn