log

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2024 License: MIT Imports: 16 Imported by: 34

README

Go Reference GitHub go.mod Go version GitHub tag (latest SemVer) Go Report Card

Logger

Provides a logger with different formatter and shipper. By default it logs to stdoutm but you can use log shipping to Logz.io as well.

Documentation

Overview

Package log provides a logger with different formatter and shipper.

Index

Constants

View Source
const (
	// LogCtxRequestId is a context key used for a request id.
	LogCtxRequestId = "requestid"
	// LogCtxLogLevel is a context key for used log level.
	LogCtxLogLevel = "loglevel"
	// LogCtxMessage is a context key for log messages.
	LogCtxMessage = "message"
	// LogCtxTimestamp is a context key for a timestamp.
	LogCtxTimestamp = "timestamp"
	// LogCtxNamespace is a context key for a namespace.
	LogCtxNamespace = "namespace"
	// LogCtxDomain is a context key for a domain.
	LogCtxDomain = "domain"
	// LogCtxHostname is a context keyfor a hostname.
	LogCtxHostname = "hostname"
	// LogCtxIp is a context key for an Ip (v4).
	LogCtxIp = "ip"
	// LogCtxK8sNode is a context key for kubernetes node name.
	LogCtxK8sNode = "k8s_node"
	// LogCtxK8sPod is a context key for a kubernetes pod name.
	LogCtxK8sPod = "k8s_pod"
)
View Source
const ENV_LOGLEVEL = "LOGLEVEL"

ENV_LOGLEVEL defines the environment variable which can be used to set a log level.

View Source
const LOGZIO_BATCH_SIZE = 10

LOGZIO_BATCH_SIZE is the default batch size the Logz.io shipper will use. Can be set by config: log.logzio.batchsize

View Source
const LOGZIO_TIMESTAMP_FORMAT = "2006-01-02T15:04:05.999Z"

LOGZIO_TIMESTAMP_FORMAT is Logz.io timestamp format which will be used for @timestamp value in a log record.

View Source
const LOGZIO_TOKEN_KEY = "LOGZIO_TOKEN"

LOGZIO_TOKEN_KEY defines the key which will be used to obtain the Logz.io token from secrets mananger.

View Source
const LOGZIO_URL = "https://listener.logz.io:8071/"

LOGZIO_URL define the endpoint all logs will be shipped to. Can be set by config: log.logzio.url

View Source
const MESSAGE_READ_TIMEOUT = 50 * time.Millisecond

MESSAGE_READ_TIMEOUT defines the time a shipper will wait for new messages during reading from message stack. Can be set by config: log.logzio.messagereadtimeout

View Source
const MESSAGE_STACK_SIZE = 500

MESSAGE_STACK_SIZE defines the capacity of internal message buffer. Can be set by config: log.logzio.messagestacksize

View Source
const SHIPMENT_STACK_SIZE = 2

SHIPMENT_STACK_SIZE defines the number of worker which can ship logs in parallel to Logz.io. Can be set by config: log.logzio.shipmentstacksize

View Source
const SHIPMENT_WAIT_TIMEOUT = 1 * time.Second

SHIPMENT_WAIT_TIMEOUT defines the time the shipper will wait to get a slot from shipment stack. Can be set by config: log.logzio.shipmenttimeout

Variables

This section is empty.

Functions

func LogContextWithValues

func LogContextWithValues(ctx context.Context, values map[string]string) context.Context

LogContextWithValues adds passed log values to passed context.

Types

type DefaultFormatter

type DefaultFormatter struct {
}

DefaultFormatter is a fallback formatter to convert log values into a message.

type LogContext

type LogContext struct {
	// contains filtered or unexported fields
}

LogContext provides context values for logging.

func DefaultContextForK8s

func DefaultContextForK8s() LogContext

DefaultContextForK8s returns a log context with kubernetes node and pod name if both has benn set as K8S_NODE_NAME and K8S_POD_NAME during deployment.

func DefaultContextForNodes

func DefaultContextForNodes() LogContext

DefaultContextForNodes returns a log context with hostname and ip (v4).

func (LogContext) AppendValues

func (logContext LogContext) AppendValues(values map[string]string) LogContext

AppendValues reads values from current log context, append passed values and returns a new log context.

func (LogContext) String

func (logContext LogContext) String() string

String creates a string representation of internal values map.

type LogFormatter

type LogFormatter interface {
	// contains filtered or unexported methods
}

LogFormatter will convert passed log values into a suitable log message.

type LogHandler

type LogHandler struct {
	// contains filtered or unexported fields
}

LogHandler provides methods to log messges with different log level and takes care about formatting and shipping logs.

func (*LogHandler) Debug

func (logger *LogHandler) Debug(v ...interface{})

Debug will create a log message with given values for log level Debug.

func (*LogHandler) Debugf

func (logger *LogHandler) Debugf(message string, v ...interface{})

Debugf format given log message for log level Debug.

func (*LogHandler) Error

func (logger *LogHandler) Error(v ...interface{})

Error will create a log message with given values for log level Error.

func (*LogHandler) Errorf

func (logger *LogHandler) Errorf(message string, v ...interface{})

Errorf format given log message for log level Error.

func (*LogHandler) Flush

func (logger *LogHandler) Flush()

