-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglog.go
51 lines (41 loc) · 1.42 KB
/
glog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// (c) 2022 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)
// Package glogadapter provides yala adapter which leverages glog package (https://github.com/golang/glog).
package glogadapter
import (
"context"
"strings"
"github.com/elgopher/yala/adapter/logfmt"
"github.com/elgopher/yala/logger"
"github.com/golang/glog"
)
// Adapter is a logger.Adapter implementation, which is using `glog` package (https://github.com/golang/glog).
type Adapter struct{}
// Log logs the entry using glog package.
func (a Adapter) Log(_ context.Context, entry logger.Entry) {
var fieldsAndError strings.Builder
if len(entry.Fields) > 0 {
logfmt.WriteFields(&fieldsAndError, entry.Fields)
}
if entry.Error != nil {
if len(entry.Fields) > 0 {
fieldsAndError.WriteByte(' ')
}
logfmt.WriteField(&fieldsAndError, logger.Field{Key: "error", Value: entry.Error})
}
fieldsAndErrorString := fieldsAndError.String()
message := entry.Message
depth := entry.SkippedCallerFrames + 1
switch entry.Level {
case logger.DebugLevel:
glog.InfoDepth(depth, message, " ", fieldsAndErrorString)
case logger.InfoLevel:
glog.InfoDepth(depth, message, " ", fieldsAndErrorString)
case logger.WarnLevel:
glog.WarningDepth(depth, message, " ", fieldsAndErrorString)
case logger.ErrorLevel:
glog.ErrorDepth(depth, message, " ", fieldsAndErrorString)
default:
glog.InfoDepth(depth, message, " ", fieldsAndErrorString)
}
}