github.com/swiftstack/ProxyFS@v0.0.0-20210203235616-4017c267d62f/pfsagentd/log.go (about) 1 // Copyright (c) 2015-2021, NVIDIA CORPORATION. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package main 5 6 import ( 7 "fmt" 8 "log" 9 "os" 10 "time" 11 ) 12 13 func logFatal(err error) { 14 logf("FATAL", "%v", err) 15 os.Exit(1) 16 } 17 18 func logFatalf(format string, args ...interface{}) { 19 logf("FATAL", format, args...) 20 os.Exit(1) 21 } 22 23 func logErrorf(format string, args ...interface{}) { 24 logf("ERROR", format, args...) 25 } 26 27 func logWarnf(format string, args ...interface{}) { 28 logf("WARN", format, args...) 29 } 30 31 func logInfof(format string, args ...interface{}) { 32 logf("INFO", format, args...) 33 } 34 35 func logTracef(format string, args ...interface{}) { 36 if globals.config.TraceEnabled { 37 logf("TRACE", format, args...) 38 } 39 } 40 41 func logf(level string, format string, args ...interface{}) { 42 var ( 43 enhancedArgs []interface{} 44 enhancedFormat string 45 err error 46 logMsg string 47 ) 48 49 enhancedFormat = "[%s][%s][%d][%s] " + format 50 enhancedArgs = append([]interface{}{time.Now().Format(time.RFC3339Nano), globals.config.FUSEVolumeName, os.Getpid(), level}, args...) 51 52 logMsg = fmt.Sprintf(enhancedFormat, enhancedArgs[:]...) 53 54 if nil == globals.logFile { 55 if "" != globals.config.LogFilePath { 56 globals.logFile, err = os.OpenFile(globals.config.LogFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) 57 if nil == err { 58 globals.logFile.WriteString(logMsg + "\n") 59 } else { 60 globals.logFile = nil 61 } 62 } 63 } else { 64 globals.logFile.WriteString(logMsg + "\n") 65 } 66 if globals.config.LogToConsole { 67 fmt.Fprintln(os.Stderr, logMsg) 68 } 69 } 70 71 func newLogger() *log.Logger { 72 return log.New(&globals, "", 0) 73 } 74 75 func (dummy *globalsStruct) Write(p []byte) (n int, err error) { 76 logf("FISSION", "%s", string(p[:])) 77 return 0, nil 78 }