Documentation ¶
Overview ¶
Package sdjournal provides a low-level Go interface to the systemd journal wrapped around the sd-journal C API.
All public read methods map closely to the sd-journal API functions. See the sd-journal.h documentation[1] for information about each function.
To write to the journal, see the pure-Go "journal" package
[1] http://www.freedesktop.org/software/systemd/man/sd-journal.html
Index ¶
- Constants
- Variables
- func GetCatalogForMessageID(messageId string) (string, error)
- type Journal
- func (j *Journal) AddConjunction() error
- func (j *Journal) AddDisjunction() error
- func (j *Journal) AddMatch(match string) error
- func (j *Journal) Close() error
- func (j *Journal) FlushMatches()
- func (j *Journal) GetCatalog() (string, error)
- func (j *Journal) GetCursor() (string, error)
- func (j *Journal) GetData(field string) (string, error)
- func (j *Journal) GetDataAll() (JournalEntry, error)
- func (j *Journal) GetDataValue(field string) (string, error)
- func (j *Journal) GetRealtimeUsec() (uint64, error)
- func (j *Journal) GetUsage() (uint64, error)
- func (j *Journal) Next() (int, error)
- func (j *Journal) NextSkip(skip uint64) (uint64, error)
- func (j *Journal) Previous() (uint64, error)
- func (j *Journal) PreviousSkip(skip uint64) (uint64, error)
- func (j *Journal) SeekCursor(cursor string) error
- func (j *Journal) SeekHead() error
- func (j *Journal) SeekMonotonicUsec(boot_id string, usec uint64) error
- func (j *Journal) SeekRealtimeUsec(usec uint64) error
- func (j *Journal) SeekTail() error
- func (j *Journal) SetDataThreshold(threshold uint64) error
- func (j *Journal) TestCursor(cursor string) (bool, error)
- func (j *Journal) Wait(timeout time.Duration) int
- type JournalEntry
- type JournalReader
- func (r *JournalReader) Close() error
- func (r *JournalReader) Follow(until <-chan time.Time, writer io.Writer) (err error)
- func (r *JournalReader) FollowJournal(until <-chan int, writer chan<- JournalEntry) (err error)
- func (r *JournalReader) Read(b []byte) (int, error)
- func (r *JournalReader) ReadEntry() (JournalEntry, error)
- type JournalReaderConfig
- type Match
Constants ¶
const ( SD_JOURNAL_FIELD_SYSTEMD_UNIT = "_SYSTEMD_UNIT" SD_JOURNAL_FIELD_MESSAGE = "MESSAGE" SD_JOURNAL_FIELD_PID = "_PID" SD_JOURNAL_FIELD_UID = "_UID" SD_JOURNAL_FIELD_GID = "_GID" SD_JOURNAL_FIELD_HOSTNAME = "_HOSTNAME" SD_JOURNAL_FIELD_MACHINE_ID = "_MACHINE_ID" )
Journal entry field strings which correspond to: http://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html
const ( SD_JOURNAL_NOP = int(C.SD_JOURNAL_NOP) SD_JOURNAL_APPEND = int(C.SD_JOURNAL_APPEND) SD_JOURNAL_INVALIDATE = int(C.SD_JOURNAL_INVALIDATE) )
Journal event constants
const ( // IndefiniteWait is a sentinel value that can be passed to // sdjournal.Wait() to signal an indefinite wait for new journal // events. It is implemented as the maximum value for a time.Duration: // https://github.com/golang/go/blob/e4dcf5c8c22d98ac9eac7b9b226596229624cb1d/src/time/time.go#L434 IndefiniteWait time.Duration = 1<<63 - 1 )
Variables ¶
var (
ErrExpired = errors.New("Timeout expired")
)
Functions ¶
func GetCatalogForMessageID ¶
GetCatalogForMessageID works similar to GetCatalog(), but the entry is looked up by the specified message ID (no open journal context is necessary for this), and no field substitution is performed.
Types ¶
type Journal ¶
type Journal struct {
// contains filtered or unexported fields
}
Journal is a Go wrapper of an sd_journal structure.
func NewJournal ¶
NewJournal returns a new Journal instance pointing to the local journal
func NewJournalFromDir ¶
NewJournalFromDir returns a new Journal instance pointing to a journal residing in a given directory. The supplied path may be relative or absolute; if relative, it will be converted to an absolute path before being opened.
func (*Journal) AddConjunction ¶
AddConjunction inserts a logical AND in the match list.
func (*Journal) AddDisjunction ¶
AddDisjunction inserts a logical OR in the match list.
func (*Journal) FlushMatches ¶
func (j *Journal) FlushMatches()
FlushMatches flushes all matches, disjunctions and conjunctions.
func (*Journal) GetCatalog ¶
GetCatalog retrieves a message catalog entry for the current journal entry. This will look up an entry in the message catalog by using the "MESSAGE_ID=" field of the current journal entry. Before returning the entry all journal field names in the catalog entry text enclosed in "@" will be replaced by the respective field values of the current entry. If a field name referenced in the message catalog entry does not exist, in the current journal entry, the "@" will be removed, but the field name otherwise left untouched.
func (*Journal) GetCursor ¶
GetCursor returns a cursor string for the current journal entry. A cursor is a serialization of the current journal position formatted as text. The string only contains printable characters and can be passed around in text form. The cursor identifies a journal entry globally and in a stable way and may be used to later seek to it via SeekCursor.
func (*Journal) GetData ¶
GetData gets the data object associated with a specific field from the current journal entry.
func (*Journal) GetDataAll ¶
func (j *Journal) GetDataAll() (JournalEntry, error)
func (*Journal) GetDataValue ¶
GetDataValue gets the data object associated with a specific field from the current journal entry, returning only the value of the object.
func (*Journal) GetRealtimeUsec ¶
GetRealtimeUsec gets the realtime (wallclock) timestamp of the current journal entry.
func (*Journal) NextSkip ¶
NextSkip advances the read pointer by multiple entries at once, as specified by the skip parameter.
func (*Journal) PreviousSkip ¶
PreviousSkip sets back the read pointer by multiple entries at once, as specified by the skip parameter.
func (*Journal) SeekCursor ¶
SeekCursor seeks to the entry located at the specified cursor string. If no entry matching the specified cursor is found the call will seek to the next closest entry (in terms of time) instead. SeekCursor returns true if it was able to seek to the exact postion, or false, if it was able to only seek to the next closest position. It returns an error if the operation failed completely
func (*Journal) SeekHead ¶
SeekHead seeks to the beginning of the journal, i.e. the oldest available entry.
func (*Journal) SeekMonotonicUsec ¶
SeekMonotonicUsec seeks to the entry with the specified monotonic timestamp, i.e. CLOCK_MONOTONIC. Since monotonic time restarts on every reboot a boot ID needs to be specified as well.
func (*Journal) SeekRealtimeUsec ¶
SeekRealtimeUsec seeks to the entry with the specified realtime (wallclock) timestamp, i.e. CLOCK_REALTIME.
func (*Journal) SeekTail ¶
SeekTail may be used to seek to the end of the journal, i.e. the most recent available entry.
func (*Journal) SetDataThreshold ¶
SetDataThresold sets the data field size threshold for data returned by GetData. To retrieve the complete data fields this threshold should be turned off by setting it to 0, so that the library always returns the complete data objects.
func (*Journal) TestCursor ¶
TestCursor may be used to check whether the current position in the journal matches the specified cursor. This is useful since cursor strings do not uniquely identify an entry: the same entry might be referred to by multiple different cursor strings, and hence string comparing cursors is not possible. Use this call to verify after an invocation of SeekCursor whether the entry being sought to was actually found in the journal or the next closest entry was used instead.
type JournalEntry ¶
type JournalEntry map[string]interface{}
JournalEntry is an alias for map[string]interface{}
type JournalReader ¶
type JournalReader struct {
Journal *Journal
}
JournalReader is an io.ReadCloser which provides a simple interface for iterating through the systemd journal.
func NewJournalReader ¶
func NewJournalReader(config JournalReaderConfig) (*JournalReader, error)
NewJournalReader creates a new JournalReader with configuration options that are similar to the systemd journalctl tool's iteration and filtering features.
func (*JournalReader) Close ¶
func (r *JournalReader) Close() error
func (*JournalReader) Follow ¶
Follow synchronously follows the JournalReader, writing each new journal entry to writer. The follow will continue until a single time.Time is received on the until channel.
func (*JournalReader) FollowJournal ¶
func (r *JournalReader) FollowJournal(until <-chan int, writer chan<- JournalEntry) (err error)
FollowJournal synchronously follows the JournalReader, writing each new journal entry to writer. The follow will continue until any int is received on the until channel. All Journal entries are pushed to the writer channel.
func (*JournalReader) ReadEntry ¶
func (r *JournalReader) ReadEntry() (JournalEntry, error)
type JournalReaderConfig ¶
type JournalReaderConfig struct { // The Since and NumFromTail options are mutually exclusive and determine // where the reading begins within the journal. Since time.Duration // start relative to a Duration from now NumFromTail uint64 // start relative to the tail // Show only journal entries whose fields match the supplied values. If // the array is empty, entries will not be filtered. Matches []Match }
JournalReaderConfig represents options to drive the behavior of a JournalReader.