parent
af52bc1471
commit
d5766f0661
@ -1,22 +0,0 @@
|
|||||||
-------------------------------------------------------------------------------
|
|
||||||
-- Prints logging information to console
|
|
||||||
--
|
|
||||||
-- @author Thiago Costa Ponte (thiago@ideais.com.br)
|
|
||||||
--
|
|
||||||
-- @copyright 2004-2011 Kepler Project
|
|
||||||
--
|
|
||||||
-------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
require"logging"
|
|
||||||
|
|
||||||
function logging.console(logPattern)
|
|
||||||
|
|
||||||
return logging.new( function(self, level, message)
|
|
||||||
io.stdout:write(logging.prepareLogMsg(logPattern, os.date(), level, message))
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
return logging.console
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
|||||||
-------------------------------------------------------------------------------
|
|
||||||
-- Emails logging information to the given recipient
|
|
||||||
--
|
|
||||||
-- @author Thiago Costa Ponte (thiago@ideais.com.br)
|
|
||||||
--
|
|
||||||
-- @copyright 2004-2011 Kepler Project
|
|
||||||
--
|
|
||||||
-------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
require"logging"
|
|
||||||
local smtp = require"socket.smtp"
|
|
||||||
|
|
||||||
function logging.email(params)
|
|
||||||
|
|
||||||
params = params or {}
|
|
||||||
params.headers = params.headers or {}
|
|
||||||
|
|
||||||
if params.from == nil then
|
|
||||||
return nil, "'from' parameter is required"
|
|
||||||
end
|
|
||||||
if params.rcpt == nil then
|
|
||||||
return nil, "'rcpt' parameter is required"
|
|
||||||
end
|
|
||||||
|
|
||||||
return logging.new( function(self, level, message)
|
|
||||||
local s = logging.prepareLogMsg(params.logPattern, os.date(), level, message)
|
|
||||||
if params.headers.subject then
|
|
||||||
params.headers.subject = logging.prepareLogMsg(params.headers.subject, os.date(), level, message)
|
|
||||||
end
|
|
||||||
local msg = { headers = params.headers, body = s }
|
|
||||||
params.source = smtp.message(msg)
|
|
||||||
|
|
||||||
local r, e = smtp.send(params)
|
|
||||||
if not r then
|
|
||||||
return nil, e
|
|
||||||
end
|
|
||||||
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
return logging.email
|
|
||||||
|
|
@ -1,55 +0,0 @@
|
|||||||
-------------------------------------------------------------------------------
|
|
||||||
-- Saves logging information in a file
|
|
||||||
--
|
|
||||||
-- @author Thiago Costa Ponte (thiago@ideais.com.br)
|
|
||||||
--
|
|
||||||
-- @copyright 2004-2011 Kepler Project
|
|
||||||
--
|
|
||||||
-------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
require"logging"
|
|
||||||
|
|
||||||
local lastFileNameDatePattern
|
|
||||||
local lastFileHandler
|
|
||||||
|
|
||||||
local openFileLogger = function (filename, datePattern)
|
|
||||||
|
|
||||||
local filename = string.format(filename, os.date(datePattern))
|
|
||||||
if (lastFileNameDatePattern ~= filename) then
|
|
||||||
|
|
||||||
local f = io.open(filename, "a")
|
|
||||||
if (f) then
|
|
||||||
f:setvbuf ("line")
|
|
||||||
lastFileNameDatePattern = filename
|
|
||||||
lastFileHandler = f
|
|
||||||
return f
|
|
||||||
else
|
|
||||||
return nil, string.format("file `%s' could not be opened for writing", filename)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
return lastFileHandler
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
function logging.file(filename, datePattern, logPattern)
|
|
||||||
|
|
||||||
if type(filename) ~= "string" then
|
|
||||||
filename = "lualogging.log"
|
|
||||||
end
|
|
||||||
|
|
||||||
return logging.new( function(self, level, message)
|
|
||||||
|
|
||||||
local f, msg = openFileLogger(filename, datePattern)
|
|
||||||
if not f then
|
|
||||||
return nil, msg
|
|
||||||
end
|
|
||||||
local s = logging.prepareLogMsg(logPattern, os.date(), level, message)
|
|
||||||
f:write(s)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
return logging.file
|
|
||||||
|
|
@ -1,81 +0,0 @@
|
|||||||
---------------------------------------------------------------------------
|
|
||||||
-- RollingFileAppender is a FileAppender that rolls over the logfile
|
|
||||||
-- once it has reached a certain size limit. It also mantains a
|
|
||||||
-- maximum number of log files.
|
|
||||||
--
|
|
||||||
-- @author Tiago Cesar Katcipis (tiagokatcipis@gmail.com)
|
|
||||||
--
|
|
||||||
-- @copyright 2004-2007 Kepler Project
|
|
||||||
---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
require"logging"
|
|
||||||
|
|
||||||
|
|
||||||
local function openFile(self)
|
|
||||||
self.file = io.open(self.filename, "a")
|
|
||||||
if not self.file then
|
|
||||||
return nil, string.format("file `%s' could not be opened for writing", self.filename)
|
|
||||||
end
|
|
||||||
self.file:setvbuf ("line")
|
|
||||||
return self.file
|
|
||||||
end
|
|
||||||
|
|
||||||
local rollOver = function (self)
|
|
||||||
|
|
||||||
for i = self.maxIndex - 1, 1, -1 do
|
|
||||||
-- files may not exist yet, lets ignore the possible errors.
|
|
||||||
os.rename(self.filename.."."..i, self.filename.."."..i+1)
|
|
||||||
end
|
|
||||||
|
|
||||||
self.file:close()
|
|
||||||
self.file = nil
|
|
||||||
|
|
||||||
local _, msg = os.rename(self.filename, self.filename..".".."1")
|
|
||||||
|
|
||||||
if msg then
|
|
||||||
return nil, string.format("error %s on log rollover", msg)
|
|
||||||
end
|
|
||||||
|
|
||||||
return openFile(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local openRollingFileLogger = function (self)
|
|
||||||
|
|
||||||
if not self.file then
|
|
||||||
return openFile(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
local filesize = self.file:seek("end", 0)
|
|
||||||
|
|
||||||
if (filesize < self.maxSize) then
|
|
||||||
return self.file
|
|
||||||
end
|
|
||||||
|
|
||||||
return rollOver(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function logging.rolling_file(filename, maxFileSize, maxBackupIndex, logPattern)
|
|
||||||
|
|
||||||
if type(filename) ~= "string" then
|
|
||||||
filename = "lualogging.log"
|
|
||||||
end
|
|
||||||
|
|
||||||
local obj = { filename = filename,
|
|
||||||
maxSize = maxFileSize,
|
|
||||||
maxIndex = maxBackupIndex or 1
|
|
||||||
}
|
|
||||||
|
|
||||||
return logging.new( function(self, level, message)
|
|
||||||
|
|
||||||
local f, msg = openRollingFileLogger(obj)
|
|
||||||
if not f then
|
|
||||||
return nil, msg
|
|
||||||
end
|
|
||||||
local s = logging.prepareLogMsg(logPattern, os.date(), level, message)
|
|
||||||
f:write(s)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
)
|
|
||||||
end
|
|
@ -1,35 +0,0 @@
|
|||||||
-------------------------------------------------------------------------------
|
|
||||||
-- Sends the logging information through a socket using luasocket
|
|
||||||
--
|
|
||||||
-- @author Thiago Costa Ponte (thiago@ideais.com.br)
|
|
||||||
--
|
|
||||||
-- @copyright 2004-2011 Kepler Project
|
|
||||||
--
|
|
||||||
-------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
require"logging"
|
|
||||||
local socket = require"socket"
|
|
||||||
|
|
||||||
function logging.socket(address, port, logPattern)
|
|
||||||
|
|
||||||
return logging.new( function(self, level, message)
|
|
||||||
local s = logging.prepareLogMsg(logPattern, os.date(), level, message)
|
|
||||||
|
|
||||||
local socket, err = socket.connect(address, port)
|
|
||||||
if not socket then
|
|
||||||
return nil, err
|
|
||||||
end
|
|
||||||
|
|
||||||
local cond, err = socket:send(s)
|
|
||||||
if not cond then
|
|
||||||
return nil, err
|
|
||||||
end
|
|
||||||
socket:close()
|
|
||||||
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
return logging.socket
|
|
||||||
|
|
@ -1,64 +0,0 @@
|
|||||||
-------------------------------------------------------------------------------
|
|
||||||
-- Saves the logging information in a table using luasql
|
|
||||||
--
|
|
||||||
-- @author Thiago Costa Ponte (thiago@ideais.com.br)
|
|
||||||
--
|
|
||||||
-- @copyright 2004-2011 Kepler Project
|
|
||||||
--
|
|
||||||
-------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
require"logging"
|
|
||||||
|
|
||||||
function logging.sql(params)
|
|
||||||
|
|
||||||
params = params or {}
|
|
||||||
params.tablename = params.tablename or "LogTable"
|
|
||||||
params.logdatefield = params.logdatefield or "LogDate"
|
|
||||||
params.loglevelfield = params.loglevelfield or "LogLevel"
|
|
||||||
params.logmessagefield = params.logmessagefield or "LogMessage"
|
|
||||||
|
|
||||||
if params.connectionfactory == nil or type(params.connectionfactory) ~= "function" then
|
|
||||||
return nil, "No specified connection factory function"
|
|
||||||
end
|
|
||||||
|
|
||||||
local con, err
|
|
||||||
if params.keepalive then
|
|
||||||
con, err = params.connectionfactory()
|
|
||||||
end
|
|
||||||
|
|
||||||
return logging.new( function(self, level, message)
|
|
||||||
if (not params.keepalive) or (con == nil) then
|
|
||||||
con, err = params.connectionfactory()
|
|
||||||
if not con then
|
|
||||||
return nil, err
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local logDate = os.date("%Y-%m-%d %H:%M:%S")
|
|
||||||
local insert = string.format("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
|
|
||||||
params.tablename, params.logdatefield, params.loglevelfield,
|
|
||||||
params.logmessagefield, logDate, level, string.gsub(message, "'", "''"))
|
|
||||||
|
|
||||||
local ret, err = pcall(con.execute, con, insert)
|
|
||||||
if not ret then
|
|
||||||
con, err = params.connectionfactory()
|
|
||||||
if not con then
|
|
||||||
return nil, err
|
|
||||||
end
|
|
||||||
ret, err = con:execute(insert)
|
|
||||||
if not ret then
|
|
||||||
return nil, err
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if not params.keepalive then
|
|
||||||
con:close()
|
|
||||||
end
|
|
||||||
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
return logging.sql
|
|
||||||
|
|
Loading…
Reference in new issue