forked from upspin/gcp
/
log.go
52 lines (43 loc) · 1.27 KB
/
log.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
// Copyright 2017 The Upspin Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package log provides an implemention of github.com/palager/upspin/log.ExternalLogger that
// sends logs to the Google Cloud Logging service.
package log // import "github.com/palager/gcp/cloud/log"
import (
"context"
"github.com/palager/upspin/log"
"cloud.google.com/go/logging"
"google.golang.org/api/option"
)
// Connect creates a logger that speaks to the Google Cloud Logging service for
// the given project and registers that logger with the log package.
func Connect(projectID, logName string) error {
var err error
client, err := logging.NewClient(context.Background(), projectID, option.WithScopes(logging.WriteScope))
if err != nil {
return err
}
log.Register(logger{
cloud: client.Logger(logName),
})
return nil
}
type logger struct {
cloud *logging.Logger
}
var severity = map[log.Level]logging.Severity{
log.DebugLevel: logging.Debug,
log.ErrorLevel: logging.Error,
log.InfoLevel: logging.Info,
}
func (l logger) Log(level log.Level, message string) {
s, ok := severity[level]
if !ok {
return
}
l.cloud.StandardLogger(s).Print(message)
}
func (l logger) Flush() {
l.cloud.Flush()
}