localLOG=require"log".new(
-- maximum log level"trace",
-- Writerrequire'log.writer.list'.new( -- multi writers:require'log.writer.console.color'.new(), -- * console colorrequire'log.writer.file.roll'.new( -- * roll files'./logs', -- log dir'events.log', -- current log name10, -- count files10*1024*1024-- max file size in bytes
)
),
-- Formatterrequire"log.formatter.concat".new()
)
LOG.error("some", "error")localLOG=require"log".new(
require"log.writer.async.zmq".new(
'inproc://async.logger',
function() -- calls from separate thread/Lua statereturnrequire'log.writer.file.by_day'.new(
'./logs', 'events.log', 5000
)
end
)
)
LOG.error("some error")localhost=arg[1] or'127.0.0.1'localport=arg[2] or514-- this code run in separate thread.localInitWriter=[[ return require 'log.writer.list'.new( -- multi writers: require 'log.writer.console.color'.new(), -- * console color require 'log.writer.net.zmq'.new('%{zmq_cnn_host}'), -- * zmq pub socket require "log.writer.format".new( -- * syslog over udp require "log.logformat.syslog".new("user"), require 'log.writer.net.udp'.new('%{udp_cnn_host}', %{udp_cnn_port}) ) )]]-- create async writer and run new work thread.-- communicate with work thread using zmq librarylocalwriter=require"log.writer.async.zmq".new(
'inproc://async.logger', InitWriter:gsub('%%{(.-)}', {
zmq_cnn_host='tcp://' ..host..':' ..port;
udp_cnn_host=host;
udp_cnn_port=port;
})
)
-- create new loggerlocalLOG=require"log".new(writer)
require"socket".sleep(0.5) -- net.zmq need time to connectLOG.fatal("can not allocate memory")
LOG.error("file not found")
LOG.warning("cache server is not started")
LOG.info("new message is received")
LOG.notice("message has 2 file")
print("Press enter ...") io.flush() io.read()This example show how to send logs in 2 separate destination with 2 formats. Write to stdout as whell as to remote syslog server. In fact here no async on Lua side. Application write to network directly from main thread. But it is possible use one or more work threads that will do this.
-- Buld writer with 2 destinationslocalwriter=require"log.writer.list".new(
require"log.writer.format".new(
-- explicit set logformat to stdout writerrequire"log.logformat.default".new(), require"log.writer.stdout".new()
),
-- define network writer.-- This writer has no explicit format so it will-- use one defined for logger.require"log.writer.net.udp".new('127.0.0.1', 514)
)
localfunctionSYSLOG_NEW(level, ...)
returnrequire"log".new(level, writer,
require"log.formatter.mix".new(),
require"log.logformat.syslog".new(...)
)
endlocalSYSLOG= {
-- Define first syslog logger with some settingsKERN=SYSLOG_NEW('trace', 'kern'),
-- Define second syslog logger with other settingsUSER=SYSLOG_NEW('trace', 'USER'),
}
SYSLOG.KERN.emerg ('emergency message')
SYSLOG.USER.alert ('alert message')- llthreads2
- or llthreads
- writer.net.udp
- llthreads2
- or llthreads
- writer.net.zmq
- LuaLanes - This is experemental writer
- ansicolors
- or lua-conio
- or cio (Windows only)