Skip to content

Commit

Permalink
fix: handle journald errors
Browse files Browse the repository at this point in the history
If logging to journald fails, fall back to stderr instead of dropping
the message.
  • Loading branch information
eriksw committed Aug 15, 2023
1 parent b3256f4 commit f418c02
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cmd/sendmail/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019, RetailNext, Inc.
// Copyright (c) 2023, RetailNext, Inc.
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
// All rights reserved.
Expand Down Expand Up @@ -26,11 +26,15 @@ func formatError(err error) string {
}

func errorWithMessage(err error, message string) {
var journalOk bool
if journal.Enabled() {
vars := map[string]string{}
journal.Send(formatError(err), journal.PriErr, vars)
journal.Send(message, journal.PriErr, vars)
} else {
err1 := journal.Send(formatError(err), journal.PriErr, vars)
err2 := journal.Send(message, journal.PriErr, vars)
journalOk = err1 == nil && err2 == nil
}
// Skip logging to stderr if logging to the journal worked.
if !journalOk {
bytes, _ := json.Marshal(map[string]string{"message": message})
fmt.Fprintln(os.Stderr, formatError(err), string(bytes))
}
Expand Down

0 comments on commit f418c02

Please sign in to comment.