log

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: Apache-2.0 Imports: 5 Imported by: 9

README

CI Go Report Card GoDoc

log

import "github.com/go-coldbrew/log"

Package log provides a minimal interface for structured logging in services. ColdBrew uses this log package for all logs. It provides a simple interface to log errors, warnings, info and debug messages. It also provides a mechanism to add contextual information to logs. available implementations of BaseLogger are in loggers package. You can also implement your own BaseLogger to use with this package.

How To Use

The simplest way to use this package is by calling static log functions to report particular level (error/warning/info/debug)

log.Error(...)
log.Warn(...)
log.Info(...)
log.Debug(...)

You can also initialize a new logger by calling 'log.NewLogger' and passing a loggers.BaseLogger implementation (loggers package provides a number of pre built implementations)

logger := log.NewLogger(gokit.NewLogger())
logger.Info(ctx, "key", "value")

Note:

Preferred logging output is in either logfmt or json format, so to facilitate these log function arguments should be in pairs of key-value
Contextual Logs

log package uses context.Context to pass additional information to logs, you can use 'loggers.AddToLogContext' function to add additional information to logs. For example in access log from service

{"@timestamp":"2018-07-30T09:58:18.262948679Z","caller":"http/http.go:66","error":null,"grpcMethod":"/AuthSvc.AuthService/Authenticate","level":"info","method":"POST","path":"/2.0/authenticate/","took":"1.356812ms","trace":"15592e1b-93df-11e8-bdfd-0242ac110002","transport":"http"}

we pass 'grpcMethod' from context, this information gets automatically added to all log calls called inside the service and makes debugging services much easier. ColdBrew also generates a 'trace' ID per request, this can be used to trace an entire request path in logs.

this package is based on https://github.com/carousell/Orion/tree/master/utils/log

Index

Constants

SupportPackageIsVersion1 is a compile-time assertion constant. Downstream packages reference this to enforce version compatibility.

const SupportPackageIsVersion1 = true

func AddToContext

func AddToContext(ctx context.Context, key string, value any) context.Context

AddToContext adds log fields to the provided context. Any info added here will be included in all logs that use the returned context. This is the preferred entry point for adding contextual logging fields and is implemented internally using loggers.AddToLogContext.

Example

package main

import (
	"context"

	"github.com/go-coldbrew/log"
)

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

	// Add per-request fields to context — these appear in all subsequent log lines
	ctx = log.AddToContext(ctx, "request_id", "abc-123")
	ctx = log.AddToContext(ctx, "user_id", "user-42")

	// All logs using this context now include request_id and user_id
	log.Info(ctx, "msg", "processing request", "step", "validation")
	log.Info(ctx, "msg", "request complete", "status", "ok", "duration_ms", 42)
}

func Debug

func Debug(ctx context.Context, args ...any)

Debug writes out a debug log to global logger This is a convenience function for GetLogger().Log(ctx, loggers.DebugLevel, 1, args...)

func Error

func Error(ctx context.Context, args ...any)

Error writes out an error log to global logger This is a convenience function for GetLogger().Log(ctx, loggers.ErrorLevel, 1, args...)

Example

package main

import (
	"context"

	"github.com/go-coldbrew/log"
)

func main() {
	ctx := context.Background()
	log.Error(ctx, "msg", "database connection failed", "host", "db.internal", "port", 5432, "retry_in", "5s")
}

func GetLevel

func GetLevel() loggers.Level

GetLevel returns the current log level This is useful for checking if a log level is enabled

func GetOverridenLogLevel

func GetOverridenLogLevel(ctx context.Context) (loggers.Level, bool)

GetOverridenLogLevel fetches overriden log level from context If no log level is overriden, it returns false If log level is overriden, it returns the log level and true

func Info

func Info(ctx context.Context, args ...any)

Info writes out an info log to global logger This is a convenience function for GetLogger().Log(ctx, loggers.InfoLevel, 1, args...)

Example

package main

import (
	"context"

	"github.com/go-coldbrew/log"
)

func main() {
	ctx := context.Background()
	log.Info(ctx, "msg", "order processed", "order_id", "ORD-123", "items", 3)
}

func OverrideLogLevel

