Skip to content
forked from moteus/lua-log

Asynchronous logging library for Lua 5.1/5.2

License

Notifications You must be signed in to change notification settings

agladysh/lua-log

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Asynchronous logging library for Lua 5.1/5.2


##Dependences## ###core###

  • LuaDate

###writer.async.udp###

###writer.async.zmq###

###writer.async.lane###

  • LuaLanes - This is experemental writer

###writer.console.color###

  • ansicolors
  • or lua-conio
  • or cio (Windows only)

###writer.file.by_day###

###writer.net.udp###

###writer.net.zmq###

###writer.net.smtp###


Usage

Write to file from separate thread.

local LOG = require "log".new(
  require "log.writer.async.zmq".new(
    'inproc:https://async.logger',
    "return require 'log.writer.file.by_day'.new('./logs', 'events.log', 5000)"
  )
)

LOG.error("some error")

More complex example

local host = arg[1] or '127.0.0.1'
local port = arg[2] or 514

-- this code run in separate thread.
local InitWriter = [[
  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 library
local writer = require "log.writer.async.zmq".new(
  'inproc:https://async.logger', 
  InitWriter:gsub('%%{(.-)}', {
    zmq_cnn_host = 'tcp:https://' .. host .. ':' .. port;
    udp_cnn_host = host;
    udp_cnn_port = port;
  })
)

-- create new logger
local LOG = require"log".new(writer)

require "socket".sleep(0.5) -- net.zmq need time to connect

LOG.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()

About

Asynchronous logging library for Lua 5.1/5.2

Resources

License

Stars

Watchers

Forks

Packages

No packages published