• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

moteus/lua-log: Asynchronous logging library for Lua

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

moteus/lua-log

开源软件地址(OpenSource Url):

https://github.com/moteus/lua-log

开源编程语言(OpenSource Language):

Lua 72.3%

开源软件介绍(OpenSource Introduction):

Logging library for Lua 5.1/5.2

Build Status Build Status Licence


Usage

Write to roll file and to console

local LOG = require "log".new(
  -- maximum log level
  "trace",

  -- Writer
  require 'log.writer.list'.new(               -- multi writers:
    require 'log.writer.console.color'.new(),  -- * console color
    require 'log.writer.file.roll'.new(        -- * roll files
      './logs',                                --   log dir
      'events.log',                            --   current log name
      10,                                      --   count files
      10*1024*1024                             --   max file size in bytes
    )
  ),

  -- Formatter
  require "log.formatter.concat".new()
)

LOG.error("some", "error")

Write to file from separate thread

local LOG = require "log".new(
  require "log.writer.async.zmq".new(
    'inproc://async.logger',
    function() -- calls from separate thread/Lua state
      return require 'log.writer.file.by_day'.new(
        './logs', 'events.log', 5000
      )
    end
  )
)

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://async.logger', 
  InitWriter:gsub('%%{(.-)}', {
    zmq_cnn_host = 'tcp://' .. 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()

Sendout logs to separate process/host

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 destinations
local writer = require "log.writer.list".new(
  require "log.writer.format".new(
    -- explicit set logformat to stdout writer
    require "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)
)

local function SYSLOG_NEW(level, ...)
  return require "log".new(level, writer,
    require "log.formatter.mix".new(),
    require "log.logformat.syslog".new(...)
  )
end

local SYSLOG = {
  -- Define first syslog logger with some settings
  KERN = SYSLOG_NEW('trace', 'kern'),
  
  -- Define second syslog logger with other settings
  USER = SYSLOG_NEW('trace', 'USER'),
}

SYSLOG.KERN.emerg ('emergency message')
SYSLOG.USER.alert ('alert message')

Dependences

core

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




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
prettier/plugin-lua: Prettier Lua Plugin (WIP)发布时间:2022-08-16
下一篇:
Olivine-Labs/lua-jwt: JSON Web Token library发布时间:2022-08-16
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap