You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.4 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package log
  5. import "strings"
  6. // These flags define which text to prefix to each log entry generated
  7. // by the Logger. Bits are or'ed together to control what's printed.
  8. // There is no control over the order they appear (the order listed
  9. // here) or the format they present (as described in the comments).
  10. // The prefix is followed by a colon only if more than time is stated
  11. // is specified. For example, flags Ldate | Ltime
  12. // produce, 2009/01/23 01:23:23 message.
  13. // The standard is:
  14. // 2009/01/23 01:23:23 ...a/logger/c/d.go:23:runtime.Caller() [I]: message
  15. const (
  16. Ldate = 1 << iota // the date in the local time zone: 2009/01/23
  17. Ltime // the time in the local time zone: 01:23:23
  18. Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime.
  19. Llongfile // full file name and line number: /a/logger/c/d.go:23
  20. Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
  21. Lfuncname // function name of the caller: runtime.Caller()
  22. Lshortfuncname // last part of the function name
  23. LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
  24. Llevelinitial // Initial character of the provided level in brackets eg. [I] for info
  25. Llevel // Provided level in brackets [INFO]
  26. // Last 20 characters of the filename
  27. Lmedfile = Lshortfile | Llongfile
  28. // LstdFlags is the initial value for the standard logger
  29. LstdFlags = Ldate | Ltime | Lmedfile | Lshortfuncname | Llevelinitial
  30. )
  31. var flagFromString = map[string]int{
  32. "none": 0,
  33. "date": Ldate,
  34. "time": Ltime,
  35. "microseconds": Lmicroseconds,
  36. "longfile": Llongfile,
  37. "shortfile": Lshortfile,
  38. "funcname": Lfuncname,
  39. "shortfuncname": Lshortfuncname,
  40. "utc": LUTC,
  41. "levelinitial": Llevelinitial,
  42. "level": Llevel,
  43. "medfile": Lmedfile,
  44. "stdflags": LstdFlags,
  45. }
  46. // FlagsFromString takes a comma separated list of flags and returns
  47. // the flags for this string
  48. func FlagsFromString(from string) int {
  49. flags := 0
  50. for _, flag := range strings.Split(strings.ToLower(from), ",") {
  51. f, ok := flagFromString[strings.TrimSpace(flag)]
  52. if ok {
  53. flags = flags | f
  54. }
  55. }
  56. return flags
  57. }