-
Notifications
You must be signed in to change notification settings - Fork 13
/
logger.go
80 lines (68 loc) · 1.85 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package s3tar
import (
"context"
"fmt"
"log"
"os"
)
const (
contextKeyLogger = contextKey("logger")
contextKeyLoggerLevel = contextKey("logger-level")
)
type logWriter struct {
}
func (writer logWriter) Write(bytes []byte) (int, error) {
return fmt.Print(string(bytes))
}
func SetLogLevel(ctx context.Context, level int) context.Context {
return context.WithValue(ctx, contextKeyLoggerLevel, level)
}
func SetupLogger(incoming context.Context) context.Context {
logger := log.New(os.Stdout, "", 0)
logger.SetOutput(new(logWriter))
return context.WithValue(incoming, contextKeyLogger, logger)
}
func Debugf(ctx context.Context, format string, v ...interface{}) {
logger, level := getValues(ctx)
if level > 2 && level <= 3 {
logger.Printf(format, v...)
}
}
func Warnf(ctx context.Context, format string, v ...interface{}) {
logger, level := getValues(ctx)
if level > 1 && level <= 3 {
logger.Printf(format, v...)
}
}
// Errorf, always log regardless of log level, but don't stop the application
func Errorf(ctx context.Context, format string, v ...interface{}) {
logger, _ := getValues(ctx)
logger.Printf(format, v...)
}
func Fatalf(ctx context.Context, format string, v ...interface{}) {
log.Fatalf(format, v...)
}
func Infof(ctx context.Context, format string, v ...interface{}) {
logger, level := getValues(ctx)
if level >= 1 {
logger.Printf(format, v...)
}
}
func getValues(ctx context.Context) (*log.Logger, int) {
var logger *log.Logger
var level int
if _logger, ok := ctx.Value(contextKeyLogger).(*log.Logger); ok {
logger = _logger
} else {
log.Printf("default logger")
logger = log.Default()
}
if _level, ok := ctx.Value(contextKeyLoggerLevel).(int); ok {
level = _level
} else {
level = 0
}
return logger, level
}