1
0

xlog.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "sort"
  17. "github.com/fatedier/frp/pkg/util/log"
  18. )
  19. type LogPrefix struct {
  20. // Name is the name of the prefix, it won't be displayed in log but used to identify the prefix.
  21. Name string
  22. // Value is the value of the prefix, it will be displayed in log.
  23. Value string
  24. // The prefix with higher priority will be displayed first, default is 10.
  25. Priority int
  26. }
  27. // Logger is not thread safety for operations on prefix
  28. type Logger struct {
  29. prefixes []LogPrefix
  30. prefixString string
  31. }
  32. func New() *Logger {
  33. return &Logger{
  34. prefixes: make([]LogPrefix, 0),
  35. }
  36. }
  37. func (l *Logger) ResetPrefixes() (old []LogPrefix) {
  38. old = l.prefixes
  39. l.prefixes = make([]LogPrefix, 0)
  40. l.prefixString = ""
  41. return
  42. }
  43. func (l *Logger) AppendPrefix(prefix string) *Logger {
  44. return l.AddPrefix(LogPrefix{
  45. Name: prefix,
  46. Value: prefix,
  47. Priority: 10,
  48. })
  49. }
  50. func (l *Logger) AddPrefix(prefix LogPrefix) *Logger {
  51. found := false
  52. if prefix.Priority <= 0 {
  53. prefix.Priority = 10
  54. }
  55. for _, p := range l.prefixes {
  56. if p.Name == prefix.Name {
  57. found = true
  58. p.Value = prefix.Value
  59. p.Priority = prefix.Priority
  60. }
  61. }
  62. if !found {
  63. l.prefixes = append(l.prefixes, prefix)
  64. }
  65. l.renderPrefixString()
  66. return l
  67. }
  68. func (l *Logger) renderPrefixString() {
  69. sort.SliceStable(l.prefixes, func(i, j int) bool {
  70. return l.prefixes[i].Priority < l.prefixes[j].Priority
  71. })
  72. l.prefixString = ""
  73. for _, v := range l.prefixes {
  74. l.prefixString += "[" + v.Value + "] "
  75. }
  76. }
  77. func (l *Logger) Spawn() *Logger {
  78. nl := New()
  79. nl.prefixes = append(nl.prefixes, l.prefixes...)
  80. nl.renderPrefixString()
  81. return nl
  82. }
  83. func (l *Logger) Error(format string, v ...interface{}) {
  84. log.Log.Error(l.prefixString+format, v...)
  85. }
  86. func (l *Logger) Warn(format string, v ...interface{}) {
  87. log.Log.Warn(l.prefixString+format, v...)
  88. }
  89. func (l *Logger) Info(format string, v ...interface{}) {
  90. log.Log.Info(l.prefixString+format, v...)
  91. }
  92. func (l *Logger) Debug(format string, v ...interface{}) {
  93. log.Log.Debug(l.prefixString+format, v...)
  94. }
  95. func (l *Logger) Trace(format string, v ...interface{}) {
  96. log.Log.Trace(l.prefixString+format, v...)
  97. }