sd

package module
v7.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2021 License: BSD-2-Clause Imports: 14 Imported by: 0

README

Install

go get github.com/aletheia7/sd
cd <sd location>
go test -v

Older systemd versions used libsystemd-journal. Change the following line if you have libsystemd-journal:

// #cgo pkg-config: --cflags --libs libsystemd

to

// #cgo pkg-config: --cflags --libs libsystemd-journal

Documentation

New_journal() and New_journal_m() create a Journal struct. Journal.Emerg(), Journal.Alert(), Journal.Crit(), Journal.Err(), Journal.Warning(), Journal.Notice(), Journal.Info(), Journal.Debug() write to the systemd journal.

Each method contains a format *f method; i.e. Infof, Errf.

Each method also contains a *_a (array variation) method that allows sending your own fields as an array of SOMEFILE=value strings. An *_a_f variation supports fmt.Printf style arguments.

Each method contains a *_m (map variation) method that allows sending your own fields. The map suppports string and []byte (binary).

Each method also contains a *_m_f (map & format variation) method that supports fmt.Printf style arguments.

Each of the methods will add journal fields GO_FILE, and GO_FUNC fields to the journal to indicate where the methods were called. The *_m_f methods can take nil map in order to only use the format functionality.

Helpful Hints

  • You may need to increase RateLimitInterval and/or RateLimitBurst settings in journald.conf when sending large amounts of data to the journal. Data will not appear in the log when settings are too low.
  • journalctl will truncate output by default. journalctl uses less as it's pager and sets it's own defaults via SYSTEMD_LESS. To restore the output, set the SYSTEMD_LESS environment variable; i.e. export SYSTEMD_LESS=FRXMK. See man journalctl.

Example

package main
import (
	"io"
	"log"
	"sd"
)

func main() {

	j := sd.New_journal()
	j.Alert("Alert example")

	// COMMENT_2_BINARY = abcNULLabc
	m := map[string]interface{}{"COMMENT_1": "This function ran successfully",
		"COMMENT_2_BINARY": []byte{0x61, 0x62, 0x63, 0x00, 0x61, 0x62, 0x63},
	}

	// Use: "journal -f --output verbose" to see fields
	j.Alert_m(m, "Alert_m exmaple")

	j.Alert_m_f(m, "Alert_m_f example: Salary: %v, Year: %v", 0.00, 2014)

	// Use log package
	// Remove ANSI escape sequences
	// systemd will convert messages to binary with ANSI escapes sequences
	sd.Set_default_remove_ansi_escape(true)
	j := sd.New_journal()
	// systemd will output red text
	j.Set_writer_priority(sd.Log_err)
	// Hello World
	// Hello is green
	// World is yellow
	s := "\x1b[32mHello\x1b[0m \x1b[93mWorld\x1b[0m"
	log.SetFlags(0)
	// Send to stderr and systemd-journald
	log.SetOutput(io.MultiWriter(os.Stderr, j))
	log.Println(s)
}

Ouput

# tail the systemd journal 
journal -f --output verbose

License

Use of this source code is governed by a BSD-2-Clause license that can be found in the LICENSE file.

BSD-2-Clause License

Documentation

Overview

Package sd provides methods to write to the systemd-journal.

Index

Examples

Constants

View Source
const (
	// bit flags
	Remove_journal remove_ansi_escape = 1 << iota
	Remove_writer
)
View Source
const (
	Sd_message = "MESSAGE"
	// Used in Set_default_fields(). systemd provides a default
	Sd_tag = "SYSLOG_IDENTIFIER"
)

See http://www.freedesktop.org/software/systemd/man/SD_JOURNAL_SUPPRESS_LOCATION.html, or man sd_journal_print, for valid systemd journal fields.

Variables

These are log/syslog.Priority values.

Functions

func Set_default_colors

func Set_default_colors(colors map[Priority]Writer_option)

Set default colors for io.Writer.

default: red (bold, highlight): Log_alert, Log_crti, Log_err, orange (bold, highlight): Log_warning, Log_notice

example: map[Priority]string{Log_err: ansi.ColorCode("green")}

func Set_default_disable_journal

func Set_default_disable_journal(disable bool) option

Journal output will be disabled. Useful for just stdout/stderr logging with color.

func Set_default_remove_ansi

func Set_default_remove_ansi(rm remove_ansi_escape) option

Sets the package level/default remove_ansi_escape and the current *Journal intance. Returns previous default remove_ansi_escape.

func Set_default_remove_ansi_escape

func Set_default_remove_ansi_escape(rm remove_ansi_escape)

Set default_remove_ansi_escape will set the default value for a new Journal.

func Set_default_writer

func Set_default_writer(w io.Writer) option

Set output to an additional io.Writer

func Set_default_writer_stderr

func Set_default_writer_stderr() option

func Set_default_writer_stdout

