当前位置: 首页 > news >正文

网站备案 异地新闻软文怎么写

网站备案 异地,新闻软文怎么写,尚义住房和城乡规划建设局网站,广西教育学会 网站建设需求描述 日志每天会以天为单位产生一个日志,不清理的话会越来越多。这里写一个Lua自定定时清理日志目录下的日志文件。 依赖安装 安装 lfs 模块 yum install luarocks yum install lua-develluarocks install luafilesystem 创建模拟旧文件 创建了一个1月的旧…

需求描述

日志每天会以天为单位产生一个日志,不清理的话会越来越多。这里写一个Lua自定定时清理日志目录下的日志文件。

 依赖安装

安装 lfs 模块

yum install luarocks
yum install lua-develluarocks install luafilesystem

创建模拟旧文件

创建了一个1月的旧文件

[root@iZbp1xxxxxxxxxq7ioZ 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 "lfs"local 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  escape=json '{"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_GlobalId"''"response_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_fails=5 fail_timeout=30s;server 10.66.66.88:8501 max_fails=5 fail_timeout=30s;}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天之前的日志文件没有了。

[root@iZbpxxxxxxxxxxxoZ 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.yayakq.cn/news/618341/

相关文章:

  • 做可视化的网站wordpress 段子模板
  • 网上房地产网站长春网站开发公司
  • 网站建设人员叫什么科目宁波网络推广公司价格
  • 定制网站开发蒙特php网站怎么做
  • 做理财的网站有哪些上海外贸学院
  • 云南做公司网站多少钱软件开发文档总结
  • 创建网站需要什么昆明网站建设 昆明光硕
  • 网站触屏版产品软文范例500字
  • 网站建设评审标准如何设计网站建设方案
  • 网站流量是怎么赚钱的自己开发网站怎么开发
  • 网站建设南昌中国工程预算网
  • 怎样搭建免费网站东莞旅游必去十大景点
  • 天涯重庆论坛seo优化招商
  • 网站设计建设有限公司开发网站建设设计公司
  • 江苏住房与城乡建设厅网站建设网站代理
  • 网站建设与O2O的应用网站背投广告代码
  • php网站开发说明文档凡科门店通
  • 搜索引擎网站优化推广云南网站建设维修公司
  • 云服务器怎么建立网站高端品牌网站建设方案
  • 住建局网站官网头像在线制作网站
  • 南充企业网站建设html代码分享
  • 西安公司网站费用wordpress多媒体
  • 北京seo推广外包下载班级优化大师并安装
  • 动画制作器玉林网站优化
  • 用dedecms织梦做中英文网站建德网站制作公司
  • 温州市建设工程质量监督站网站东莞做网站的联系电
  • 浙江专业做网站微信小程序开发技术栈
  • 中山学校网站建设施工合同
  • 哈尔滨做网站企业网站后台管理软件
  • 多合一网站源码wordpress 类别 排序