Skip to content
Snippets Groups Projects
Commit 195f5f7b authored by Manuel Kieweg's avatar Manuel Kieweg
Browse files

Commented exported functions

parent c285ebf9
No related branches found
No related tags found
2 merge requests!41Resolve "Enhanced Logging",!18Develop
......@@ -22,7 +22,7 @@ type Logger struct {
func get() *Logger {
once.Do(func() {
logger = &Logger{
Out: os.Stdout,
Out: os.Stderr,
Loglevel: INFO,
lock: sync.Mutex{},
}
......@@ -30,6 +30,8 @@ func get() *Logger {
return logger
}
//Loglevel sets the verbosity of the logger
//Defaults to INFO
func Loglevel(level Level) {
l := get()
l.lock.Lock()
......@@ -37,6 +39,8 @@ func Loglevel(level Level) {
l.Loglevel = level
}
//Output defines the output of the logger
//Defaults to os.Stderr
func Output(out io.Writer) {
l := get()
l.lock.Lock()
......@@ -44,38 +48,62 @@ func Output(out io.Writer) {
l.Out = out
}
//Debug passes the DEBUG flag and a
//message to the logger
func Debug(args ...interface{}) {
log(DEBUG, args...)
}
//Debug passes the DEBUG flag and a
//message to the logger
func Info(args ...interface{}) {
log(INFO, args...)
}
//Warn passes the WARNING flag and a
//message to the logger
func Warn(args ...interface{}) {
log(WARNING, args...)
}
//Error passes the ERROR flag and a
//message to the logger
func Error(args ...interface{}) {
log(ERROR, args...)
}
//Fatal passes the FATAL flag and a
//message to the logger and calls
//os.Exit(1)
func Fatal(args ...interface{}) {
log(FATAL, args...)
os.Exit(1)
}
//Panic passes the PANIC flag and a
//message to the logger
//Also calls builtin.panic()
func Panic(args ...interface{}) {
log(PANIC, args...)
panic(args)
}
func log(level Level, args ...interface{}) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
l := get()
l.lock.Lock()
defer l.lock.Unlock()
if level <= l.Loglevel {
msg := fmt.Sprint(args...)
logMessage := time.Now().Format(time.RFC3339) + "\t" + prefix(level) + "\t" + msg + "\n"
l.Out.Write([]byte(logMessage))
_,err := l.Out.Write([]byte(logMessage))
if err != nil {
panic(err)
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment