Skip to content

Commit

Permalink
Merge pull request #1184 from Mashape/fix/loggly
Browse files Browse the repository at this point in the history
fix (plugin/loggly) fix for issue 1183
  • Loading branch information
subnetmarco committed May 24, 2016
2 parents fa5baf2 + d821397 commit 37a302f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
6 changes: 2 additions & 4 deletions kong/plugins/loggly/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ local BasePlugin = require "kong.plugins.base_plugin"
local basic_serializer = require "kong.plugins.log-serializers.basic"
local cjson = require "cjson"


local LogglyLogHandler = BasePlugin:extend()

LogglyLogHandler.PRIORITY = 1


local os_date = os.date
local tostring = tostring
local ngx_log = ngx.log
Expand Down Expand Up @@ -94,9 +92,9 @@ local function log(premature, conf, message)
if premature then return end

if message.response.status >= 500 then
return decide_severity(conf.log_level, conf.server_errors_severity, message)
return decide_severity(conf, conf.server_errors_severity, message)
elseif message.response.status >= 400 then
return decide_severity(conf.log_level, conf.client_errors_severity, message)
return decide_severity(conf, conf.client_errors_severity, message)
else
return decide_severity(conf, conf.successful_severity, message)
end
Expand Down
65 changes: 63 additions & 2 deletions spec/plugins/loggly/log_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ local cjson = require "cjson"
local spec_helper = require "spec.spec_helpers"
local http_client = require "kong.tools.http_client"

local STUB_GET_URL = spec_helper.STUB_GET_URL
local PROXY_URL = spec_helper.PROXY_URL
local STUB_GET_URL = PROXY_URL.."/request"

local UDP_PORT = spec_helper.find_port()

Expand All @@ -15,7 +16,7 @@ describe("Logging Plugins", function()
{ request_host = "logging.com", upstream_url = "http://mockbin.com" },
{ request_host = "logging1.com", upstream_url = "http://mockbin.com" },
{ request_host = "logging2.com", upstream_url = "http://mockbin.com" },
{ request_host = "logging3.com", upstream_url = "http://mockbin.com" }
{ request_host = "logging3.com", upstream_url = "http://mockbin.com" },
},
plugin = {
{ name = "loggly", config = { host = "127.0.0.1", port = UDP_PORT, key = "123456789", log_level = "info",
Expand Down Expand Up @@ -111,6 +112,66 @@ describe("Logging Plugins", function()
local pri = string.sub(res,2,3)
assert.are.equal("14", pri)

local message = {}
for w in string.gmatch(res,"{.*}") do
table.insert(message, w)
end
local log_message = cjson.decode(message[1])
assert.are.same("127.0.0.1", log_message.client_ip)
end)
it("should log to UDP when severity and log level are default values and response status is 200", function()
local thread = spec_helper.start_udp_server(UDP_PORT) -- Starting the mock TCP server

local _, status = http_client.get(PROXY_URL, nil, { host = "logging3.com" })
assert.are.equal(200, status)

local ok, res = thread:join()
assert.truthy(ok)
assert.truthy(res)

local pri = string.sub(res,2,3)
assert.are.equal("14", pri)

local message = {}
for w in string.gmatch(res,"{.*}") do
table.insert(message, w)
end
local log_message = cjson.decode(message[1])
assert.are.same("127.0.0.1", log_message.client_ip)
end)
it("should log to UDP when severity and log level are default values and response status is 401", function()
local thread = spec_helper.start_udp_server(UDP_PORT) -- Starting the mock TCP server

local _, status = http_client.get(PROXY_URL.."/status/401/", nil, { host = "logging3.com" })
assert.are.equal(401, status)

local ok, res = thread:join()
assert.truthy(ok)
assert.truthy(res)

local pri = string.sub(res,2,3)
assert.are.equal("14", pri)

local message = {}
for w in string.gmatch(res,"{.*}") do
table.insert(message, w)
end
local log_message = cjson.decode(message[1])
assert.are.same("127.0.0.1", log_message.client_ip)
end)
it("should log to UDP when severity and log level are default values and response status is 500", function()
local thread = spec_helper.start_udp_server(UDP_PORT) -- Starting the mock TCP server

local _, status = http_client.get(PROXY_URL.."/status/500/", nil, { host = "logging3.com" })
assert.are.equal(500, status)

local ok, res = thread:join()
assert.truthy(ok)
assert.truthy(res)

local pri = string.sub(res,2,3)
assert.are.equal("14", pri)

local message = {}
for w in string.gmatch(res,"{.*}") do
table.insert(message, w)
Expand Down

0 comments on commit 37a302f

Please sign in to comment.