Skip to content

Commit

Permalink
feat(rewrite) allow to set log lvl and format from cassandra
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Dec 12, 2015
1 parent a7f321a commit 67dbaa9
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
unused_args = false
redefined = false
globals = {"ngx", "describe", "setup", "teardown", "it", "pending", "before_each", "after_each", "finally", "spy"}
globals = {"ngx", "describe", "setup", "teardown", "it", "pending", "before_each", "after_each", "finally", "spy", "mock"}
14 changes: 7 additions & 7 deletions spec/integration/cassandra_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local cassandra = require "cassandra"
local LOG_LVL = "ERR"

-- Define log level for tests
utils.set_log_lvl(LOG_LVL)
cassandra.set_log_level(LOG_LVL)

local _shm = "cassandra_specs"
local _hosts = utils.hosts
Expand All @@ -36,9 +36,9 @@ describe("spawn_cluster()", function()
end
end)
it("should iterate over contact_points to find an entrance into the cluster", function()
utils.set_log_lvl("QUIET")
cassandra.set_log_level("QUIET")
finally(function()
utils.set_log_lvl(LOG_LVL)
cassandra.set_log_level(LOG_LVL)
end)

local contact_points = {"0.0.0.1", "0.0.0.2", "0.0.0.3"}
Expand All @@ -52,9 +52,9 @@ describe("spawn_cluster()", function()
assert.True(ok)
end)
it("should accept a custom port for given hosts", function()
utils.set_log_lvl("QUIET")
cassandra.set_log_level("QUIET")
finally(function()
utils.set_log_lvl(LOG_LVL)
cassandra.set_log_level(LOG_LVL)
end)

local contact_points = {}
Expand All @@ -70,9 +70,9 @@ describe("spawn_cluster()", function()
assert.equal("NoHostAvailableError", err.type)
end)
it("should accept a custom port through an option", function()
utils.set_log_lvl("QUIET")
cassandra.set_log_level("QUIET")
finally(function()
utils.set_log_lvl(LOG_LVL)
cassandra.set_log_level(LOG_LVL)
end)

local ok, err = cassandra.spawn_cluster({
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/cql_types_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local _hosts = utils.hosts
local _keyspace = "resty_cassandra_cql_types_specs"

-- Define log level for tests
utils.set_log_lvl("ERR")
cassandra.set_log_level("ERR")

describe("CQL types integration", function()
local session
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/error_handling_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local cassandra = require "cassandra"
local LOG_LVL = "ERR"

-- Define log level for tests
utils.set_log_lvl(LOG_LVL)
cassandra.set_log_level(LOG_LVL)

local _shm = "cassandra_error_specs"
local _hosts = utils.hosts
Expand Down
5 changes: 0 additions & 5 deletions spec/spec_utils.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local say = require "say"
local log = require "cassandra.log"
local types = require "cassandra.types"
local assert = require "luassert.assert"
local string_utils = require "cassandra.utils.string"
Expand All @@ -13,10 +12,6 @@ end

local _M = {}

function _M.set_log_lvl(lvl)
log.set_lvl(lvl)
end

function _M.create_keyspace(session, keyspace)
local res, err = session:execute([[
CREATE KEYSPACE IF NOT EXISTS ]]..keyspace..[[
Expand Down
60 changes: 60 additions & 0 deletions spec/unit/cassandra_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
local log = require "cassandra.log"
local cassandra = require "cassandra"

describe("Casandra", function()
local p = log.print
setup(function()
spy.on(log, "set_lvl")
local l = mock(log.print, true)
log.print = l
end)
teardown(function()
log.set_lvl:revert()
log.print = p
end)
it("should have a default logging level", function()
local lvl = log.get_lvl()
assert.equal(3, lvl)
end)
it("should have a default format", function()
finally(function()
log.print:clear()
end)
log.err("hello")
assert.spy(log.print).was.called_with("ERR -- hello")
end)
describe("set_log_level", function()
it("should set the logging level when outside of ngx_lua", function()
finally(function()
log.print:clear()
end)

cassandra.set_log_level("INFO")
assert.spy(log.set_lvl).was.called_with("INFO")

-- INFO
log.err("hello world")
log.info("hello world")
assert.spy(log.print).was.called(2)

log.print:clear()
cassandra.set_log_level("ERR")

-- ERR
log.err("bye world")
log.info("bye world")
assert.spy(log.print).was.called(1)
end)
end)
describe("set_log_format", function()
it("should set the logging format when outside of ngx_lua", function()
finally(function()
log.print:clear()
end)

cassandra.set_log_format("Cassandra [%s]: %s")
log.err("some error")
assert.spy(log.print).was.called_with("Cassandra [ERR]: some error")
end)
end)
end)
10 changes: 9 additions & 1 deletion src/cassandra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ function RequestHandler:send_on_next_coordinator(request)
return nil, err
end

log.info("Acquired connection through load balancing policy: "..coordinator.address)
log.debug("Acquired connection through load balancing policy: "..coordinator.address)

return self:send(request)
end
Expand Down Expand Up @@ -1091,6 +1091,14 @@ function Cassandra.spawn_cluster(options)
return true
end

function Cassandra.set_log_level(lvl)
log.set_lvl(lvl)
end

function Cassandra.set_log_format(fmt)
log.set_format(fmt)
end

--- Type serializer shorthands.
-- When binding parameters to a query from `execute`, some
-- types cannot be infered automatically and will require manual
Expand Down
19 changes: 16 additions & 3 deletions src/cassandra/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ local ngx_get_phase = is_ngx and ngx.get_phase
local string_format = string.format
local print = print

-- ngx_lua levels redefinition for helpers and
-- when outside of ngx_lua.
-- ngx_lua levels redefinition when outside of ngx_lua.
local LEVELS = {
QUIET = 0,
ERR = 1,
Expand All @@ -21,6 +20,7 @@ local LEVELS = {

-- Default logging level when outside of ngx_lua.
local cur_lvl = LEVELS.INFO
local cur_fmt = "%s -- %s"

local log = {}

Expand All @@ -31,12 +31,25 @@ function log.set_lvl(lvl_name)
end
end

function log.get_lvl()
return cur_lvl
end

function log.set_format(fmt)
cur_fmt = fmt
end

-- Makes this module testable by spying on this function
function log.print(str)
print(str)
end

for lvl_name, lvl in pairs(LEVELS) do
log[lvl_name:lower()] = function(...)
if is_ngx and ngx_get_phase() ~= "init" then
ngx_log(ngx[lvl_name], ...)
elseif lvl <= cur_lvl then
print(string_format("%s -- %s", lvl_name, ...))
log.print(string_format(cur_fmt, lvl_name, ...))
end
end
end
Expand Down

0 comments on commit 67dbaa9

Please sign in to comment.