-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogz.go
125 lines (107 loc) · 3.54 KB
/
logz.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package logz
import (
"context"
"fmt"
"io"
"net/http"
"time"
"github.com/glassonion1/logz/internal/config"
"github.com/glassonion1/logz/internal/logger"
"github.com/glassonion1/logz/internal/severity"
"github.com/glassonion1/logz/internal/types"
logzpropagation "github.com/glassonion1/logz/propagation"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
func init() {
config.WriteAccessLog = logger.WriteAccessLog
}
// Config is configurations for logz
type Config struct {
// GCP Project ID
ProjectID string
// CallerDepth is the number of stack frames to ascend
CallerSkip int
// Whether or not to write the access log
NeedsAccessLog bool
// Output for application log
ApplicationLogOut io.Writer
// Output for access log
AccessLogOut io.Writer
}
// SetProjectID sets gcp project id to the logger
func SetProjectID(projectID string) {
config.ProjectID = projectID
}
// SetConfig sets config to the logger
func SetConfig(cfg Config) {
if cfg.ProjectID != "" {
config.ProjectID = cfg.ProjectID
}
config.CallerSkip = cfg.CallerSkip
if !cfg.NeedsAccessLog {
config.WriteAccessLog = types.WriteEmptyAccessLog
}
if cfg.ApplicationLogOut != nil {
config.ApplicationLogOut = cfg.ApplicationLogOut
}
if cfg.AccessLogOut != nil {
config.AccessLogOut = cfg.AccessLogOut
}
}
// Debugf writes debug log to the stdout
func Debugf(ctx context.Context, format string, a ...interface{}) {
logger.WriteApplicationLog(ctx, severity.Debug, format, a...)
}
// Infof writes info log to the stdout
func Infof(ctx context.Context, format string, a ...interface{}) {
logger.WriteApplicationLog(ctx, severity.Info, format, a...)
}
// Warningf writes warning log to the stdout
func Warningf(ctx context.Context, format string, a ...interface{}) {
logger.WriteApplicationLog(ctx, severity.Warning, format, a...)
}
// Errorf writes error log to the stdout
func Errorf(ctx context.Context, format string, a ...interface{}) {
logger.WriteApplicationLog(ctx, severity.Error, format, a...)
}
// Criticalf writes critical log to the stdout
func Criticalf(ctx context.Context, format string, a ...interface{}) {
logger.WriteApplicationLog(ctx, severity.Critical, format, a...)
}
// Access writes access log to the stderr
func Access(ctx context.Context, r http.Request, statusCode, responseSize int, elapsed time.Duration) {
req := types.MakeHTTPRequest(r, statusCode, responseSize, elapsed)
config.WriteAccessLog(ctx, req)
}
// AccessLog writes access log to the stderr without http.Request
func AccessLog(ctx context.Context, method, url, userAgent, remoteIP, protocol string, statusCode, requestSize, responseSize int, elapsed time.Duration) {
req := types.HTTPRequest{
RequestMethod: method,
RequestURL: url,
RequestSize: fmt.Sprintf("%d", requestSize),
Status: statusCode,
ResponseSize: fmt.Sprintf("%d", responseSize),
UserAgent: userAgent,
RemoteIP: remoteIP,
Latency: types.MakeDuration(elapsed),
Protocol: protocol,
}
config.WriteAccessLog(ctx, req)
}
// InitTracer initializes OpenTelemetry tracer
func InitTracer() {
tp := sdktrace.NewTracerProvider()
otel.SetTracerProvider(tp)
props := propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
logzpropagation.HTTPFormat{})
otel.SetTextMapPropagator(props)
}
// StartCollectingSeverity starts collectiong severity
func StartCollectingSeverity(ctx context.Context) context.Context {
cs := &severity.ContextSeverity{}
return severity.SetContextSeverity(ctx, cs)
}