From 4ba0c9c83cfc6080200c95ee3c487ced0e0c9b7d Mon Sep 17 00:00:00 2001 From: Rushikesh Tote Date: Sat, 1 May 2021 18:04:14 +0530 Subject: [PATCH 1/2] feat: Added logging module for logging to console/file Signed-off-by: Rushikesh Tote --- spec/model/model_spec.lua | 9 ++++++++ spec/persist/saved_policy.csv | 5 ---- spec/rbac/role_manager_spec.lua | 22 ++++++++++++++++++ spec/util/log_spec.lua | 41 +++++++++++++++++++++++++++++++++ src/model/Model.lua | 5 ++-- src/model/Policy.lua | 10 +++++--- src/rbac/DefaultRoleManager.lua | 10 +++++--- src/util/Log.lua | 37 +++++++++++++++++++++++++++++ src/util/Util.lua | 22 ------------------ 9 files changed, 126 insertions(+), 35 deletions(-) delete mode 100644 spec/persist/saved_policy.csv create mode 100644 spec/util/log_spec.lua create mode 100644 src/util/Log.lua diff --git a/spec/model/model_spec.lua b/spec/model/model_spec.lua index 35c76d9..c8a4519 100644 --- a/spec/model/model_spec.lua +++ b/spec/model/model_spec.lua @@ -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) diff --git a/spec/persist/saved_policy.csv b/spec/persist/saved_policy.csv deleted file mode 100644 index 8479f3c..0000000 --- a/spec/persist/saved_policy.csv +++ /dev/null @@ -1,5 +0,0 @@ -p, alice, data1, read -p, bob, data2, write -p, data2_admin, data2, read -p, data2_admin, data2, write -g, alice, data2_admin diff --git a/spec/rbac/role_manager_spec.lua b/spec/rbac/role_manager_spec.lua index 12a25c1..7cb55f2 100644 --- a/spec/rbac/role_manager_spec.lua +++ b/spec/rbac/role_manager_spec.lua @@ -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) diff --git a/spec/util/log_spec.lua b/spec/util/log_spec.lua new file mode 100644 index 0000000..ed7db1e --- /dev/null +++ b/spec/util/log_spec.lua @@ -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) diff --git a/src/model/Model.lua b/src/model/Model.lua index 391c01a..0d96701 100644 --- a/src/model/Model.lua +++ b/src/model/Model.lua @@ -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 diff --git a/src/model/Policy.lua b/src/model/Policy.lua index 0c26013..d87d125 100644 --- a/src/model/Policy.lua +++ b/src/model/Policy.lua @@ -14,12 +14,14 @@ require "src/model/Assertion" require "src/util/Util" +require "src/util/Log" -- model's struct is map> Policy = {} function Policy:new() local o = {} + o.logger = Log:getLogger() setmetatable(o, self) self.__index = self return o @@ -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 diff --git a/src/rbac/DefaultRoleManager.lua b/src/rbac/DefaultRoleManager.lua index 0e0a37f..ba8c00f 100644 --- a/src/rbac/DefaultRoleManager.lua +++ b/src/rbac/DefaultRoleManager.lua @@ -13,6 +13,7 @@ --limitations under the License. require "src/rbac/Role" +require "src/util/Log" DefaultRoleManager = { maxHierarchyLevel = 0 @@ -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 @@ -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 diff --git a/src/util/Log.lua b/src/util/Log.lua new file mode 100644 index 0000000..76d7a04 --- /dev/null +++ b/src/util/Log.lua @@ -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. + +require "logging" +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 = logging.file(filePath) + return logger +end diff --git a/src/util/Util.lua b/src/util/Util.lua index 28870c4..cff1c41 100644 --- a/src/util/Util.lua +++ b/src/util/Util.lua @@ -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 = "" @@ -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 From 330d5a2533da229e6379aa68e8bd16750f24c0dd Mon Sep 17 00:00:00 2001 From: Rushikesh Tote Date: Sat, 1 May 2021 18:45:01 +0530 Subject: [PATCH 2/2] fix: error with other lua versions Signed-off-by: Rushikesh Tote --- src/util/Log.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/util/Log.lua b/src/util/Log.lua index 76d7a04..0a36ea6 100644 --- a/src/util/Log.lua +++ b/src/util/Log.lua @@ -12,15 +12,15 @@ --See the License for the specific language governing permissions and --limitations under the License. -require "logging" -require "logging.file" +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) + local logger = Logging.new(function(self, level, message) print(level, message) return true end) @@ -32,6 +32,6 @@ function Log:getFileLogger(filePath) if not filePath then error("no filePath for logger provided") end - local logger = logging.file(filePath) + local logger = fileLogging(filePath) return logger end