Skip to content

Instantly share code, notes, and snippets.

@hauke96
Last active July 29, 2017 15:30
Show Gist options
  • Save hauke96/19a267d828518a746a9c68138c071331 to your computer and use it in GitHub Desktop.
Save hauke96/19a267d828518a746a9c68138c071331 to your computer and use it in GitHub Desktop.
Simple logger for golang
package logger
import (
"fmt"
"os"
"runtime"
"runtime/debug"
"strconv"
"strings"
)
var DebugMode = false
var TestMode = false
func Debug(message string) {
if !DebugMode || TestMode {
return
}
os.Stdout.WriteString(fmt.Sprintf("[debug] %s: %s\n", getCallerName()+"() at "+strconv.Itoa(getCallerLine()), message))
}
func Info(message string) {
if TestMode {
return
}
os.Stdout.WriteString(fmt.Sprintf("[info] %s: %s\n", getCallerName(), message))
}
func Error(message string) {
if TestMode {
return
}
os.Stderr.WriteString(fmt.Sprintf("[ERROR] %s: %s\n", getCallerName()+"() at "+strconv.Itoa(getCallerLine()), message))
}
func Fatal(message string) {
os.Stderr.WriteString(fmt.Sprintf("\n\n[FATAL] %s: %s\n\n\n", getCallerName()+"() at "+strconv.Itoa(getCallerLine()), message))
debug.PrintStack()
Plain("\n\nAhhh, *urg*, I'm sorry but there was a really bad error inside of me. Above the stack trace is a message marked with [FATAL], you'll find some information there.\n\nI hope my death ... eh ... crash is only an exception and will be fixed soon ... my power ... leaves me ... good bye ... x.x")
os.Exit(1)
}
func Plain(message string) {
if TestMode {
return
}
os.Stdout.WriteString(message + "\n")
}
func getCallerName() string {
pc, _, _, _ := runtime.Caller(2)
path := runtime.FuncForPC(pc).Name()
splittedPath := strings.Split(path, "/")
fileName := splittedPath[len(splittedPath)-1]
return fileName
}
func getCallerLine() int {
_, _, line, _ := runtime.Caller(2)
return line
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment