xlog.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2019 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package xlog
  15. import (
  16. "cmp"
  17. "slices"
  18. "github.com/fatedier/frp/pkg/util/log"
  19. )
  20. type LogPrefix struct {
  21. // Name is the name of the prefix, it won't be displayed in log but used to identify the prefix.
  22. Name string
  23. // Value is the value of the prefix, it will be displayed in log.
  24. Value string
  25. // The prefix with higher priority will be displayed first, default is 10.
  26. Priority int
  27. }
  28. // Logger is not thread safety for operations on prefix
  29. type Logger struct {
  30. prefixes []LogPrefix
  31. prefixString string
  32. }
  33. func New() *Logger {
  34. return &Logger{
  35. prefixes: make([]LogPrefix, 0),
  36. }
  37. }
  38. func (l *Logger) ResetPrefixes() (old []LogPrefix) {
  39. old = l.prefixes
  40. l.prefixes = make([]LogPrefix, 0)
  41. l.prefixString = ""
  42. return
  43. }
  44. func (l *Logger) AppendPrefix(prefix string) *Logger {
  45. return l.AddPrefix(LogPrefix{
  46. Name: prefix,
  47. Value: prefix,
  48. Priority: 10,
  49. })
  50. }
  51. func (l *Logger) AddPrefix(prefix LogPrefix) *Logger {
  52. found := false
  53. if prefix.Priority <= 0 {
  54. prefix.Priority = 10
  55. }
  56. for _, p := range l.prefixes {
  57. if p.Name == prefix.Name {
  58. found = true
  59. p.Value = prefix.Value
  60. p.Priority = prefix.Priority
  61. }
  62. }
  63. if !found {
  64. l.prefixes = append(l.prefixes, prefix)
  65. }
  66. l.renderPrefixString()
  67. return l
  68. }
  69. func (l *Logger) renderPrefixString() {
  70. slices.SortStableFunc(l.prefixes, func(a, b LogPrefix) int {
  71. return cmp.Compare(a.Priority, b.Priority)
  72. })
  73. l.prefixString = ""
  74. for _, v := range l.prefixes {
  75. l.prefixString += "[" + v.Value + "] "
  76. }
  77. }
  78. func (l *Logger) Spawn() *Logger {
  79. nl := New()
  80. nl.prefixes = append(nl.prefixes, l.prefixes...)
  81. nl.renderPrefixString()
  82. return nl
  83. }
  84. func (l *Logger) Errorf(format string, v ...interface{}) {
  85. log.Logger.Errorf(l.prefixString+format, v...)
  86. }
  87. func (l *Logger) Warnf(format string, v ...interface{}) {
  88. log.Logger.Warnf(l.prefixString+format, v...)
  89. }
  90. func (l *Logger) Infof(format string, v ...interface{}) {
  91. log.Logger.Infof(l.prefixString+format, v...)
  92. }
  93. func (l *Logger) Debugf(format string, v ...interface{}) {
  94. log.Logger.Debugf(l.prefixString+format, v...)
  95. }
  96. func (l *Logger) Tracef(format string, v ...interface{}) {
  97. log.Logger.Tracef(l.prefixString+format, v...)
  98. }