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 ¶
ContextWithEnvConfig returns a new context with the given EnvConfig embedded as a value.
func ToSnakeCase ¶
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 ¶
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.
type Logger ¶
func NewFromCtx ¶
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 ¶
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 ¶
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.