Source File
log.go
Belonging Package
golang.org/x/pkgsite/internal/log
traceIDKey struct{}
labelsKey struct{}
)
type stackdriverLogger struct {
sdlogger *logging.Logger
}
if , := .(error); {
= .Error()
}
, := .Value(traceIDKey{}).(string) // if not present, traceID is "", which is fine
, := .Value(labelsKey{}).(map[string]string)
:= experimentString()
if len() > 0 {
:= map[string]string{}
for , := range {
[] =
}
["experiments"] =
=
}
.sdlogger.Log(logging.Entry{
Severity: ,
Labels: ,
Payload: ,
Trace: ,
})
}
type stdlibLogger struct{}
if os.Getenv("GO_DISCOVERY_ON_GKE") != "" {
log.SetOutput(os.Stdout)
}
}
func (stdlibLogger) ( context.Context, logging.Severity, interface{}) {
var []string
, := .Value(traceIDKey{}).(string) // if not present, traceID is ""
if != "" {
= append(, fmt.Sprintf("traceID %s", ))
}
if , := .Value(labelsKey{}).(map[string]string); {
= append(, fmt.Sprint())
}
:= experimentString()
if len() > 0 {
= append(, fmt.Sprintf("experiments %s", ))
}
var string
if len() > 0 {
= " (" + strings.Join(, ", ") + ")"
}
log.Printf("%s%s: %+v", , , )
}
func ( context.Context) string {
return strings.Join(experiment.FromContext().Active(), ", ")
}
func ( context.Context, *config.Config, string) ( *logging.Logger, error) {
defer derrors.Wrap(&, "UseStackdriver(ctx, %q)", )
, := logging.NewClient(, .ProjectID)
if != nil {
return nil,
}
:= []logging.LoggerOption{logging.CommonResource(.MonitoredResource)}
if .OnGKE() {
= append(, logging.CommonLabels(map[string]string{
"k8s-pod/env": .DeploymentEnvironment(),
"k8s-pod/app": .Application(),
}))
}
:= .Logger(, ...)
:= .Logger(+"-child", ...)
mu.Lock()
defer mu.Unlock()
if , := logger.(*stackdriverLogger); {
return nil, errors.New("already called once")
}
logger = &stackdriverLogger{}
return , nil
}
func ( context.Context, interface{}) {
doLog(, logging.Critical, )
die()
}
func ( context.Context, logging.Severity, interface{}) {
if getLevel() > {
return
}
mu.Lock()
:= logger
mu.Unlock()
.log(, , )
}
func () {
mu.Lock()
if , := logger.(*stackdriverLogger); {
.sdlogger.Flush()
}
mu.Unlock()
os.Exit(1)
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |