Skip to content

Commit

Permalink
Merge pull request #43 from rushitote/master
Browse files Browse the repository at this point in the history
feat: Added logging module for logging to console/file
  • Loading branch information
hsluoyz authored May 1, 2021
2 parents 9a3493f + 330d5a2 commit db9ef24
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 35 deletions.
9 changes: 9 additions & 0 deletions spec/model/model_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,13 @@ describe("model tests", function()
assert.are.same(res, filteredRules)

end)

it("test printPolicy and printModel", function ()
local m = Model:new()
m:loadModel(basic_path)
assert.has_no.errors(function ()
m:printModel()
m:printPolicy()
end)
end)
end)
5 changes: 0 additions & 5 deletions spec/persist/saved_policy.csv

This file was deleted.

22 changes: 22 additions & 0 deletions spec/rbac/role_manager_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,26 @@ describe("DefaultRoleManager tests", function ()
assert.are.same(rm:createRole("g1"):toString(), "g1 < g3")
assert.are.same(rm:createRole("u4"):toString(), "u4 < g2, g3")
end)

it("test printRoles", function ()
local rm = DefaultRoleManager:new(10)
rm:addLink("u1", "g1")
rm:addLink("u2", "g1")
rm:addLink("u3", "g2")
rm:addLink("u4", "g2")
rm:addLink("u4", "g3")
rm:addLink("g1", "g3")
--[[
# Current role inheritance tree:
# g3 g2
# / \ / \
# g1 u4 u3
# / \
# u1 u2
]]

assert.has_no.errors(function ()
rm:printRoles()
end)
end)
end)
41 changes: 41 additions & 0 deletions spec/util/log_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--Copyright 2021 The casbin Authors. All Rights Reserved.
--
--Licensed under the Apache License, Version 2.0 (the "License");
--you may not use this file except in compliance with the License.
--You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
--Unless required by applicable law or agreed to in writing, software
--distributed under the License is distributed on an "AS IS" BASIS,
--WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--See the License for the specific language governing permissions and
--limitations under the License.

local log = require("src.util.Log")
local path = os.getenv("PWD") or io.popen("cd"):read()
path = path .. "/testLogFile.log"

describe("log tests", function ()

it("test console logger", function ()
local logger = Log:getLogger()
assert.has_no.errors(function ()
logger:info("logging to console")
end)
end)

it("test file logger", function ()
local logger = Log:getFileLogger(path)
logger:info("new log started")
assert.has_no.errors(function ()
io.open(path, "r")
end)
end)

it("test filePath error", function ()
assert.has_error(function ()
local logger = Log:getFileLogger()
end)
end)
end)
5 changes: 3 additions & 2 deletions src/model/Model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,11 @@ end

-- * printModel prints the model to the log.
function Model:printModel()
Util.logPrint("Model:")
self.logger:info("Model: \n")
for k,v in pairs(self.model) do
for k2, v2 in pairs(v) do
Util.logPrintf("%s.%s: %s", k, k2, v2)
self.logger:info("[%s.%s]:", k, k2)
self.logger:info(v2)
end
end
end
Expand Down
10 changes: 7 additions & 3 deletions src/model/Policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

require "src/model/Assertion"
require "src/util/Util"
require "src/util/Log"

-- model's struct is map<string, map<string, Assertion>>
Policy = {}

function Policy:new()
local o = {}
o.logger = Log:getLogger()
setmetatable(o, self)
self.__index = self
return o
Expand All @@ -42,16 +44,18 @@ end
* printPolicy prints the policy to log.
]]
function Policy:printPolicy()
Util.logPrint("Policy:")
self.logger:info("Policy: \n")
if self.model["p"] then
for k, ast in pairs(self.model["p"]) do
Util.logPrint(k .. ":" .. ast.value .. ":" .. ast.policy)
self.logger:info("%s: %s:", k, ast.value)
self.logger:info(ast.policy)
end
end

if self.model["g"] then
for k, ast in pairs(self.model["g"]) do
Util.logPrint(k .. ":" .. ast.value .. ":" .. ast.policy)
self.logger:info("%s: %s:", k, ast.value)
self.logger:info(ast.policy)
end
end
end
Expand Down
10 changes: 7 additions & 3 deletions src/rbac/DefaultRoleManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
--limitations under the License.

require "src/rbac/Role"
require "src/util/Log"

DefaultRoleManager = {
maxHierarchyLevel = 0
Expand All @@ -38,6 +39,7 @@ function DefaultRoleManager:new(maxHierarchyLevel, matchingFunc, domainMatchingF
local o = {}
setmetatable(o, self)
self.__index = self
o.logger = Log:getLogger()
o.allRoles = {}
o.maxHierarchyLevel = maxHierarchyLevel
o.matchingFunc = matchingFunc
Expand Down Expand Up @@ -222,13 +224,15 @@ end

-- printRoles prints all the roles to log.
function DefaultRoleManager:printRoles(name, ...)
local lines = {}

self.logger:info("Roles: ")
for _, role in pairs(self.allRoles) do
local text = role:toString()
if text then table.insert(lines, text) end
if text then
self.logger:info(text)
end
end

-- TODO: add logger here
end

return DefaultRoleManager
37 changes: 37 additions & 0 deletions src/util/Log.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--Copyright 2021 The casbin Authors. All Rights Reserved.
--
--Licensed under the Apache License, Version 2.0 (the "License");
--you may not use this file except in compliance with the License.
--You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
--Unless required by applicable law or agreed to in writing, software
--distributed under the License is distributed on an "AS IS" BASIS,
--WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--See the License for the specific language governing permissions and
--limitations under the License.

local Logging = require "logging"
local fileLogging = require "logging.file"

-- The logging module for logging to console or any file
Log = {}

-- returns logger function for logging to console
function Log:getLogger()
local logger = Logging.new(function(self, level, message)
print(level, message)
return true
end)
return logger
end

-- returns logger function for logging to file and @param: filePath = path of the log file
function Log:getFileLogger(filePath)
if not filePath then
error("no filePath for logger provided")
end
local logger = fileLogging(filePath)
return logger
end
22 changes: 0 additions & 22 deletions src/util/Util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,8 @@
--limitations under the License.

-- Utility Functions for lua-casbin
local logging = require("logging")

Util = {}

Util.logger = logging.new(function(self, level, message)
print(level, message)
return true
end)

-- Whether to print logs or not.
Util.enableLog = true

-- arrayToString convert table of strings to one string
function Util.arrayToString(rule)
local str = ""
Expand Down Expand Up @@ -97,18 +87,6 @@ function Util.removeComments(str)
return Util.trim(str)
end

function Util.logPrint(v)
if enableLog then
Util.logger:info(v)
end
end

function Util.logPrintf(format, ...)
if enableLog then
Util.logger.info(format, ...)
end
end

function Util.arrayEquals(a, b)
if #a ~= #b then
return false
Expand Down

0 comments on commit db9ef24

Please sign in to comment.