Documentation ¶
Overview ¶
Package log provides a logger with different formatter and shipper.
Index ¶
- Constants
- func LogContextWithValues(ctx context.Context, values map[string]string) context.Context
- type DefaultFormatter
- type LogContext
- type LogFormatter
- type LogHandler
- func (logger *LogHandler) Debug(v ...interface{})
- func (logger *LogHandler) Debugf(message string, v ...interface{})
- func (logger *LogHandler) Error(v ...interface{})
- func (logger *LogHandler) Errorf(message string, v ...interface{})
- func (logger *LogHandler) Flush()
- func (logger *LogHandler) Info(v ...interface{})
- func (logger *LogHandler) Infof(message string, v ...interface{})
- func (logger *LogHandler) Log(logLevel LogLevel, v ...interface{})
- func (logger *LogHandler) Logf(logLevel LogLevel, message string, v ...interface{})
- func (logger *LogHandler) Status(v ...interface{})
- func (logger *LogHandler) Statusf(message string, v ...interface{})
- func (logger *LogHandler) WithContext(ctx context.Context)
- type LogLevel
- type LogShipper
- type Logger
- func AppendContextValues(logger Logger, values map[string]string) Logger
- func AppendFromLambdaContext(logger Logger, ctx context.Context) Logger
- func NewLogger(logLevel LogLevel, formatter LogFormatter, shipper LogShipper) Logger
- func NewLoggerFromConfig(conf config.Config, secretsManager secrets.SecretsManager) Logger
- func WithK8sContext(logger Logger) Logger
- func WithNameSpace(logger Logger, namespace string) Logger
- type LogzioJsonFormatter
- type LogzioShipper
- type StdoutShipper
Constants ¶
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" )
const ENV_LOGLEVEL = "LOGLEVEL"
ENV_LOGLEVEL defines the environment variable which can be used to set a log level.
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
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.
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.
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
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
const MESSAGE_STACK_SIZE = 500
MESSAGE_STACK_SIZE defines the capacity of internal message buffer. Can be set by config: log.logzio.messagestacksize
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
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 ¶
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.
func LogLevelByName ¶
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
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) SyslogLevel ¶ added in v1.1.2
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
appendContextValues adds passed values to context fo given logger.
func AppendFromLambdaContext ¶ added in v1.2.2
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
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
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.