forked from CaptainCodeman/gce-cache-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
73 lines (57 loc) · 1.51 KB
/
logging.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
package cachecluster
import (
"context"
"fmt"
"log"
"cloud.google.com/go/compute/metadata"
"cloud.google.com/go/logging"
"github.com/clockworksoul/smudge"
)
var logger *stackdriverLogging
type (
// Logging provides logging abstraction
Logging interface {
Debugf(format string, args ...interface{})
}
stackdriverLogging struct {
client *logging.Client
logger *logging.Logger
}
)
func init() {
logger, _ = NewStackdriverLogging()
}
// NewStackdriverLogging creates a new stackdriver logger
func NewStackdriverLogging() (*stackdriverLogging, error) {
ctx := context.Background()
project, _ := metadata.ProjectID()
client, err := logging.NewClient(ctx, project)
if err != nil {
return nil, err
}
logger := client.Logger("cache-cluster")
return &stackdriverLogging{
client: client,
logger: logger,
}, nil
}
func (s *stackdriverLogging) Debugf(format string, args ...interface{}) {
text := fmt.Sprintf(format, args...)
s.logger.Log(logging.Entry{Severity: logging.Debug, Payload: text})
}
func (s *stackdriverLogging) Logger() *log.Logger {
return s.logger.StandardLogger(logging.Info)
}
const logThreshhold = smudge.LogInfo
func (s *stackdriverLogging) Log(level smudge.LogLevel, a ...interface{}) (int, error) {
if level >= logThreshhold {
s.Debugf("%s %v", level.String(), a[0])
}
return 0, nil
}
func (s *stackdriverLogging) Logf(level smudge.LogLevel, format string, a ...interface{}) (int, error) {
if level >= logThreshhold {
s.Debugf(level.String()+" "+format, a...)
}
return 0, nil
}