func OverrideLogLevel(ctx context.Context, level loggers.Level) context.Context

OverrideLogLevel allows the default log level to be overridden from request context This is useful when you want to override the log level for a specific request For example, you can set the log level to debug for a specific request while the default log level is set to info

func SetLevel

func SetLevel(level loggers.Level)

SetLevel sets the log level to filter logs

func SetLogger

func SetLogger(l Logger)

SetLogger sets the global logger

func Warn

func Warn(ctx context.Context, args ...any)

Warn writes out a warning log to global logger This is a convenience function for GetLogger().Log(ctx, loggers.WarnLevel, 1, args...)

type Logger

Logger interface is implemnted by the log implementation to provide the log methods to the application code.

type Logger interface {
    loggers.BaseLogger
    // Debug logs a message at level Debug.
    // ctx is used to extract the request id and other context information.
    Debug(ctx context.Context, args ...any)
    // Info logs a message at level Info.
    // ctx is used to extract the request id and other context information.
    Info(ctx context.Context, args ...any)
    // Warn logs a message at level Warn.
    // ctx is used to extract the request id and other context information.
    Warn(ctx context.Context, args ...any)
    // Error logs a message at level Error.
    // ctx is used to extract the request id and other context information.
    Error(ctx context.Context, args ...any)
}

func GetLogger
func GetLogger() Logger

GetLogger returns the global logger If the global logger is not set, it will create a new one with slog logger

func NewLogger
func NewLogger(log loggers.BaseLogger) Logger

NewLogger creates a new logger with a provided BaseLogger The default logger is slog logger

Generated by gomarkdoc

Documentation

Overview

Package log provides a minimal interface for structured logging in services. ColdBrew uses this log package for all logs. It provides a simple interface to log errors, warnings, info and debug messages. It also provides a mechanism to add contextual information to logs. available implementations of BaseLogger are in loggers package. You can also implement your own BaseLogger to use with this package.

How To Use

The simplest way to use this package is by calling static log functions to report particular level (error/warning/info/debug)

log.Error(...)
log.Warn(...)
log.Info(...)
log.Debug(...)

You can also initialize a new logger by calling 'log.NewLogger' and passing a loggers.BaseLogger implementation (loggers package provides a number of pre built implementations)

logger := log.NewLogger(gokit.NewLogger())
logger.Info(ctx, "key", "value")

Note:

Preferred logging output is in either logfmt or json format, so to facilitate these log function arguments should be in pairs of key-value

Contextual Logs

log package uses context.Context to pass additional information to logs, you can use 'loggers.AddToLogContext' function to add additional information to logs. For example in access log from service

{"@timestamp":"2018-07-30T09:58:18.262948679Z","caller":"http/http.go:66","error":null,"grpcMethod":"/AuthSvc.AuthService/Authenticate","level":"info","method":"POST","path":"/2.0/authenticate/","took":"1.356812ms","trace":"15592e1b-93df-11e8-bdfd-0242ac110002","transport":"http"}

we pass 'grpcMethod' from context, this information gets automatically added to all log calls called inside the service and makes debugging services much easier. ColdBrew also generates a 'trace' ID per request, this can be used to trace an entire request path in logs.

this package is based on https://github.com/carousell/Orion/tree/master/utils/log

Index

Examples

Constants

View Source
const SupportPackageIsVersion1 = true

SupportPackageIsVersion1 is a compile-time assertion constant. Downstream packages reference this to enforce version compatibility.

Variables

This section is empty.

Functions

func AddToContext added in v0.2.6

func AddToContext(ctx context.Context, key string, value any) context.Context

AddToContext adds log fields to the provided context. Any info added here will be included in all logs that use the returned context. This is the preferred entry point for adding contextual logging fields and is implemented internally using loggers.AddToLogContext.

Example
package main

import (
	"context"

	"github.com/go-coldbrew/log"
)

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

	// Add per-request fields to context — these appear in all subsequent log lines
	ctx = log.AddToContext(ctx, "request_id", "abc-123")
	ctx = log.AddToContext(ctx, "user_id", "user-42")

	// All logs using this context now include request_id and user_id
	log.Info(ctx, "msg", "processing request", "step", "validation")
	log.Info(ctx, "msg", "request complete", "status", "ok", "duration_ms", 42)
}

