Giter VIP home page Giter VIP logo

sd's Introduction

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

sd's People

Contributors

aletheia7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

sd's Issues

misplaced // +build line

Hi. I've been surveying // +build usage in the Go ecosystem, and I noticed that in s.go, the line:

// +build linux,cgo

is inside a C /* / comment. The line will be ignored there; it must come before the / */ comment, followed by a blank line (important!).

Also, the ",cgo" is implied by the import "C" in the file, so you can use just // +build linux.

Also, the blank line between the C /* */ comment and the package line is keeping the big comment from being treated as package documentation. You probably want to remove that blank line.

Best,
Russ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.