xlog.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. "github.com/fatedier/frp/pkg/util/log"
  17. )
  18. // Logger is not thread safety for operations on prefix
  19. type Logger struct {
  20. prefixes []string
  21. prefixString string
  22. }
  23. func New() *Logger {
  24. return &Logger{
  25. prefixes: make([]string, 0),
  26. }
  27. }
  28. func (l *Logger) ResetPrefixes() (old []string) {
  29. old = l.prefixes
  30. l.prefixes = make([]string, 0)
  31. l.prefixString = ""
  32. return
  33. }
  34. func (l *Logger) AppendPrefix(prefix string) *Logger {
  35. l.prefixes = append(l.prefixes, prefix)
  36. l.prefixString += "[" + prefix + "] "
  37. return l
  38. }
  39. func (l *Logger) Spawn() *Logger {
  40. nl := New()
  41. for _, v := range l.prefixes {
  42. nl.AppendPrefix(v)
  43. }
  44. return nl
  45. }
  46. func (l *Logger) Error(format string, v ...interface{}) {
  47. log.Log.Error(l.prefixString+format, v...)
  48. }
  49. func (l *Logger) Warn(format string, v ...interface{}) {
  50. log.Log.Warn(l.prefixString+format, v...)
  51. }
  52. func (l *Logger) Info(format string, v ...interface{}) {
  53. log.Log.Info(l.prefixString+format, v...)
  54. }
  55. func (l *Logger) Debug(format string, v ...interface{}) {
  56. log.Log.Debug(l.prefixString+format, v...)
  57. }
  58. func (l *Logger) Trace(format string, v ...interface{}) {
  59. log.Log.Trace(l.prefixString+format, v...)
  60. }