Flush will force it's log shipper to deliver all remaining log messages.

func (*LogHandler) Info

func (logger *LogHandler) Info(v ...interface{})

Info will create a log message with given values for log level Info.

func (*LogHandler) Infof

func (logger *LogHandler) Infof(message string, v ...interface{})

Infof format given log message for log level Info.

func (*LogHandler) Log added in v1.2.0

func (logger *LogHandler) Log(logLevel LogLevel, v ...interface{})

Log passed log message with given log level.

func (*LogHandler) Logf added in v1.2.0

func (logger *LogHandler) Logf(logLevel LogLevel, message string, v ...interface{})

Log passed log message with given log level.

func (*LogHandler) Status added in v1.1.0

func (logger *LogHandler) Status(v ...interface{})

Status will create a log message with given values for log level Status.

func (*LogHandler) Statusf added in v1.1.0

func (logger *LogHandler) Statusf(message string, v ...interface{})

Statusf format given log message for log level Status.

func (*LogHandler) WithContext

func (logger *LogHandler) WithContext(ctx context.Context)

WithContext applies the log context.

type LogLevel

type LogLevel int

LogLevel defines the log level, e.g. error or debug.

const (
	// None diables logging.
	None LogLevel = iota * 100
	// Status can be used to log messages.
	Status
	// Error is a log level for errors.
	Error
	// Info is a log level for status information.
	Info
	// Debug is a log level for dev outputs.
	Debug
)

func LogLevelByName

func LogLevelByName(logLevelName string) LogLevel

LogLevelByName will try to convert passed name of a log level into a log level. If there's no suitable log level for a given name, log level None is returned, which disables logging.

func LogLevelFromConfig added in v1.1.2

func LogLevelFromConfig(conf config.Config) LogLevel

LogLevelFromConfig reads log level from config using "log.loglevel" as key. If there's no log level defined in passed config default Error will be returned.

func LogLevelFromEnv

func LogLevelFromEnv() LogLevel

LogLevelFromEnv will lookup for environment variable defined by ENV_LOGLEVEL and if it exists call LogLevelByName to convert it's value to a log level.

func (LogLevel) String

func (logLevel LogLevel) String() string

String returns the name of a log level.

func (LogLevel) SyslogLevel added in v1.1.2

func (logLevel LogLevel) SyslogLevel() int

SyslogLevel returns corresponding syslog(3) log level.

type LogShipper

type LogShipper interface {
	// contains filtered or unexported methods
}

LogShipper will take care of sending logs to a defined target.

type Logger

type Logger interface {

	// WithContext sets a given log context.
	WithContext(context.Context)

	// Statusf logs a formated message with log level Status.
	Statusf(message string, v ...interface{})

	// Status logs given message with log level Status.
	Status(v ...interface{})

	// Errorf logs a formated message with log level Error.
	Errorf(message string, v ...interface{})

	// Error logs given message with log level Error.
	Error(v ...interface{})

	// Errorf logs a formated message with log level Info.
	Infof(message string, v ...interface{})

	// Error logs given message with log level Info.
	Info(v ...interface{})

	// Errorf logs a formated message with log level Debug.
	Debugf(message string, v ...interface{})

	// Error logs given message with log level Debug.
	Debug(v ...interface{})

	// Logs a formated message with given log level.
	Logf(logLevel LogLevel, message string, v ...interface{})

	// Logs passed message with given log level.
	Log(logLevel LogLevel, v ...interface{})

	// FLush tells the log shipper to cleat it's internal message queue.
	Flush()
}

Logger is an infterface for different types of logger.

func AppendContextValues added in v1.2.1

func AppendContextValues(logger Logger, values map[string]string) Logger

appendContextValues adds passed values to context fo given logger.

func AppendFromLambdaContext added in v1.2.2

func AppendFromLambdaContext(logger Logger, ctx context.Context) Logger

AppendFromLambdaContext appends some values from given context, e.g. a request id, to current log context if passed context is a AWS Lambda context.

func NewLogger

func NewLogger(logLevel LogLevel, formatter LogFormatter, shipper LogShipper) Logger

NewLogger returns a new logger with passed log level, formatter and shipper. If you omit formatter and shipper the DefaultFormatter and StdoutShipper will be used.

func NewLoggerFromConfig

func NewLoggerFromConfig(conf config.Config, secretsManager secrets.SecretsManager) Logger

NewLoggerFromConfig returns a new logger created depending on passed config.

func WithK8sContext added in v1.1.1

func WithK8sContext(logger Logger) Logger

WithK8sContext appends kubernetes values from environment variables as context At the moment following environment variables are supported:

K8S_NODE_NAME 	- Node name
K8S_POD_NAME	- Pod name

func WithNameSpace added in v1.1.1

func WithNameSpace(logger Logger, namespace string) Logger

WithNameSpace appends passed namespace as log context

type LogzioJsonFormatter

type LogzioJsonFormatter struct {
}

LogzioJsonFormatter will convert passed values to a JSON record suitlable for an import at Logz.io.

type LogzioShipper

type LogzioShipper struct {
	// contains filtered or unexported fields
}

LogzioShipper will deliver log messages to Logz.io.

type StdoutShipper

type StdoutShipper struct {
}

StdoutShipper will print given log messages on stdout.

Jump to

Keyboard shortcuts

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