Archived
1
0
Fork 0
This repository has been archived on 2024-12-18. You can view files and clone it, but cannot push or open issues or pull requests.
patroniglue/src/log.go
2019-03-08 18:22:16 +01:00

74 lines
1.3 KiB
Go

package main
import (
"fmt"
"log"
)
// LogLevel is the minimum level of log messages to print
type LogLevel int
// Auto set log level
const (
DEBUG LogLevel = iota
INFO
WARNING
ERROR
FATAL
)
var level = INFO
// SetLogLevel sets minimum log level to print
func SetLogLevel(logLevel string) error {
switch logLevel {
case "FATAL":
level = FATAL
case "ERROR":
level = ERROR
case "WARNING":
level = WARNING
case "INFO":
level = INFO
case "DEBUG":
level = DEBUG
default:
return fmt.Errorf("log level %s not allowed", logLevel)
}
return nil
}
// Debug prints very verbose messages
func Debug(message string, args ...interface{}) {
if level <= DEBUG {
log.Printf("DEBUG: "+message, args...)
}
}
// Info prints informative messages
func Info(message string, args ...interface{}) {
if level <= INFO {
log.Printf("INFO: "+message, args...)
}
}
// Warning prints messages you should give attention
func Warning(message string, args ...interface{}) {
if level <= WARNING {
log.Printf("WARNING: "+message, args...)
}
}
// Error prints impacting messages
func Error(message string, args ...interface{}) {
if level <= ERROR {
log.Printf("ERROR: "+message, args...)
}
}
// Fatal prints a message and exit program
func Fatal(message string, args ...interface{}) {
if level <= FATAL {
log.Fatalf("FATAL: "+message, args...)
}
}