Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

colors_router.go 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 (
  6. "time"
  7. )
  8. var statusToColor = map[int][]byte{
  9. 100: ColorBytes(Bold),
  10. 200: ColorBytes(FgGreen),
  11. 300: ColorBytes(FgYellow),
  12. 304: ColorBytes(FgCyan),
  13. 400: ColorBytes(Bold, FgRed),
  14. 401: ColorBytes(Bold, FgMagenta),
  15. 403: ColorBytes(Bold, FgMagenta),
  16. 500: ColorBytes(Bold, BgRed),
  17. }
  18. // ColoredStatus addes colors for HTTP status
  19. func ColoredStatus(status int, s ...string) *ColoredValue {
  20. color, ok := statusToColor[status]
  21. if !ok {
  22. color, ok = statusToColor[(status/100)*100]
  23. }
  24. if !ok {
  25. color = fgBoldBytes
  26. }
  27. if len(s) > 0 {
  28. return NewColoredValueBytes(s[0], &color)
  29. }
  30. return NewColoredValueBytes(status, &color)
  31. }
  32. var methodToColor = map[string][]byte{
  33. "GET": ColorBytes(FgBlue),
  34. "POST": ColorBytes(FgGreen),
  35. "DELETE": ColorBytes(FgRed),
  36. "PATCH": ColorBytes(FgCyan),
  37. "PUT": ColorBytes(FgYellow, Faint),
  38. "HEAD": ColorBytes(FgBlue, Faint),
  39. }
  40. // ColoredMethod addes colors for HtTP methos on log
  41. func ColoredMethod(method string) *ColoredValue {
  42. color, ok := methodToColor[method]
  43. if !ok {
  44. return NewColoredValueBytes(method, &fgBoldBytes)
  45. }
  46. return NewColoredValueBytes(method, &color)
  47. }
  48. var (
  49. durations = []time.Duration{
  50. 10 * time.Millisecond,
  51. 100 * time.Millisecond,
  52. 1 * time.Second,
  53. 5 * time.Second,
  54. 10 * time.Second,
  55. }
  56. durationColors = [][]byte{
  57. ColorBytes(FgGreen),
  58. ColorBytes(Bold),
  59. ColorBytes(FgYellow),
  60. ColorBytes(FgRed, Bold),
  61. ColorBytes(BgRed),
  62. }
  63. wayTooLong = ColorBytes(BgMagenta)
  64. )
  65. // ColoredTime addes colors for time on log
  66. func ColoredTime(duration time.Duration) *ColoredValue {
  67. for i, k := range durations {
  68. if duration < k {
  69. return NewColoredValueBytes(duration, &durationColors[i])
  70. }
  71. }
  72. return NewColoredValueBytes(duration, &wayTooLong)
  73. }