Skip to content

Instantly share code, notes, and snippets.

@cpliakas
Last active August 29, 2015 14:20
Show Gist options
  • Save cpliakas/4c74b51c7629bc02af79 to your computer and use it in GitHub Desktop.
Save cpliakas/4c74b51c7629bc02af79 to your computer and use it in GitHub Desktop.
Stoopid simple leveled logger fo Go
package main
import (
"io"
"io/ioutil"
"log"
"os"
)
const (
LOG_EMERG int = 0
LOG_ALERT int = 1
LOG_CRIT int = 2
LOG_ERROR int = 3
LOG_WARN int = 4
LOG_NOTICE int = 5
LOG_INFO int = 6
LOG_DEBUG int = 7
)
type Logger struct {
EmergLogger *log.Logger
AlertLogger *log.Logger
CritLogger *log.Logger
ErrorLogger *log.Logger
WarnLogger *log.Logger
NoticeLogger *log.Logger
InfoLogger *log.Logger
DebugLogger *log.Logger
}
func main() {
logger := NewLogger(LOG_INFO)
logger.Debug("will not display")
logger.Info("will display")
logger.Error("will display")
}
func NewLogger(level int) *Logger {
handler := make([]io.Writer, 8)
for k, _ := range handler {
if k <= level {
handler[k] = os.Stdout
} else {
handler[k] = ioutil.Discard
}
}
return &Logger{
EmergLogger: log.New(handler[0], "EMERG ", log.Ldate|log.Ltime),
AlertLogger: log.New(handler[1], "ALERT ", log.Ldate|log.Ltime),
CritLogger: log.New(handler[2], "CRIT ", log.Ldate|log.Ltime),
ErrorLogger: log.New(handler[3], "ERROR ", log.Ldate|log.Ltime),
WarnLogger: log.New(handler[4], "WARN ", log.Ldate|log.Ltime),
NoticeLogger: log.New(handler[5], "NOTICE ", log.Ldate|log.Ltime),
InfoLogger: log.New(handler[6], "INFO ", log.Ldate|log.Ltime),
DebugLogger: log.New(handler[7], "DEBUG ", log.Ldate|log.Ltime|log.Lshortfile),
}
}
func (l *Logger) Emerg(format string, v ...interface{}) {
l.EmergLogger.Printf(format, v...)
}
func (l *Logger) Alert(format string, v ...interface{}) {
l.AlertLogger.Printf(format, v...)
}
func (l *Logger) Crit(format string, v ...interface{}) {
l.CritLogger.Printf(format, v...)
}
func (l *Logger) Error(format string, v ...interface{}) {
l.ErrorLogger.Printf(format, v...)
}
func (l *Logger) Warn(format string, v ...interface{}) {
l.WarnLogger.Printf(format, v...)
}
func (l *Logger) Notice(format string, v ...interface{}) {
l.NoticeLogger.Printf(format, v...)
}
func (l *Logger) Info(format string, v ...interface{}) {
l.InfoLogger.Printf(format, v...)
}
func (l *Logger) Debug(format string, v ...interface{}) {
l.DebugLogger.Printf(format, v...)
}
func (l *Logger) Fatal(format string, v ...interface{}) {
l.EmergLogger.Fatalf(format, v...)
}
func (l *Logger) Panic(format string, v ...interface{}) {
l.EmergLogger.Panicf(format, v...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment