func getJournal()

in pkg/systemlogmonitor/logwatchers/journald/log_watcher.go [143:189]


func getJournal(cfg types.WatcherConfig, startTime time.Time) (*sdjournal.Journal, error) {
	var journal *sdjournal.Journal
	var err error
	if cfg.LogPath == "" {
		journal, err = sdjournal.NewJournal()
		if err != nil {
			return nil, fmt.Errorf("failed to create journal client from default log path: %v", err)
		}
		glog.Info("unspecified log path so using systemd default")
	} else {
		// If the path doesn't exist, NewJournalFromDir will
		// create it instead of returning error. So check the
		// path existence ourselves.
		if _, err = os.Stat(cfg.LogPath); err != nil {
			return nil, fmt.Errorf("failed to stat the log path %q: %v", cfg.LogPath, err)
		}
		// Get journal client from the log path.
		journal, err = sdjournal.NewJournalFromDir(cfg.LogPath)
		if err != nil {
			return nil, fmt.Errorf("failed to create journal client from path %q: %v", cfg.LogPath, err)
		}
	}
	// Seek journal client based on startTime.
	seekTime := startTime
	now := time.Now()
	if now.Before(seekTime) {
		seekTime = now
	}
	err = journal.SeekRealtimeUsec(timeToJournalTimestamp(seekTime))
	if err != nil {
		return nil, fmt.Errorf("failed to seek journal at %v (now %v): %v", seekTime, now, err)
	}
	// Empty source is not allowed and treated as an error.
	source := cfg.PluginConfig[configSourceKey]
	if source == "" {
		return nil, fmt.Errorf("failed to filter journal log, empty source is not allowed")
	}
	match := sdjournal.Match{
		Field: sdjournal.SD_JOURNAL_FIELD_SYSLOG_IDENTIFIER,
		Value: source,
	}
	err = journal.AddMatch(match.String())
	if err != nil {
		return nil, fmt.Errorf("failed to add log filter %#v: %v", match, err)
	}
	return journal, nil
}