aelog

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: 27

Documentation

Overview

Example
package main

import (
	"context"
	"net/http"
	"os"

	"github.com/vvakame/sdlog/aelog"
)

func main() {
	_ = os.Setenv("GAE_APPLICATION", "example")
	r, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		panic(err)
	}
	// mimic AppEngine environment's request
	r.Header.Add("X-Cloud-Trace-Context", "e5e6be3a33c969fc2c696daf9d969fd9/4901377287092830619;o=1")

	// in your app...
	ctx := context.Background()
	ctx = aelog.WithHTTPRequest(ctx, r)

	aelog.Debugf(ctx, "appengine compat log: %s", "💕 AppEngine")
}
Output:

Example (WithOpenCensus)
package main

import (
	"context"
	"os"

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

func main() {
	_ = os.Setenv("GAE_APPLICATION", "example")

	// in your app...
	ctx := context.Background()
	ctx, span := trace.StartSpan(ctx, "foobar")
	defer span.End()

	aelog.Debugf(ctx, "appengine compat log: %s", "💕 AppEngine")
}
Output:

Example (WithRecover)
package main

import (
	"context"
	"fmt"
	"net/http"
	"os"
	"runtime/debug"

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

func main() {
	_ = os.Setenv("GAE_APPLICATION", "example")

	// in your app...
	ctx := context.Background()
	ctx, span := trace.StartSpan(ctx, "foobar")
	defer span.End()
	defer func() {
		err := recover()
		if err != nil {
			// not perfect... but better.
			aelog.Errorf(ctx, "%s\n\n%s", err, string(debug.Stack()))
			panic(err)
		}
	}()

	var obj *http.Request
	// boom!!
	fmt.Println(obj.URL.String())
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var LogWriter = func(ctx context.Context, logEntry *buildlog.LogEntry) {
	b, err := json.Marshal(logEntry)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(b))
}

LogWriter use write log entry to somewhere. default is stdout.

Functions

func Criticalf

func Criticalf(ctx context.Context, format string, args ...interface{})

Criticalf is like Debugf, but at Critical level.

func Debugf

func Debugf(ctx context.Context, format string, args ...interface{})

Debugf formats its arguments according to the format, analogous to fmt.Printf, and records the text as a log message at Debug level. The message will be associated with the request linked with the provided context.

func Errorf

func Errorf(ctx context.Context, format string, args ...interface{})

Errorf is like Debugf, but at Error level.

func Infof

func Infof(ctx context.Context, format string, args ...interface{})

Infof is like Debugf, but at Info level.

func Warningf

func Warningf(ctx context.Context, format string, args ...interface{})

Warningf is like Debugf, but at Warning level.

func WithHTTPRequest

func WithHTTPRequest(ctx context.Context, r *http.Request) context.Context

WithHTTPRequest is required when you don't use OpenCensus.

func WithSourceLocationSkip

func WithSourceLocationSkip(ctx context.Context, skip int) context.Context

WithSourceLocationSkip is add skip source location number to context.

Types

type AppEngineConfigurator

type AppEngineConfigurator struct{}

AppEngineConfigurator works on AppEngine.

func (*AppEngineConfigurator) ProjectID

func (*AppEngineConfigurator) ProjectID() string

ProjectID returns current GCP project id.

func (*AppEngineConfigurator) RemoteIP

func (*AppEngineConfigurator) RemoteIP(r *http.Request) string

RemoteIP of client.

func (*AppEngineConfigurator) TraceInfo

func (*AppEngineConfigurator) TraceInfo(ctx context.Context) (traceID, spanID string)

TraceInfo returns TraceID and SpanID.

Jump to

Keyboard shortcuts

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