buildlog

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2023 License: MIT Imports: 9 Imported by: 2

Documentation

Overview

Example (EmitJSONPayload)
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"time"

	"github.com/vvakame/sdlog/buildlog"
	"go.opencensus.io/trace"
)

func main() {
	ctx := context.Background()
	ctx, span := trace.StartSpan(ctx, "test")
	defer span.End()

	logEntry := buildlog.NewLogEntry(ctx)

	// for stable log output
	logEntry.Trace = "projects/foobar/traces/65ed3bb1ceb342ba0ca62fa64076c738"
	logEntry.SpanID = "2325d572b51a4ba6"
	logEntry.Time = buildlog.Time(time.Date(2019, 5, 18, 13, 47, 0, 0, time.UTC))
	logEntry.SourceLocation.File = "/tmp/123456/sdlog/buildlog/example_test.go"
	logEntry.SourceLocation.Line = 10

	b, err := json.Marshal(logEntry)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(b))

}
Output:

{"severity":"DEFAULT","time":"2019-05-18T13:47:00Z","logging.googleapis.com/trace":"projects/foobar/traces/65ed3bb1ceb342ba0ca62fa64076c738","logging.googleapis.com/spanId":"2325d572b51a4ba6","logging.googleapis.com/sourceLocation":{"file":"/tmp/123456/sdlog/buildlog/example_test.go","line":"10","function":"github.com/vvakame/sdlog/buildlog_test.Example_emitJSONPayload"}}
Example (EmitJSONPayloadWithEmbed)
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"time"

	"github.com/vvakame/sdlog/buildlog"
	"go.opencensus.io/trace"
)

func main() {
	ctx := context.Background()
	ctx, span := trace.StartSpan(ctx, "test")
	defer span.End()

	type MyLog struct {
		Message string
		buildlog.LogEntry
	}

	buildMyLog := func(message string) *MyLog {
		myLog := &MyLog{
			Message:  message,
			LogEntry: *buildlog.NewLogEntry(ctx, buildlog.WithSourceLocationSkip(4)),
		}
		return myLog
	}

	logEntry := buildMyLog("Hi!")

	// for stable log output
	logEntry.Trace = "projects/foobar/traces/65ed3bb1ceb342ba0ca62fa64076c738"
	logEntry.SpanID = "2325d572b51a4ba6"
	logEntry.Time = buildlog.Time(time.Date(2019, 5, 18, 13, 47, 0, 0, time.UTC))
	logEntry.SourceLocation.File = "/tmp/123456/sdlog/buildlog/example_test.go"
	logEntry.SourceLocation.Line = 55

	b, err := json.Marshal(logEntry)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(b))

}
Output:

{"Message":"Hi!","severity":"DEFAULT","time":"2019-05-18T13:47:00Z","logging.googleapis.com/trace":"projects/foobar/traces/65ed3bb1ceb342ba0ca62fa64076c738","logging.googleapis.com/spanId":"2325d572b51a4ba6","logging.googleapis.com/sourceLocation":{"file":"/tmp/123456/sdlog/buildlog/example_test.go","line":"55","function":"github.com/vvakame/sdlog/buildlog_test.Example_emitJSONPayloadWithEmbed"}}
Example (EmitTextPayload)
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"time"

	"github.com/vvakame/sdlog/buildlog"
	"go.opencensus.io/trace"
)

func main() {
	ctx := context.Background()
	ctx, span := trace.StartSpan(ctx, "test")
	defer span.End()

	logEntry := buildlog.NewLogEntry(ctx)
	logEntry.Message = "Hi!"

	// for stable log output
	logEntry.Trace = "projects/foobar/traces/65ed3bb1ceb342ba0ca62fa64076c738"
	logEntry.SpanID = "2325d572b51a4ba6"
	logEntry.Time = buildlog.Time(time.Date(2019, 5, 18, 13, 47, 0, 0, time.UTC))
	logEntry.SourceLocation.File = "/tmp/123456/sdlog/buildlog/example_test.go"
	logEntry.SourceLocation.Line = 55

	b, err := json.Marshal(logEntry)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(b))

}
Output:

