|
With nginx-lua take service needs to write the log, the general nginx comes accesslog plus logrotate script to achieve, but can not meet the need to write more than one request log demand
Before using C and GO to achieve a similar code, the idea is to write the main process piping, pipes and child reading by the apache-rotatelogs write log
Lua is also used here to achieve a bit -
- Rlog is an non-blocking logging based on pipe.
- Rotatelogs of Apache is recommanded to be the output of pipe.
- Before using Rlog, you should install luaposix.
-
local posix = require ( "posix")
local os = require ( "os")
- Input of pipe
local pipe_in
- Init rlog
- @param Pipe_cmd pipe operator and the reveiver command.
- E.g. "| rotatelogs -l% Y% m% d% H.logfile 3600"
- @return True when success, false when fail
local function init (pipe_cmd)
- If already inited, do nothing
if pipe_in then return true end
- If pipe_cmd is not start with pipe operator, return false
if string.sub (pipe_cmd, 1, 1) ~ = "|" then return false end
- Create pipe
local pout, pin = posix.pipe ()
- Fork process to execute cmd
if posix.fork () == 0 then
- Close stdin
io.close (io.stdin)
- Set output of pipe as stdin
posix.dup2 (pout, posix.fileno (io.stdin))
- Close input and output of pipe
posix.close (pout)
posix.close (pin)
- Execute cmd
os.execute (string.sub (pipe_cmd, 2, -1))
end
- Get input of pipe
pipe_in = pin
- Close output of pipe
posix.close (pout)
return true
end
- Write log
local function log (log_str)
- Write log into pipe
posix.write (pipe_in, log_str .. " n")
end
return {
init = init,
log = log
}
Below is its testing program
local rlog = require ( "rlog")
- Init
ret = rlog.init ( "| rotatelogs -l% Y% m% d% H.log 3600")
if not ret then
print ( "init fail")
end
- Log
rlog.log ( "Hello Rlog")
Since posix.write write pipeline is atomic, so no need to worry about multi-process or multi-threaded write confusing issue
However, this code depends luaposix library needs to be installed before using it. |
|
|
|