lua-resty-filecache is an file write module with fd cache
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


lua-resty-filecache is an file write module with cache


    lua_package_path "/path/to/lua-resty-filecache/lib/?.lua;;";
    init_worker_by_lua_block {
        -- Import the module
        local filecache = require("resty.filecache")
        -- Start timer, regularly clean up the handle.
        filecache.start_cleanup_timer(600) --600 seconds no write operation, close the file handle.
    server {
        listen 199 default; 
        location /writelog {
            content_by_lua_block {
                local time = ngx.localtime()
                local date = string.sub(time,1,4) .. string.sub(time,6,7) .. string.sub(time, 9,10)
                local hour = string.sub(time, 12,13) 
                local filename = string.format("/tmp/logs/myserver/%s/%s.log", date, hour)
                local filecache = require("resty.filecache")
                local log = ngx.req.get_body_data()
                log = log .. "\n"
                local err = filecache.write(filename, log)
                if err then 
                    ngx.log(ngx.ERR, "write log to [", filename, "] failed! err:", tostring(err))

Test Write Log

curl -d "testlog"  

View the log

tail -f /tmp/logs/myserver/$yyyymmdd/$hh.log

#Author Xiaojie Liu.