github.com/release-engineering/exodus-rsync@v1.11.2/internal/log/journald.go (about) 1 package log 2 3 import ( 4 "fmt" 5 "strings" 6 "sync" 7 8 apexLog "github.com/apex/log" 9 "github.com/coreos/go-systemd/v22/journal" 10 ) 11 12 type journalHandler struct { 13 test bool 14 mutex sync.Mutex 15 Entries []string 16 } 17 18 func newJournalHandler() (apexLog.Handler, error) { 19 return &journalHandler{}, nil 20 } 21 22 func priority(e *apexLog.Entry) journal.Priority { 23 if e.Level >= apexLog.ErrorLevel { 24 return journal.PriErr 25 } 26 if e.Level == apexLog.WarnLevel { 27 return journal.PriWarning 28 } 29 if e.Level == apexLog.InfoLevel { 30 return journal.PriInfo 31 } 32 return journal.PriDebug 33 } 34 35 func journalFields(e *apexLog.Entry) map[string]string { 36 out := make(map[string]string) 37 38 for key := range e.Fields { 39 val := fmt.Sprint(e.Fields[key]) 40 key = strings.ToUpper(key) 41 out[key] = val 42 } 43 44 return out 45 } 46 47 func (h *journalHandler) HandleLog(e *apexLog.Entry) error { 48 msg := e.Message 49 pri := priority(e) 50 fields := journalFields(e) 51 52 if h.test { 53 h.mutex.Lock() 54 defer h.mutex.Unlock() 55 56 var sFields string 57 for k, v := range fields { 58 sFields += fmt.Sprintf("%s=%s", k, v) 59 } 60 h.Entries = append(h.Entries, msg+" "+sFields) 61 } 62 63 return journal.Send(msg, pri, fields) 64 }