-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
41 lines (34 loc) · 1.03 KB
/
Copy pathlog.go
File metadata and controls
41 lines (34 loc) · 1.03 KB
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
// SPDX-FileCopyrightText: 2026 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package sallust
import (
"bytes"
"log"
"net/http"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// LoggerFunc is a strategy for adding key/value pairs (possibly) based on an HTTP request.
// Functions of this type must append key/value pairs to the supplied slice and then return
// the new slice.
type LoggerFunc func([]zap.Field, *http.Request) []zap.Field
func NewTestLogger(level zapcore.Level) (*bytes.Buffer, *zap.Logger) {
b := new(bytes.Buffer)
return b, zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zap.CombineWriteSyncers(os.Stderr, zapcore.AddSync(b)),
level,
))
}
// NewServerLogger creates a new zap.Logger appropriate for http.Server.ErrorLog
func NewServerLogger(serverName string, logger *zap.Logger) *log.Logger {
if logger == nil {
logger = Default()
}
return log.New(
zap.NewStdLog(logger).Writer(),
serverName,
log.LstdFlags|log.LUTC,
)
}