{"severity":"DEFAULT","time":"2019-05-18T13:47:00Z","logging.googleapis.com/trace":"projects/foobar/traces/65ed3bb1ceb342ba0ca62fa64076c738","logging.googleapis.com/spanId":"2325d572b51a4ba6","logging.googleapis.com/sourceLocation":{"file":"/tmp/123456/sdlog/buildlog/example_test.go","line":"55","function":"github.com/vvakame/sdlog/buildlog_test.Example_emitTextPayload"},"message":"Hi!"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithConfigurator

func WithConfigurator(ctx context.Context, cfg Configurator) (context.Context, error)

WithConfigurator bundles Configurator to context.

Types

type Configurator

type Configurator interface {
	ProjectID() string
	TraceInfo(ctx context.Context) (traceID, spanID string)
	RemoteIP(r *http.Request) string
}

Configurator provides some value from environment.

var DefaultConfigurator Configurator = &GCPDefaultConfigurator{}

DefaultConfigurator will use logging without WithConfigurator.

type Duration

type Duration time.Duration

Duration provides time.Duration by protobuf format.

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON convert raw value to JSON value.

type GCPDefaultConfigurator

type GCPDefaultConfigurator struct{}

GCPDefaultConfigurator works on AppEngine, Cloud Run, Compute Engine etc.

func (*GCPDefaultConfigurator) ProjectID

func (cfg *GCPDefaultConfigurator) ProjectID() string

ProjectID returns current GCP project id.

func (*GCPDefaultConfigurator) RemoteIP

func (cfg *GCPDefaultConfigurator) RemoteIP(r *http.Request) string

RemoteIP of client.

func (*GCPDefaultConfigurator) TraceInfo

func (cfg *GCPDefaultConfigurator) TraceInfo(ctx context.Context) (string, string)

TraceInfo returns TraceID and SpanID.

type HTTPRequest

type HTTPRequest struct {
	RequestMethod                  string   `json:"requestMethod"`
	RequestURL                     string   `json:"requestUrl"`
	RequestSize                    int64    `json:"requestSize,string,omitempty"`
	Status                         int      `json:"status"`
	ResponseSize                   int64    `json:"responseSize,string,omitempty"`
	UserAgent                      string   `json:"userAgent,omitempty"`
	RemoteIP                       string   `json:"remoteIp,omitempty"`
	Referer                        string   `json:"referer,omitempty"`
	Latency                        Duration `json:"latency,omitempty"`
	CacheLookup                    *bool    `json:"cacheLookup,omitempty"`
	CacheHit                       *bool    `json:"cacheHit,omitempty"`
	CacheValidatedWithOriginServer *bool    `json:"cacheValidatedWithOriginServer,omitempty"`
	CacheFillBytes                 *int64   `json:"cacheFillBytes,string,omitempty"`
	Protocol                       string   `json:"protocol"`
}

HTTPRequest provides HTTPRequest log. spec: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#httprequest

type LogEntry

type LogEntry struct {
	Severity       Severity                `json:"severity"`
	HTTPRequest    *HTTPRequest            `json:"httpRequest,omitempty"`
	Time           Time                    `json:"time,omitempty"`
	Trace          string                  `json:"logging.googleapis.com/trace,omitempty"`
	SpanID         string                  `json:"logging.googleapis.com/spanId,omitempty"`
	Operation      *LogEntryOperation      `json:"logging.googleapis.com/operation,omitempty"`
	SourceLocation *LogEntrySourceLocation `json:"logging.googleapis.com/sourceLocation,omitempty"`
	Message        string                  `json:"message,omitempty"`
}

LogEntry provides special fields in structured log. spec: https://cloud.google.com/logging/docs/agent/configuration#special-fields

func NewLogEntry

func NewLogEntry(ctx context.Context, opts ...LogEntryOption) *LogEntry

NewLogEntry returns *LogEntry for current executing line.

type LogEntryOperation

type LogEntryOperation struct {
	ID       string `json:"id,omitempty"`
	Producer string `json:"producer,omitempty"`
	First    *bool  `json:"first,omitempty"`
	Last     *bool  `json:"last,omitempty"`
}

LogEntryOperation provides information for long-running operation. spec: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logentryoperation

type LogEntryOption

type LogEntryOption func(*logEntryConfig)

LogEntryOption provides some options.

func WithSourceLocationSkip

func WithSourceLocationSkip(skip int) LogEntryOption

WithSourceLocationSkip provides skip depth for runtime.Caller.

type LogEntrySourceLocation

type LogEntrySourceLocation struct {
	File     string `json:"file,omitempty"`
	Line     int64  `json:"line,string,omitempty"`
	Function string `json:"function,omitempty"`
}

LogEntrySourceLocation provides source location of log emitting. spec: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logentrysourcelocation

type Severity

type Severity int

Severity provides log levels. spec: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity

const (
	// SeverityDefault provides severity level.
	SeverityDefault Severity = iota * 100
	// SeverityDebug provides severity level.
	SeverityDebug
	// SeverityInfo provides severity level.
	SeverityInfo
	// SeverityNotice provides severity level.
	SeverityNotice
	// SeverityWarning provides severity level.
	SeverityWarning
	// SeverityError provides severity level.
	SeverityError
	// SeverityCritical provides severity level.
	SeverityCritical
	// SeverityAlert provides severity level.
	SeverityAlert
	// SeverityEmergency provides severity level.
	SeverityEmergency
)

func (Severity) MarshalJSON

func (severity Severity) MarshalJSON() ([]byte, error)

MarshalJSON convert raw value to JSON value.

func (Severity) String

func (severity Severity) String() string

String returns Severity about string format.

type Time

type Time time.Time

Time provides time.Time by protobuf format.

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON convert raw value to JSON value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL