log_by_lua '
local ffi = require "ffi"
ffi.cdef[[
typedef struct {
char *fpos;
void *base;
unsigned short handle;
short flags;
short unget;
unsigned long alloc;
unsigned short buffincrement;
} FILE;
FILE *fopen(const char *filename, const char *mode);
int fprintf(FILE *stream, const char *format, ...);
int fclose(FILE *stream);
]]
local function log(premature, message)
local f = ffi.C.fopen("/some/path.log", "a+")
ffi.C.fprintf(f, message)
ffi.C.fclose(f)
end
local ok, err = ngx.timer.at(0, log, "Test Message")
if not ok then
ngx.log(ngx.ERR, "Failed to create timer: ", err)
end
';
The example above still opens and closes the file on every request, I will go ahead and implement the @agentzh suggestion for sharing the file handler.
On Friday, May 29, 2015 at 12:59:27 AM UTC-7, agentzh wrote:
Hello!
On Fri, May 29, 2015 at 3:55 PM, Vladislav Manchev wrote:
> It won't be possible to share a file descriptor between workers, so it's a
> dead end I think.
>
We don't need to share the log file descriptor across workers in the
first place. Even nginx's builtin logger does not do this. Thanks to
modern operating systems' atomic file appending feature ;)
> Also, not sure what's stopping you from logging to a third party file on
> disk with lua-resty-logger-socket. You can just log to a localhost syslog-ng
> instance (although I never tried that).
>
Sure, AFAIK, syslog-ng server can further log into the file system on
our behalf.
Regards,
-agentzh