logfjournald

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2021 License: MIT Imports: 7 Imported by: 0

README

logf Appender and Encoder for systemd Journal

GoDoc Build Status Go Report Status Coverage Status

Package logfjournald provides logf Appender and Encoder for systemd Journal. It supports structured logging and bulk operations with zero allocs.

Example

The following example creates the new logf logger with logfjournald appender.

package main

import (
    "runtime"

    "github.com/ssgreg/logf"
    "github.com/ssgreg/logfjournald"
)

func main() {
    // Create journald Appender with default journald Encoder.
    appender, appenderClose := logfjournald.NewAppender(logfjournald.NewEncoder.Default())
    defer appenderClose()

    // Create ChannelWriter with journald Encoder.
    writer, writerClose := logf.NewChannelWriter(logf.ChannelWriterConfig{
        Appender: appender,
    })
    defer writerClose()

    // Create Logger with ChannelWriter.
    logger := logf.NewLogger(logf.LevelInfo, writer)

    logger.Info("got cpu info", logf.Int("count", runtime.NumCPU()))
}

The JSON representation of the journal entry this generates:

{
  "TS": "2018-11-01T07:25:18Z",
  "PRIORITY": "6",
  "LEVEL": "info",
  "MESSAGE": "got cpu info",
  "COUNT": "4",
}

Documentation

Index

Constants

View Source
const (
	DefaultFieldKeyLevel  = "LEVEL"
	DefaultFieldKeyTime   = "TS"
	DefaultFieldKeyName   = "LOGGER"
	DefaultFieldKeyCaller = "CALLER"

	// Systemd journal dependent field keys.
	DefaultFieldKeyPriority = "PRIORITY"
	DefaultFieldKeyMessage  = "MESSAGE"
)

Default field keys.

Variables

View Source
var NewEncoder = jsonEncoderGetter(
	func(c EncoderConfig, mf logf.TypeEncoderFactory) logf.Encoder {
		return &encoder{c.WithDefaults(), mf, nil, logf.NewCache(100)}
	},
)

NewEncoder creates the new instance of Encoder for journal Encode with the given EncoderConfig and TypeEncoderFactory for non-basic types.

View Source
var NewTypeEncoderFactory = jsonTypeEncoderFactoryGetter(
	func(c EncoderConfig, mf logf.TypeEncoderFactory) logf.TypeEncoderFactory {
		return &encoder{c.WithDefaults(), mf, nil, nil}
	},
)

NewTypeEncoderFactory creates the new instance of TypeEncoderFactory for journal message Encode with the given EncoderConfig and another TypeEncoderFactory for non-basic types.

Functions

This section is empty.

Types

type AppenderCloseFunc

type AppenderCloseFunc func() error

AppenderCloseFunc allows to close underlying journal at the end of Appender life cycle.

func NewAppender

func NewAppender(enc logf.Encoder) (logf.Appender, AppenderCloseFunc)

NewAppender creates the new instance of journal appender with the given Encoder.

type EncoderConfig

type EncoderConfig struct {
	FieldKeyTime   string
	FieldKeyLevel  string
	FieldKeyName   string
	FieldKeyCaller string

	// DisableFieldLevel disabled the Time field.
	// Native journal's time field (when the message was added to the
	// journal) stayes enabled.
	DisableFieldTime bool

	// DisableFieldLevel disables the Level field.
	DisableFieldLevel bool

	// DisableFieldPriority disables the native journal's PRIORITY field.
	DisableFieldPriority bool

	// DisableFieldName disables the logger name filed.
	DisableFieldName bool

	// DisableFieldCaller disables the caller field.
	DisableFieldCaller bool

	EncodeTime     logf.TimeEncoder
	EncodeDuration logf.DurationEncoder
	EncodeError    logf.ErrorEncoder
	EncodeLevel    logf.LevelEncoder
	EncodeCaller   logf.CallerEncoder
}

EncoderConfig allows to configure journal Encoder.

Note that PRIORITY and MESSAGE field names could not be configured. Both PRIORITY and FieldKeyLevel fields are usable.

  • PRIORITY allows to use journal native features such as

filtering and color highlighting.

  • FieldKeyLevel allows to check original severity level.

func (EncoderConfig) WithDefaults

func (c EncoderConfig) WithDefaults() EncoderConfig

WithDefaults returns the new config in which all uninitialized fields are filled with their default values.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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