frp/pkg/util/xlog/xlog.go

116 lines
2.7 KiB
Go
Raw Permalink Normal View History

2019-10-12 20:13:12 +08:00
// Copyright 2019 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package xlog
import (
2024-02-20 12:01:41 +08:00
"cmp"
"slices"
2023-11-21 11:19:35 +08:00
2020-09-23 13:49:14 +08:00
"github.com/fatedier/frp/pkg/util/log"
2019-10-12 20:13:12 +08:00
)
2023-11-21 11:19:35 +08:00
type LogPrefix struct {
// Name is the name of the prefix, it won't be displayed in log but used to identify the prefix.
Name string
// Value is the value of the prefix, it will be displayed in log.
Value string
// The prefix with higher priority will be displayed first, default is 10.
Priority int
}
2019-10-12 20:13:12 +08:00
// Logger is not thread safety for operations on prefix
type Logger struct {
2023-11-21 11:19:35 +08:00
prefixes []LogPrefix
2019-10-12 20:13:12 +08:00
prefixString string
}
func New() *Logger {
return &Logger{
2023-11-21 11:19:35 +08:00
prefixes: make([]LogPrefix, 0),
2019-10-12 20:13:12 +08:00
}
}
2023-11-21 11:19:35 +08:00
func (l *Logger) ResetPrefixes() (old []LogPrefix) {
2019-10-12 20:13:12 +08:00
old = l.prefixes
2023-11-21 11:19:35 +08:00
l.prefixes = make([]LogPrefix, 0)
2019-10-12 20:13:12 +08:00
l.prefixString = ""
return
}
func (l *Logger) AppendPrefix(prefix string) *Logger {
2023-11-21 11:19:35 +08:00
return l.AddPrefix(LogPrefix{
Name: prefix,
Value: prefix,
Priority: 10,
})
}
func (l *Logger) AddPrefix(prefix LogPrefix) *Logger {
found := false
if prefix.Priority <= 0 {
prefix.Priority = 10
}
for _, p := range l.prefixes {
if p.Name == prefix.Name {
found = true
p.Value = prefix.Value
p.Priority = prefix.Priority
}
}
if !found {
l.prefixes = append(l.prefixes, prefix)
}
l.renderPrefixString()
2019-10-12 20:13:12 +08:00
return l
}
2023-11-21 11:19:35 +08:00
func (l *Logger) renderPrefixString() {
2024-02-20 12:01:41 +08:00
slices.SortStableFunc(l.prefixes, func(a, b LogPrefix) int {
return cmp.Compare(a.Priority, b.Priority)
2023-11-21 11:19:35 +08:00
})
l.prefixString = ""
2019-10-12 20:13:12 +08:00
for _, v := range l.prefixes {
2023-11-21 11:19:35 +08:00
l.prefixString += "[" + v.Value + "] "
2019-10-12 20:13:12 +08:00
}
2023-11-21 11:19:35 +08:00
}
func (l *Logger) Spawn() *Logger {
nl := New()
nl.prefixes = append(nl.prefixes, l.prefixes...)
nl.renderPrefixString()
2019-10-12 20:13:12 +08:00
return nl
}
2024-03-12 13:58:53 +08:00
func (l *Logger) Errorf(format string, v ...interface{}) {
log.Logger.Errorf(l.prefixString+format, v...)
2019-10-12 20:13:12 +08:00
}
2024-03-12 13:58:53 +08:00
func (l *Logger) Warnf(format string, v ...interface{}) {
log.Logger.Warnf(l.prefixString+format, v...)
2019-10-12 20:13:12 +08:00
}
2024-03-12 13:58:53 +08:00
func (l *Logger) Infof(format string, v ...interface{}) {
log.Logger.Infof(l.prefixString+format, v...)
2019-10-12 20:13:12 +08:00
}
2024-03-12 13:58:53 +08:00
func (l *Logger) Debugf(format string, v ...interface{}) {
log.Logger.Debugf(l.prefixString+format, v...)
2019-10-12 20:13:12 +08:00
}
2024-03-12 13:58:53 +08:00
func (l *Logger) Tracef(format string, v ...interface{}) {
log.Logger.Tracef(l.prefixString+format, v...)
2019-10-12 20:13:12 +08:00
}