func Debug

func Debug(ctx context.Context, args ...any)

Debug writes out a debug log to global logger This is a convenience function for GetLogger().Log(ctx, loggers.DebugLevel, 1, args...)

func Error

func Error(ctx context.Context, args ...any)

Error writes out an error log to global logger This is a convenience function for GetLogger().Log(ctx, loggers.ErrorLevel, 1, args...)

Example
package main

import (
	"context"

	"github.com/go-coldbrew/log"
)

func main() {
	ctx := context.Background()
	log.Error(ctx, "msg", "database connection failed", "host", "db.internal", "port", 5432, "retry_in", "5s")
}

func GetLevel

func GetLevel() loggers.Level

GetLevel returns the current log level This is useful for checking if a log level is enabled

func GetOverridenLogLevel added in v0.2.2

func GetOverridenLogLevel(ctx context.Context) (loggers.Level, bool)

GetOverridenLogLevel fetches overriden log level from context If no log level is overriden, it returns false If log level is overriden, it returns the log level and true

func Info

func Info(ctx context.Context, args ...any)

Info writes out an info log to global logger This is a convenience function for GetLogger().Log(ctx, loggers.InfoLevel, 1, args...)

Example
package main

import (
	"context"

	"github.com/go-coldbrew/log"
)

func main() {
	ctx := context.Background()
	log.Info(ctx, "msg", "order processed", "order_id", "ORD-123", "items", 3)
}

func OverrideLogLevel added in v0.2.2

func OverrideLogLevel(ctx context.Context, level loggers.Level) context.Context

OverrideLogLevel allows the default log level to be overridden from request context This is useful when you want to override the log level for a specific request For example, you can set the log level to debug for a specific request while the default log level is set to info

func SetLevel

func SetLevel(level loggers.Level)

SetLevel sets the log level to filter logs

func SetLogger

func SetLogger(l Logger)

SetLogger sets the global logger

func Warn

func Warn(ctx context.Context, args ...any)

Warn writes out a warning log to global logger This is a convenience function for GetLogger().Log(ctx, loggers.WarnLevel, 1, args...)

Types

type Logger

type Logger interface {
	loggers.BaseLogger
	// Debug logs a message at level Debug.
	// ctx is used to extract the request id and other context information.
	Debug(ctx context.Context, args ...any)
	// Info logs a message at level Info.
	// ctx is used to extract the request id and other context information.
	Info(ctx context.Context, args ...any)
	// Warn logs a message at level Warn.
	// ctx is used to extract the request id and other context information.
	Warn(ctx context.Context, args ...any)
	// Error logs a message at level Error.
	// ctx is used to extract the request id and other context information.
	Error(ctx context.Context, args ...any)
}

Logger interface is implemnted by the log implementation to provide the log methods to the application code.

func GetLogger

func GetLogger() Logger

GetLogger returns the global logger If the global logger is not set, it will create a new one with slog logger

func NewLogger

func NewLogger(log loggers.BaseLogger) Logger

NewLogger creates a new logger with a provided BaseLogger The default logger is slog logger

Directories

Path Synopsis
Package loggers provides loggers implementation for log package
Package loggers provides loggers implementation for log package
gokit
Deprecated: Package gokit provides BaseLogger implementation for go-kit/log.
Deprecated: Package gokit provides BaseLogger implementation for go-kit/log.
logrus
Deprecated: Package logrus provides a BaseLogger implementation for logrus.
Deprecated: Package logrus provides a BaseLogger implementation for logrus.
slog
Package slog provides a BaseLogger implementation for log/slog.
Package slog provides a BaseLogger implementation for log/slog.
stdlog
Package stdlog provides a BaseLogger implementation for golang "log" package
Package stdlog provides a BaseLogger implementation for golang "log" package
zap
Package zap provides a BaseLogger implementation for uber/zap
Package zap provides a BaseLogger implementation for uber/zap
Package wrap provides multiple wrap functions to wrap log implementation of other log packages
Package wrap provides multiple wrap functions to wrap log implementation of other log packages

Jump to

Keyboard shortcuts

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