func Set_default_writer_stdout() option

func Set_field

func Set_field(name string, value interface{}) option

Sets the journal field name to value. The field will be removed when value is nil. An invalid name will be silently ignored. See info for Sd_tag.

func Set_message_id

func Set_message_id(uuid string)

Set_message_id sets the systemd MESSAGE_ID (UUID) for all Journal (Global) instances. Generate an application UUID with journalctl --new-id128. See man journalctl.

uuid is unset with ""

func Set_priority

func Set_priority(p Priority) option

func Set_remove_ansi

func Set_remove_ansi(rm remove_ansi_escape) option

func Set_writer

func Set_writer(w io.Writer) option

Types

type Journal

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

Journal can contain default systemd fields. See Set_default_fields().

Example
j := sd.New_journal()
j.Alert("Alert example")

// COMMENT_2_BINARY = abcNULLabc
m := map[string]interface{}{"COMMENT_1": "This function ran successfully",
	"COMMENT_2_BINARY": []byte{0x61, 0x62, 0x63, 0x00, 0x61, 0x62, 0x63},
}

// Use: "journal -f --output verbose" to see fields
j.Alert_m(m, "Alert_m exmaple")

j.Alert_m_f(m, "Alert_m_f example: Salary: %v, Year: %v", 0.00, 2014)
Output:

func New

func New(opt ...option) *Journal

New makes a Journal

func New_journal

func New_journal() *Journal

New_journal makes a Journal.

func New_journal_m

func New_journal_m(default_fields map[string]interface{}) *Journal

New_journal_m makes a Journal. The allowable interface{} values are string and []byte. A copy of []byte is made.

func (*Journal) Alert

func (j *Journal) Alert(a ...interface{}) error

Alert sends a message with Log_alert Priority (syslog severity). a ...interface{}: fmt.Println formating will become MESSAGE; see man systemd.journal-fields.

func (*Journal) Alert_a

func (j *Journal) Alert_a(fields []string, a ...interface{}) error

Alert_a sends a message with Log_alert Priority (syslog severity). fields: your user-defined systemd.journal-fields. a ...interface{}: fmt.Println formating will become MESSAGE; see man systemd.journal-fields.

func (*Journal) Alert_a_f

func (j *Journal) Alert_a_f(fields []string, format string, a ...interface{}) error

Alert_a_f sends a message with Log_alert Priority (syslog severity). The message is formed via fmt.Printf style arguments fields: your user-defined systemd.journal-fields. format string, a ...interface{}: see fmt.Printf.

func (*Journal) Alert_m

func (j *Journal) Alert_m(fields map[string]interface{}, a ...interface{}) error

Alert_m sends a message with Log_alert Priority (syslog severity). fields: your user-defined systemd.journal-fields. a ...interface{}: fmt.Println formating will become MESSAGE; see man systemd.journal-fields.

func (*Journal) Alert_m_f

func (j *Journal) Alert_m_f(fields map[string]interface{}, format string, a ...interface{}) error

Alert_m_f sends a message with Log_alert Priority (syslog severity). The message is formed via fmt.Printf style arguments fields: your user-defined systemd.journal-fields. format string, a ...interface{}: see fmt.Printf.

func (*Journal) Alertf

func (j *Journal) Alertf(format string, a ...interface{}) error

Alertf sends a message with Log_alert Priority (syslog severity). The message is formed via fmt.Printf style arguments format string, a ...interface{}: see fmt.Printf.

func (*Journal) Crit

func (j *Journal) Crit(a ...interface{}) error

func (*Journal) Crit_a

func (j *Journal) Crit_a(fields []string, a ...interface{}) error

func (*Journal) Crit_a_f

func (j *Journal) Crit_a_f(fields []string, format string, a ...interface{}) error

func (*Journal) Crit_m

func (j *Journal) Crit_m(fields map[string]interface{}, a ...interface{}) error

func (*Journal) Crit_m_f

func (j *Journal) Crit_m_f(fields map[string]interface{}, format string, a ...interface{}) error

func (*Journal) Critf

func (j *Journal) Critf(format string, a ...interface{}) error

func (*Journal) Debug

func (j *Journal) Debug(a ...interface{}) error

func (*Journal) Debug_a

func (j *Journal) Debug_a(fields []string, a ...interface{}) error

func (*Journal) Debug_a_f

func (j *Journal) Debug_a_f(fields []string, format string, a ...interface{}) error

func (*Journal) Debug_m

func (j *Journal) Debug_m(fields map[string]interface{}, a ...interface{}) error

func (*Journal) Debug_m_f

func (j *Journal) Debug_m_f(fields map[string]interface{}, format string, a ...interface{}) error

func (*Journal) Debugf

func (j *Journal) Debugf(format string, a ...interface{}) error

func (*Journal) Emerg

func (j *Journal) Emerg(a ...interface{}) error

