| // Copyright 2018 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. |
| |
| package log |
| |
| import ( |
| "fmt" |
| "log" |
| "strings" |
| ) |
| |
| // DefaultLogger is the implementation for a Logger using golang log. |
| type DefaultLogger struct { |
| enabled bool |
| } |
| |
| func (l *DefaultLogger) EnableLog(enable bool) { |
| l.enabled = enable |
| } |
| |
| func (l *DefaultLogger) IsEnabled() bool { |
| return l.enabled |
| } |
| |
| func (l *DefaultLogger) LogModel(model [][]string) { |
| if !l.enabled { |
| return |
| } |
| var str strings.Builder |
| str.WriteString("Model: ") |
| for _, v := range model { |
| str.WriteString(fmt.Sprintf("%v\n", v)) |
| } |
| |
| log.Println(str.String()) |
| } |
| |
| func (l *DefaultLogger) LogEnforce(matcher string, request []interface{}, result bool, explains [][]string) { |
| if !l.enabled { |
| return |
| } |
| |
| var reqStr strings.Builder |
| reqStr.WriteString("Request: ") |
| for i, rval := range request { |
| if i != len(request)-1 { |
| reqStr.WriteString(fmt.Sprintf("%v, ", rval)) |
| } else { |
| reqStr.WriteString(fmt.Sprintf("%v", rval)) |
| } |
| } |
| reqStr.WriteString(fmt.Sprintf(" ---> %t\n", result)) |
| |
| reqStr.WriteString("Hit Policy: ") |
| for i, pval := range explains { |
| if i != len(explains)-1 { |
| reqStr.WriteString(fmt.Sprintf("%v, ", pval)) |
| } else { |
| reqStr.WriteString(fmt.Sprintf("%v \n", pval)) |
| } |
| } |
| |
| log.Println(reqStr.String()) |
| } |
| |
| func (l *DefaultLogger) LogPolicy(policy map[string][][]string) { |
| if !l.enabled { |
| return |
| } |
| |
| var str strings.Builder |
| str.WriteString("Policy: ") |
| for k, v := range policy { |
| str.WriteString(fmt.Sprintf("%s : %v\n", k, v)) |
| } |
| |
| log.Println(str.String()) |
| } |
| |
| func (l *DefaultLogger) LogRole(roles []string) { |
| if !l.enabled { |
| return |
| } |
| |
| log.Println("Roles: ", strings.Join(roles, "\n")) |
| } |
| |
| func (l *DefaultLogger) LogError(err error, msg ...string) { |
| if !l.enabled { |
| return |
| } |
| log.Println(msg, err) |
| } |