1
0

log.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package mycasbin
  2. import (
  3. "sync/atomic"
  4. "gogs.baozhida.cn/zoie/OAuth-core/logger"
  5. )
  6. // Logger is the implementation for a Logger using golang log.
  7. type Logger struct {
  8. enable int32
  9. }
  10. // EnableLog controls whether print the message.
  11. func (l *Logger) EnableLog(enable bool) {
  12. i := 0
  13. if enable {
  14. i = 1
  15. }
  16. atomic.StoreInt32(&(l.enable), int32(i))
  17. }
  18. // IsEnabled returns if logger is enabled.
  19. func (l *Logger) IsEnabled() bool {
  20. return atomic.LoadInt32(&(l.enable)) != 0
  21. }
  22. // LogModel log info related to model.
  23. func (l *Logger) LogModel(model [][]string) {
  24. var str string
  25. for i := range model {
  26. for j := range model[i] {
  27. str += " " + model[i][j]
  28. }
  29. str += "\n"
  30. }
  31. logger.DefaultLogger.Log(logger.InfoLevel, str)
  32. }
  33. // LogEnforce log info related to enforce.
  34. func (l *Logger) LogEnforce(matcher string, request []interface{}, result bool, explains [][]string) {
  35. logger.DefaultLogger.Fields(map[string]interface{}{
  36. "matcher": matcher,
  37. "request": request,
  38. "result": result,
  39. "explains": explains,
  40. }).Log(logger.InfoLevel, nil)
  41. }
  42. // LogRole log info related to role.
  43. func (l *Logger) LogRole(roles []string) {
  44. logger.DefaultLogger.Fields(map[string]interface{}{
  45. "roles": roles,
  46. })
  47. }
  48. // LogPolicy log info related to policy.
  49. func (l *Logger) LogPolicy(policy map[string][][]string) {
  50. data := make(map[string]interface{}, len(policy))
  51. for k := range policy {
  52. data[k] = policy[k]
  53. }
  54. logger.DefaultLogger.Fields(data).Log(logger.InfoLevel, nil)
  55. }