github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/fs/log/systemd_unix.go (about) 1 // Systemd interface for Unix variants only 2 3 //go:build unix 4 5 package log 6 7 import ( 8 "fmt" 9 "log" 10 "strconv" 11 "strings" 12 13 "github.com/coreos/go-systemd/v22/journal" 14 "github.com/rclone/rclone/fs" 15 ) 16 17 // Enables systemd logs if configured or if auto-detected 18 func startSystemdLog() bool { 19 flagsStr := "," + Opt.Format + "," 20 var flags int 21 if strings.Contains(flagsStr, ",longfile,") { 22 flags |= log.Llongfile 23 } 24 if strings.Contains(flagsStr, ",shortfile,") { 25 flags |= log.Lshortfile 26 } 27 log.SetFlags(flags) 28 // TODO: Use the native journal.Print approach rather than a custom implementation 29 fs.LogPrint = func(level fs.LogLevel, text string) { 30 text = fmt.Sprintf("<%s>%-6s: %s", systemdLogPrefix(level), level, text) 31 _ = log.Output(4, text) 32 } 33 return true 34 } 35 36 var logLevelToSystemdPrefix = []journal.Priority{ 37 fs.LogLevelEmergency: journal.PriEmerg, 38 fs.LogLevelAlert: journal.PriAlert, 39 fs.LogLevelCritical: journal.PriCrit, 40 fs.LogLevelError: journal.PriErr, 41 fs.LogLevelWarning: journal.PriWarning, 42 fs.LogLevelNotice: journal.PriNotice, 43 fs.LogLevelInfo: journal.PriInfo, 44 fs.LogLevelDebug: journal.PriDebug, 45 } 46 47 func systemdLogPrefix(l fs.LogLevel) string { 48 if l >= fs.LogLevel(len(logLevelToSystemdPrefix)) { 49 return "" 50 } 51 return strconv.Itoa(int(logLevelToSystemdPrefix[l])) 52 } 53 54 func isJournalStream() bool { 55 if usingJournald, _ := journal.StderrIsJournalStream(); usingJournald { 56 return true 57 } 58 59 return false 60 }