Skip to content

Commit db9ef24

Browse files
authored
Merge pull request #43 from rushitote/master
feat: Added logging module for logging to console/file
2 parents 9a3493f + 330d5a2 commit db9ef24

File tree

9 files changed

+126
-35
lines changed

9 files changed

+126
-35
lines changed

spec/model/model_spec.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,13 @@ describe("model tests", function()
183183
assert.are.same(res, filteredRules)
184184

185185
end)
186+
187+
it("test printPolicy and printModel", function ()
188+
local m = Model:new()
189+
m:loadModel(basic_path)
190+
assert.has_no.errors(function ()
191+
m:printModel()
192+
m:printPolicy()
193+
end)
194+
end)
186195
end)

spec/persist/saved_policy.csv

Lines changed: 0 additions & 5 deletions
This file was deleted.

spec/rbac/role_manager_spec.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,26 @@ describe("DefaultRoleManager tests", function ()
273273
assert.are.same(rm:createRole("g1"):toString(), "g1 < g3")
274274
assert.are.same(rm:createRole("u4"):toString(), "u4 < g2, g3")
275275
end)
276+
277+
it("test printRoles", function ()
278+
local rm = DefaultRoleManager:new(10)
279+
rm:addLink("u1", "g1")
280+
rm:addLink("u2", "g1")
281+
rm:addLink("u3", "g2")
282+
rm:addLink("u4", "g2")
283+
rm:addLink("u4", "g3")
284+
rm:addLink("g1", "g3")
285+
--[[
286+
# Current role inheritance tree:
287+
# g3 g2
288+
# / \ / \
289+
# g1 u4 u3
290+
# / \
291+
# u1 u2
292+
]]
293+
294+
assert.has_no.errors(function ()
295+
rm:printRoles()
296+
end)
297+
end)
276298
end)

spec/util/log_spec.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--Copyright 2021 The casbin Authors. All Rights Reserved.
2+
--
3+
--Licensed under the Apache License, Version 2.0 (the "License");
4+
--you may not use this file except in compliance with the License.
5+
--You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
--Unless required by applicable law or agreed to in writing, software
10+
--distributed under the License is distributed on an "AS IS" BASIS,
11+
--WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
--See the License for the specific language governing permissions and
13+
--limitations under the License.
14+
15+
local log = require("src.util.Log")
16+
local path = os.getenv("PWD") or io.popen("cd"):read()
17+
path = path .. "/testLogFile.log"
18+
19+
describe("log tests", function ()
20+
21+
it("test console logger", function ()
22+
local logger = Log:getLogger()
23+
assert.has_no.errors(function ()
24+
logger:info("logging to console")
25+
end)
26+
end)
27+
28+
it("test file logger", function ()
29+
local logger = Log:getFileLogger(path)
30+
logger:info("new log started")
31+
assert.has_no.errors(function ()
32+
io.open(path, "r")
33+
end)
34+
end)
35+
36+
it("test filePath error", function ()
37+
assert.has_error(function ()
38+
local logger = Log:getFileLogger()
39+
end)
40+
end)
41+
end)

src/model/Model.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,11 @@ end
173173

174174
-- * printModel prints the model to the log.
175175
function Model:printModel()
176-
Util.logPrint("Model:")
176+
self.logger:info("Model: \n")
177177
for k,v in pairs(self.model) do
178178
for k2, v2 in pairs(v) do
179-
Util.logPrintf("%s.%s: %s", k, k2, v2)
179+
self.logger:info("[%s.%s]:", k, k2)
180+
self.logger:info(v2)
180181
end
181182
end
182183
end

src/model/Policy.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
require "src/model/Assertion"
1616
require "src/util/Util"
17+
require "src/util/Log"
1718

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

2122
function Policy:new()
2223
local o = {}
24+
o.logger = Log:getLogger()
2325
setmetatable(o, self)
2426
self.__index = self
2527
return o
@@ -42,16 +44,18 @@ end
4244
* printPolicy prints the policy to log.
4345
]]
4446
function Policy:printPolicy()
45-
Util.logPrint("Policy:")
47+
self.logger:info("Policy: \n")
4648
if self.model["p"] then
4749
for k, ast in pairs(self.model["p"]) do
48-
Util.logPrint(k .. ":" .. ast.value .. ":" .. ast.policy)
50+
self.logger:info("%s: %s:", k, ast.value)
51+
self.logger:info(ast.policy)
4952
end
5053
end
5154

5255
if self.model["g"] then
5356
for k, ast in pairs(self.model["g"]) do
54-
Util.logPrint(k .. ":" .. ast.value .. ":" .. ast.policy)
57+
self.logger:info("%s: %s:", k, ast.value)
58+
self.logger:info(ast.policy)
5559
end
5660
end
5761
end

src/rbac/DefaultRoleManager.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
--limitations under the License.
1414

1515
require "src/rbac/Role"
16+
require "src/util/Log"
1617

1718
DefaultRoleManager = {
1819
maxHierarchyLevel = 0
@@ -38,6 +39,7 @@ function DefaultRoleManager:new(maxHierarchyLevel, matchingFunc, domainMatchingF
3839
local o = {}
3940
setmetatable(o, self)
4041
self.__index = self
42+
o.logger = Log:getLogger()
4143
o.allRoles = {}
4244
o.maxHierarchyLevel = maxHierarchyLevel
4345
o.matchingFunc = matchingFunc
@@ -222,13 +224,15 @@ end
222224

223225
-- printRoles prints all the roles to log.
224226
function DefaultRoleManager:printRoles(name, ...)
225-
local lines = {}
227+
228+
self.logger:info("Roles: ")
226229
for _, role in pairs(self.allRoles) do
227230
local text = role:toString()
228-
if text then table.insert(lines, text) end
231+
if text then
232+
self.logger:info(text)
233+
end
229234
end
230235

231-
-- TODO: add logger here
232236
end
233237

234238
return DefaultRoleManager

src/util/Log.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--Copyright 2021 The casbin Authors. All Rights Reserved.
2+
--
3+
--Licensed under the Apache License, Version 2.0 (the "License");
4+
--you may not use this file except in compliance with the License.
5+
--You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
--Unless required by applicable law or agreed to in writing, software
10+
--distributed under the License is distributed on an "AS IS" BASIS,
11+
--WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
--See the License for the specific language governing permissions and
13+
--limitations under the License.
14+
15+
local Logging = require "logging"
16+
local fileLogging = require "logging.file"
17+
18+
-- The logging module for logging to console or any file
19+
Log = {}
20+
21+
-- returns logger function for logging to console
22+
function Log:getLogger()
23+
local logger = Logging.new(function(self, level, message)
24+
print(level, message)
25+
return true
26+
end)
27+
return logger
28+
end
29+
30+
-- returns logger function for logging to file and @param: filePath = path of the log file
31+
function Log:getFileLogger(filePath)
32+
if not filePath then
33+
error("no filePath for logger provided")
34+
end
35+
local logger = fileLogging(filePath)
36+
return logger
37+
end

src/util/Util.lua

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,8 @@
1313
--limitations under the License.
1414

1515
-- Utility Functions for lua-casbin
16-
local logging = require("logging")
17-
1816
Util = {}
1917

20-
Util.logger = logging.new(function(self, level, message)
21-
print(level, message)
22-
return true
23-
end)
24-
25-
-- Whether to print logs or not.
26-
Util.enableLog = true
27-
2818
-- arrayToString convert table of strings to one string
2919
function Util.arrayToString(rule)
3020
local str = ""
@@ -97,18 +87,6 @@ function Util.removeComments(str)
9787
return Util.trim(str)
9888
end
9989

100-
function Util.logPrint(v)
101-
if enableLog then
102-
Util.logger:info(v)
103-
end
104-
end
105-
106-
function Util.logPrintf(format, ...)
107-
if enableLog then
108-
Util.logger.info(format, ...)
109-
end
110-
end
111-
11290
function Util.arrayEquals(a, b)
11391
if #a ~= #b then
11492
return false

0 commit comments

Comments
 (0)