func (*Journal) Emerg_m

func (j *Journal) Emerg_m(fields map[string]interface{}, a ...interface{}) error

func (*Journal) Emerg_m_f

func (j *Journal) Emerg_m_f(fields map[string]interface{}, format string, a ...interface{}) error

func (*Journal) Err

func (j *Journal) Err(a ...interface{}) error

func (*Journal) Err_a

func (j *Journal) Err_a(fields []string, a ...interface{}) error

func (*Journal) Err_a_f

func (j *Journal) Err_a_f(fields []string, format string, a ...interface{}) error

func (*Journal) Err_m

func (j *Journal) Err_m(fields map[string]interface{}, a ...interface{}) error

func (*Journal) Err_m_f

func (j *Journal) Err_m_f(fields map[string]interface{}, format string, a ...interface{}) error

func (*Journal) Errf

func (j *Journal) Errf(format string, a ...interface{}) error

func (*Journal) Info

func (j *Journal) Info(a ...interface{}) error

func (*Journal) Info_a

func (j *Journal) Info_a(fields []string, a ...interface{}) error

func (*Journal) Info_a_f

func (j *Journal) Info_a_f(fields []string, format string, a ...interface{}) error

func (*Journal) Info_m

func (j *Journal) Info_m(fields map[string]interface{}, a ...interface{}) error

func (*Journal) Info_m_f

func (j *Journal) Info_m_f(fields map[string]interface{}, format string, a ...interface{}) error

func (*Journal) Infof

func (j *Journal) Infof(format string, a ...interface{}) error

func (*Journal) Notice

func (j *Journal) Notice(a ...interface{}) error

func (*Journal) Notice_a

func (j *Journal) Notice_a(fields []string, a ...interface{}) error

func (*Journal) Notice_a_f

func (j *Journal) Notice_a_f(fields []string, format string, a ...interface{}) error

func (*Journal) Notice_m

func (j *Journal) Notice_m(fields map[string]interface{}, a ...interface{}) error

func (*Journal) Notice_m_f

func (j *Journal) Notice_m_f(fields map[string]interface{}, format string, a ...interface{}) error

func (*Journal) Noticef

func (j *Journal) Noticef(format string, a ...interface{}) error

func (*Journal) Option

func (o *Journal) Option(opt ...option) (previous option)

Option sets the options specified. It returns an option to restore the last arg's previous value.

func (*Journal) Send

func (j *Journal) Send(fields map[string]interface{}) error

Send writes to the systemd-journal. The keys must be uppercase strings without a leading _. The other send methods are easier to use. See Info(), Infom(), Info_m_f(), etc. A MESSAGE key in field is the only required field.

func (*Journal) Set_add_go_code_fields

func (j *Journal) Set_add_go_code_fields(use bool)

Set_add_go_code_fields will add GO_FILE (<file name>#<line #>),and GO_FUNC fields to the journal Send() methods, Info(), Err(), Warning(), etc.. Default: use_go_code_fields = true.

func (*Journal) Set_default_fields

func (j *Journal) Set_default_fields(fields map[string]interface{})

Default fields are sent with every Send(). Do not include MESSAGE, or Priority, as these fields are always sent. The allowable interface{} values are string and []byte. A copy of []byte is made.

func (*Journal) Set_writer_priority

func (j *Journal) Set_writer_priority(p Priority) *Journal

Set_writer_priority set the priority for the write() receiver. You'll probably want to use Set_remove_ansi(sd.Remove_journal). Default: Log_info.

func (*Journal) Stack_skip

func (j *Journal) Stack_skip(skip int) *Journal

Useful when file/line are not correct default: 4

func (*Journal) Warning

func (j *Journal) Warning(a ...interface{}) error

func (*Journal) Warning_a

func (j *Journal) Warning_a(fields []string, a ...interface{}) error

func (*Journal) Warning_a_f

func (j *Journal) Warning_a_f(fields []string, format string, a ...interface{}) error

func (*Journal) Warning_m

func (j *Journal) Warning_m(fields map[string]interface{}, a ...interface{}) error

func (*Journal) Warning_m_f

func (j *Journal) Warning_m_f(fields map[string]interface{}, format string, a ...interface{}) error

func (*Journal) Warningf

func (j *Journal) Warningf(format string, a ...interface{}) error

func (*Journal) Write

func (j *Journal) Write(b []byte) (int, error)

Writer implements io.Writer. Allows Journal to be used in the log package. You might want to use Set_remove_ansi(true). See http://godoc.org/log#SetOutput.

type Priority

type Priority string

type Writer_option

type Writer_option struct {
	Color        string
	Include_file bool
}

Directories

Path Synopsis
Package ansi is a small, fast library to create ANSI colored strings and codes.
Package ansi is a small, fast library to create ANSI colored strings and codes.

Jump to

Keyboard shortcuts

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