add string tokens
Gitea/etcdmtr/pipeline/head This commit looks good
Details
Gitea/etcdmtr/pipeline/head This commit looks good
Details
This commit is contained in:
parent
bb0a19270e
commit
a3c5bd05e1
|
@ -25,40 +25,66 @@ type ELog struct {
|
|||
Format *LogFormat
|
||||
}
|
||||
|
||||
type LogFormatToken int64
|
||||
type LogFormatTokenValue int64
|
||||
|
||||
const (
|
||||
LOGFORMAT_TIMESTAMP LogFormatToken = iota
|
||||
LOGFORMAT_STRING LogFormatTokenValue = iota
|
||||
LOGFORMAT_TIMESTAMP
|
||||
LOGFORMAT_NAME
|
||||
LOGFORMAT_MESSAGE
|
||||
)
|
||||
|
||||
type LogFormat struct {
|
||||
Next *LogFormat
|
||||
Token LogFormatToken
|
||||
type LogFormatToken struct {
|
||||
Next *LogFormatToken
|
||||
Token LogFormatTokenValue
|
||||
Value string
|
||||
}
|
||||
|
||||
func NewFormat(firstToken LogFormatToken) *LogFormat {
|
||||
return &LogFormat{
|
||||
Token: firstToken,
|
||||
}
|
||||
type LogFormat struct {
|
||||
Head *LogFormatToken
|
||||
}
|
||||
|
||||
func NewFormat() *LogFormat {
|
||||
return &LogFormat{}
|
||||
}
|
||||
|
||||
// Append appends the token/node to the end of the chain, and returns the node Append was called on.
|
||||
func (f *LogFormat) Append(token LogFormatToken) *LogFormat {
|
||||
tail := f
|
||||
func (f *LogFormat) Append(token *LogFormatToken) *LogFormat {
|
||||
if f.Head == nil {
|
||||
f.Head = token
|
||||
return f
|
||||
}
|
||||
tail := f.Head
|
||||
for tail.Next != nil {
|
||||
tail = tail.Next
|
||||
}
|
||||
tail.Next = &LogFormat{
|
||||
Token: token,
|
||||
}
|
||||
tail.Next = token
|
||||
return f
|
||||
}
|
||||
|
||||
// AppendToken isn't valid for LOGFORMAT_STRING
|
||||
func (f *LogFormat) AppendToken(token LogFormatTokenValue) *LogFormat {
|
||||
return f.Append(&LogFormatToken{
|
||||
Token: token,
|
||||
})
|
||||
}
|
||||
|
||||
func (f *LogFormat) AppendString(str string) *LogFormat {
|
||||
return f.Append(&LogFormatToken{
|
||||
Token: LOGFORMAT_STRING,
|
||||
Value: str,
|
||||
})
|
||||
}
|
||||
|
||||
func NewRootLogger() *ELog {
|
||||
logger := &ELog{
|
||||
Format: NewFormat(LOGFORMAT_TIMESTAMP).Append(LOGFORMAT_NAME).Append(LOGFORMAT_MESSAGE),
|
||||
Format: NewFormat().
|
||||
AppendString("[").
|
||||
AppendToken(LOGFORMAT_TIMESTAMP).
|
||||
AppendString("] ").
|
||||
AppendToken(LOGFORMAT_NAME).
|
||||
AppendString(": ").
|
||||
AppendToken(LOGFORMAT_MESSAGE),
|
||||
}
|
||||
return logger
|
||||
}
|
||||
|
@ -68,9 +94,11 @@ func (e *ELog) _Log(level LogLevel, message string, a ...any) (n int, err error)
|
|||
return 0, nil
|
||||
}
|
||||
log := strings.Builder{}
|
||||
tail := e.Format
|
||||
tail := e.Format.Head
|
||||
for tail != nil {
|
||||
switch token := tail.Token; token {
|
||||
case LOGFORMAT_STRING:
|
||||
log.WriteString(tail.Value)
|
||||
case LOGFORMAT_TIMESTAMP:
|
||||
log.WriteString("TS")
|
||||
case LOGFORMAT_NAME:
|
||||
|
|
Loading…
Reference in New Issue