Back to godoc.org

Package log

v0.0.15
Latest Go to latest

The latest major version is .

Published: Jul 31, 2020 | License: BSD-3-Clause | Module: go.chromium.org/goma/server

Overview

Package log provides logging mechanism for goma servers.

Example

Code:

package main

import (
	"context"
	"go.chromium.org/goma/server/log"
	"go.opencensus.io/tag"
	"go.uber.org/zap"
)

func main() {
	ctx := context.Background()
	log.SetZapLogger(zap.NewExample())
	logger := log.FromContext(ctx)
	defer logger.Sync()
	k, err := tag.NewKey("go.chromium.org/goma/server/log/example")
	if err != nil {
		logger.Fatal(err)
	}
	log.RegisterTagKey(k)

	ctx, err = tag.New(ctx, tag.Insert(k, "trace_1"))
	if err != nil {
		logger.Fatal(err)
	}
	logger = log.FromContext(ctx)

	logger.Info("info")
}
{"level":"info","msg":"info","go.chromium.org/goma/server/log/example":"trace_1"}

Index

Examples

func GRPCUnaryServerInterceptor

func GRPCUnaryServerInterceptor(opts ...grpczap.Option) grpc.UnaryServerInterceptor

GRPCUnaryServerInterceptor returns server interceptor to log grpc calls.

func NewContext

func NewContext(ctx context.Context, logger Logger) context.Context

NewContext returns a new Context that carries logger.

func RegisterTagKey

func RegisterTagKey(key tag.Key)

RegsiterTagKey registers tag key that would be used for log context.

func SetZapLogger

func SetZapLogger(zapLogger *zap.Logger)

SetZapLogger sets zap logger as default logger. Useful for test

log.SetZapLogger(zap.NewExample())

type Logger

type Logger interface {
	// Debug logs to DEBUG log. Arguments are handled in the manner of fmt.Print.
	Debug(args ...interface{})

	// Debugf logs to DEBUG log. Arguments are handled in the manner of fmt.Printf.
	Debugf(format string, arg ...interface{})

	// Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
	Info(args ...interface{})
	// Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
	Infof(format string, arg ...interface{})

	// Warn logs to WARNING log. Arguments are handled in the manner of fmt.Print.
	Warn(args ...interface{})

	// Warnf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
	Warnf(format string, arg ...interface{})

	// Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
	Error(args ...interface{})

	// Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
	Errorf(format string, arg ...interface{})

	// Fatal logs to CRITICAL log. Arguments are handled in te manner of fmt.Print.
	Fatal(args ...interface{})

	// Fatalf logs to CRITICAL log. Arguments are handled in the manner of fmt.Printf.
	Fatalf(format string, arg ...interface{})

	// Sync flushes any buffered log entries.
	Sync() error
}

Logger is logging interface.

func FromContext

func FromContext(ctx context.Context) Logger

FromContext returns logger with context. opencensus's tag registered by RegisterTagKey and trace's span-id and trace-id will be added as context information of the log.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier