level.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package audit
  2. import (
  3. "strings"
  4. "udeServer/internal/response"
  5. )
  6. // ChooseLevelByBusinessCode maps a unified-response business code to a log
  7. // level. There is no gap for 300000-500000 like in sdf-serve because
  8. // udeServer always answers with HTTP 200 and encodes errors in the body.
  9. // Unknown codes are treated as warnings except the server-error code.
  10. func ChooseLevelByBusinessCode(code int64) string {
  11. switch code {
  12. case response.CodeOK:
  13. return "INFO"
  14. case response.CodeServerError:
  15. return "ERROR"
  16. default:
  17. // 100001 参数错误 / 100002 不存在 以及未来新增的业务错误码
  18. return "WARN"
  19. }
  20. }
  21. // SaveToDBByLogLevel decides whether a log entry is persisted: the stored
  22. // entry is kept when its level is at or above the configured level. An
  23. // empty or unrecognized configured level falls back to INFO so that the
  24. // audit chain keeps working before t_config is populated.
  25. func SaveToDBByLogLevel(configLevel, actualLevel string) bool {
  26. // DEBUG < INFO < WARN < ERROR
  27. levelOrder := map[string]int{
  28. "DEBUG": 1,
  29. "INFO": 2,
  30. "WARN": 3,
  31. "ERROR": 4,
  32. }
  33. effective := strings.ToUpper(strings.TrimSpace(configLevel))
  34. if _, ok := levelOrder[effective]; !ok {
  35. effective = "INFO"
  36. }
  37. actual, ok := levelOrder[actualLevel]
  38. if !ok {
  39. return false
  40. }
  41. return levelOrder[effective] <= actual
  42. }