This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlogger.go
99 lines (83 loc) · 3.94 KB
/
logger.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
// Copyright 2020 MongoDB Inc
//
// 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 main
import (
"code.cloudfoundry.org/lager"
"go.uber.org/zap"
)
// Ensure LagerZapLogger adheres to the Logger interface.
var _ lager.Logger = &LagerZapLogger{}
// LagerZapLogger is implementing the Lager interface. The OSBAPI expects us to use the lager logger,
// but we wanted to use the Zap logger for its fast, leveled, and structured logging.
// The zap methods are wrapped in the Lager method calls and is merely mapping them.
type LagerZapLogger struct {
logger *zap.SugaredLogger
}
// NewLagerZapLogger constructs and returns a new LagerZapLogger with a pointer field of type SugaredLogger.
func NewLagerZapLogger(zap *zap.SugaredLogger) *LagerZapLogger {
return &LagerZapLogger{
logger: zap,
}
}
// RegisterSink represents a write destination for a Logger. It provides a thread-safe interface for writing logs.
func (lagerZapLogger *LagerZapLogger) RegisterSink(sink lager.Sink) {}
// SessionName returns the name of the session. This is normally added when initializing a new logger but zap doesn't require nor need it.
// but the OSBAPI does. Currently it's only returning an empty string.
func (lagerZapLogger *LagerZapLogger) SessionName() string {
return ""
}
// Session sets the session of the logger and returns a new logger with a nested session. We are currently
// returning the same logger back.
func (lagerZapLogger *LagerZapLogger) Session(task string, data ...lager.Data) lager.Logger {
return lagerZapLogger
}
// WithData creates a new child with the parent fields and returns a logger with newly added data. We are currently
// returning the same logger back.
func (lagerZapLogger *LagerZapLogger) WithData(data lager.Data) lager.Logger {
return lagerZapLogger
}
// Debug logs a message at debug level. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (lagerZapLogger *LagerZapLogger) Debug(action string, data ...lager.Data) {
lagerZapLogger.logger.Debugw(action, createFields(data)...)
}
// Info logs a message at InfoLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (lagerZapLogger *LagerZapLogger) Info(action string, data ...lager.Data) {
lagerZapLogger.logger.Infow(action, createFields(data)...)
}
// Error logs a message at ErrorLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (lagerZapLogger *LagerZapLogger) Error(action string, err error, data ...lager.Data) {
lagerZapLogger.logger.Errorw(action, append(createFields(data), "error", err)...)
}
// Fatal is for logging fatal messages.
// Fatal logs a message at FatalLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (lagerZapLogger *LagerZapLogger) Fatal(action string, err error, data ...lager.Data) {
lagerZapLogger.logger.Fatalw(action, append(createFields(data), "error", err)...)
}
// createFields converts the structured log data that the lager library uses to
// the format that zap expects. lager uses a list of maps while zap expects a flat list
// of alternating keys and values.
func createFields(data []lager.Data) []interface{} {
var fields []interface{}
// Copying items from data and appending them to fields
for _, item := range data {
for k, v := range item {
fields = append(fields, k, v)
}
}
return fields
}