[U] If filename provided is empty it assumes no logging to file

master
Diego Martin 4 years ago
parent 176f3fb230
commit ca1d02efd3
  1. 62
      logging.go

@ -57,72 +57,105 @@ var (
) )
func (l *logStream) Printf(format string, a ...interface{}) { func (l *logStream) Printf(format string, a ...interface{}) {
// describe(a) // describe(a)
if LogLevel < l.logLevel { if LogLevel < l.logLevel {
return return
} }
// if no specific logger is configured for this logstrea, write to stdout... // if no specific logger is configured for this logstrea, write to stdout...
logMessage := fmt.Sprintf(format, a...) logMessage := fmt.Sprintf(format, a...)
// Caller(1) is one level up the call stack - 0 identifies the caller of // Caller(1) is one level up the call stack - 0 identifies the caller of
// Caller which is this function... // Caller which is this function...
callingStr := "[Unknown calling function]" callingStr := "[Unknown calling function]"
_, fn, lineNum, callerValid := runtime.Caller(1) _, fn, lineNum, callerValid := runtime.Caller(1)
if callerValid { if callerValid {
callingStr = fn + ":" + strconv.Itoa(lineNum) callingStr = fn + ":" + strconv.Itoa(lineNum)
} }
if l.s == nil { if l.s == nil {
fmt.Print(callingStr, " ", logMessage) fmt.Print(callingStr, " ", logMessage)
return return
} }
l.s.Print(callingStr, " ", logMessage) l.s.Print(callingStr, " ", logMessage)
// Printf(format, a...) // Printf(format, a...)
} }
func (l *logStream) init(m io.Writer, s string, ll int) { func (l *logStream) init(m io.Writer, s string, ll int) {
l.s = log.New(m, s, log.Ldate|log.Ltime) l.s = log.New(m, s, log.Ldate|log.Ltime)
l.logLevel = ll l.logLevel = ll
} }
// function to parse string representation of log level into integer - if there // function to parse string representation of log level into integer - if there
// is no match of the string, it will return 0 which maps to error only // is no match of the string, it will return 0 which maps to error only
// logging...perhaps this could be made more explicit // logging...perhaps this could be made more explicit
func determineLogLevel(lstr string) (returnVal int, err error) { func determineLogLevel(lstr string) (returnVal int, err error) {
lstrLowerCase := strings.ToLower(lstr)
var validLogLevel bool var validLogLevel bool
lstrLowerCase := strings.ToLower(lstr)
if returnVal, validLogLevel = logLevelStringMap[lstrLowerCase]; validLogLevel == false { if returnVal, validLogLevel = logLevelStringMap[lstrLowerCase]; validLogLevel == false {
err = errors.New("Invalid Log Level") err = errors.New("Invalid Log Level")
} }
return return
} }
// InitLogger initializes the logging system // InitLogger initializes the logging system
func InitLogger(logFile string, logLevelStr string, logToConsole bool) (err error) { func InitLogger(logFile string, logLevelStr string, logToConsole bool) (err error) {
var file *os.File
var writters []io.Writer
if LogLevel, err = determineLogLevel(logLevelStr); err != nil { if LogLevel, err = determineLogLevel(logLevelStr); err != nil {
return
}
var file *os.File log.Println("Unable to determine the log level:", logLevelStr, ":", err)
if file, err = os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666); err != nil {
return return
} }
var multi io.Writer if logFile != "" {
if file, err = os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666); err != nil {
log.Println("Failed to open log file:", logFile, ":", err)
return
if err != nil {
log.Println("Failed to open log file:", logFile, ":", err)
multi = io.MultiWriter(os.Stdout)
} else {
if logToConsole {
multi = io.MultiWriter(file, os.Stdout)
} else {
multi = io.MultiWriter(file)
} }
writters = append(writters, file)
}
if logToConsole {
writters = append(writters, os.Stdout)
} }
multi := io.MultiWriter(writters...)
Trace.init(multi, " TRACE: ", TRACE) Trace.init(multi, " TRACE: ", TRACE)
Debug.init(multi, " DEBUG: ", DEBUG) Debug.init(multi, " DEBUG: ", DEBUG)
Info.init(multi, " INFO: ", INFO) Info.init(multi, " INFO: ", INFO)
@ -130,4 +163,5 @@ func InitLogger(logFile string, logLevelStr string, logToConsole bool) (err erro
Error.init(multi, " ERROR: ", ERROR) Error.init(multi, " ERROR: ", ERROR)
return return
} }

Loading…
Cancel
Save