journalhook

package module
v0.0.0-...-9a0802d Latest Latest
Warning

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

Go to latest
Published: May 29, 2018 License: MIT Imports: 5 Imported by: 3

README

Systemd's Journal hook for logrus

Build Status Go Report Status

Installation

Install the package with:

go get -u github.com/ssgreg/journalhook

Usage

package main

import (
    "os"

    "github.com/sirupsen/logrus"
    "github.com/ssgreg/journalhook"

fun  main() {
    log := logrus.New()
    hook, err := journalhook.NewJournalHook()
    if err == nil {
        log.Hooks.Add(hook)
    }

    logEntry := log.WithFields(logrus.Fields{
        "n_goroutine": runtime.NumGoroutine(),
        "executable":  os.Args[0],
        "trace":       runtime.ReadTrace(),
    })

    logEntry.Info("Hello World!")

    // Make sure that journal connection will be successfully closed
    // and no message will be lost.
    logrus.Exit(0)
}

This is how it will look like:

{
  "__CURSOR": "s=f81e8eb7fd0941b089528d889c929c1f;i=f1;b=40582011948a4b1998bf5ca928517a0f;m=2345f2fc69;t=557cb96d25735;x=e26ade807f4bab79",
  "__REALTIME_TIMESTAMP": "1503906803898165",
  "__MONOTONIC_TIMESTAMP": "151497407593",
  "_BOOT_ID": "40582011948a4b1998bf5ca928517a0f",
  "PRIORITY": "6",
  "_UID": "0",
  "_GID": "0",
  "_CAP_EFFECTIVE": "a80425fb",
  "_MACHINE_ID": "78b67a34f030403aa4dc97056d5efced",
  "_HOSTNAME": "64620c2b0d13",
  "_TRANSPORT": "journal",
  "MESSAGE": "Hello World!",
  "N_GOROUTINE": "2",
  "TRACE": [ 103, 111, 32, 49, 46, 56, 32, 116, 114, 97, 99, 101, 0, 0, 0, 0 ],
  "_COMM": "usage",
  "EXECUTABLE": "./usage",
  "_PID": "7533",
  "_SOURCE_REALTIME_TIMESTAMP": "1503906803896890"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrToMsg

func ErrToMsg(entry *logrus.Entry)

ErrToMsg sets Message to the contents of the associated error, if Message is not already set.

Default journalctl output will only show the contents of the `MESSAGE` field. If the message is empty but the entry has an associated error, we replace the message with the contents of the error so that it is shown in the journalctl output by default.

This makes it possible to use `log.WithError(err).Error()` without providing an additional error message.

If a string is passed to `Error` function, this is used as the message.

func NormalizeFieldName

func NormalizeFieldName(s string) string

NormalizeFieldName returns field name acceptable by journald.

Types

type JournalHook

type JournalHook struct {
	Journal      *journald.Journal
	LogrusLevels []logrus.Level
	ErrToMsg     bool
}

JournalHook is the systemd-journald hook for logrus.

func NewJournalHook

func NewJournalHook() (*JournalHook, error)

NewJournalHook creates a hook to be added to an instance of logger.

func NewJournalHookWithErrToMsg

func NewJournalHookWithErrToMsg() (hook *JournalHook, err error)

NewJournalHookWithErrToMsg creates a hook, which converts associated errors to messages, to be added to an instance of logger

func NewJournalHookWithLevels

func NewJournalHookWithLevels(levels []logrus.Level) (*JournalHook, error)

NewJournalHookWithLevels creates a hook to be added to an instance of logger. It's also allowed to specify logrus levels to fire events for.

func (*JournalHook) Fire

func (h *JournalHook) Fire(entry *logrus.Entry) error

Fire writes a log entry to the systemd journal.

func (*JournalHook) Levels

func (h *JournalHook) Levels() []logrus.Level

Levels returns a slice of Levels the hook is fired for.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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