github.com/divyam234/rclone@v1.64.1/fs/log/systemd_unix.go (about)

     1  // Systemd interface for Unix variants only
     2  
     3  //go:build !windows && !nacl && !plan9
     4  // +build !windows,!nacl,!plan9
     5  
     6  package log
     7  
     8  import (
     9  	"fmt"
    10  	"log"
    11  	"strings"
    12  
    13  	sysdjournald "github.com/iguanesolutions/go-systemd/v5/journald"
    14  	"github.com/divyam234/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  	fs.LogPrint = func(level fs.LogLevel, text string) {
    29  		text = fmt.Sprintf("%s%-6s: %s", systemdLogPrefix(level), level, text)
    30  		_ = log.Output(4, text)
    31  	}
    32  	return true
    33  }
    34  
    35  var logLevelToSystemdPrefix = []string{
    36  	fs.LogLevelEmergency: sysdjournald.EmergPrefix,
    37  	fs.LogLevelAlert:     sysdjournald.AlertPrefix,
    38  	fs.LogLevelCritical:  sysdjournald.CritPrefix,
    39  	fs.LogLevelError:     sysdjournald.ErrPrefix,
    40  	fs.LogLevelWarning:   sysdjournald.WarningPrefix,
    41  	fs.LogLevelNotice:    sysdjournald.NoticePrefix,
    42  	fs.LogLevelInfo:      sysdjournald.InfoPrefix,
    43  	fs.LogLevelDebug:     sysdjournald.DebugPrefix,
    44  }
    45  
    46  func systemdLogPrefix(l fs.LogLevel) string {
    47  	if l >= fs.LogLevel(len(logLevelToSystemdPrefix)) {
    48  		return ""
    49  	}
    50  	return logLevelToSystemdPrefix[l]
    51  }