log

package
v0.0.35 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Example

This example illustrates how to use NewFromCtx to initialise a logger with values from context.

package main

import (
	"context"

	"github.com/cultureamp/ca-go/x/log"
	"github.com/cultureamp/ca-go/x/request"
)

func main() {
	ctx := context.Background()

	ctx = log.ContextWithEnvConfig(ctx, log.EnvConfig{
		AppName:    "test-app",
		AppVersion: "1.0.0",
		AwsRegion:  "us-east-1",
		Farm:       "test-farm",
	})

	ctx = request.ContextWithRequestIDs(ctx, request.RequestIDs{
		RequestID:     "id1",
		CorrelationID: "id2",
	})

	logger := log.NewFromCtx(ctx)
	logger.Debug().
		Str("test-str", "str").
		Int("test-number", 1).
		Msg("initialise handler")

}
Output:

{"level":"debug","app":"test-app","app_version":"1.0.0","aws_region":"us-east-1","aws_account_id":"","farm":"test-farm","request_id":"id1","correlation_id":"id2","test-str":"str","test-number":1,"event":"initialise_handler","message":"initialise handler"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithEnvConfig

func ContextWithEnvConfig(ctx context.Context, envConfig EnvConfig) context.Context

ContextWithEnvConfig returns a new context with the given EnvConfig embedded as a value.

func ToSnakeCase

func ToSnakeCase(s string) string

ToSnakeCase returns a new string in the format word_word.

Types

type EnvConfig

type EnvConfig struct {
	AppName      string `envconfig:"APP"`    // The name of the application the log was generated from
	AppVersion   string `default:"0.0.0"`    // The version of the application
	AwsRegion    string `split_words:"true"` // the AWS region this code is running in
	AwsAccountID string `split_words:"true"` // the AWS account ID this code is running in
	Farm         string `default:"local"`    // The name of the farm or where the code is running
}

EnvConfig must have fields listed in https://cultureamp.atlassian.net/wiki/spaces/TV/pages/959939199/Logging.

func EnvConfigFromContext

func EnvConfigFromContext(ctx context.Context) EnvConfig

EnvConfigFromContext returns the EnvConfig value embedded in the given context. Return a default EnvConfig if not exists.

type EventHook

type EventHook struct{}

EventHook adds a field "event" to the log which is the past-tense verb of what just happened, in snake_case. Ideally, "event" should be in non-trivial services, the event should be namespaced, delimited by periods (.), where the last section is the verb. (e.g. jobs.survey_launch.succeeded). It just parses "message" to snake case for now.

func (EventHook) Run

func (h EventHook) Run(e *zerolog.Event, level zerolog.Level, msg string)

type Logger

type Logger struct {
	zerolog.Logger
}

func NewFromCtx

func NewFromCtx(ctx context.Context) *Logger

NewFromCtx creates a new defaultLogger from a context, which should contain RequestScopedFields. If the context does not contain then, then this method will NOT add them in.

func NewFromRequest

func NewFromRequest(r *http.Request) *Logger

NewFromRequest creates a new defaultLogger from a http.Request, which should contain RequestScopedFields. If the context does not contain then, then this method will NOT add them in.

func (*Logger) SetupFormatter

func (l *Logger) SetupFormatter(farm string)

SetupFormatter decides the output formatter based on the environment where the app is running on. It uses text formatter with color if you run the app locally, while using json formatter if it's running on the cloud.

func (*Logger) WithCaller

func (l *Logger) WithCaller() *Logger

func (*Logger) WithTimestamp

func (l *Logger) WithTimestamp() *Logger

Jump to

Keyboard shortcuts

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