Created
October 23, 2018 15:26
-
-
Save archever/56cdfa98b514892bf3cfc61a820e04a8 to your computer and use it in GitHub Desktop.
golang colored log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// source code from https://github.com/xcltapestry/xclpkg/blob/master/clcolor/clcolor.go | |
package logger | |
import ( | |
"fmt" | |
"runtime" | |
) | |
const ( | |
TextBlack = iota + 30 | |
TextRed | |
TextGreen | |
TextYellow | |
TextBlue | |
TextMagenta | |
TextCyan | |
TextWhite | |
) | |
func Black(str string) string { | |
return textColor(TextBlack, str) | |
} | |
func Red(str string) string { | |
return textColor(TextRed, str) | |
} | |
func Green(str string) string { | |
return textColor(TextGreen, str) | |
} | |
func Yellow(str string) string { | |
return textColor(TextYellow, str) | |
} | |
func Blue(str string) string { | |
return textColor(TextBlue, str) | |
} | |
func Magenta(str string) string { | |
return textColor(TextMagenta, str) | |
} | |
func Cyan(str string) string { | |
return textColor(TextCyan, str) | |
} | |
func White(str string) string { | |
return textColor(TextWhite, str) | |
} | |
func textColor(color int, str string) string { | |
if IsWindows() { | |
return str | |
} | |
switch color { | |
case TextBlack: | |
return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", TextBlack, str) | |
case TextRed: | |
return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", TextRed, str) | |
case TextGreen: | |
return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", TextGreen, str) | |
case TextYellow: | |
return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", TextYellow, str) | |
case TextBlue: | |
return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", TextBlue, str) | |
case TextMagenta: | |
return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", TextMagenta, str) | |
case TextCyan: | |
return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", TextCyan, str) | |
case TextWhite: | |
return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", TextWhite, str) | |
default: | |
return str | |
} | |
} | |
func IsWindows() bool { | |
if runtime.GOOS == "windows" { | |
return true | |
} else { | |
return false | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package logger | |
import ( | |
"fmt" | |
"io" | |
"os" | |
"sync" | |
"time" | |
) | |
type LogLevel byte | |
const ( | |
DEBUG LogLevel = 1 | |
INFO LogLevel = 2 | |
WARN LogLevel = 3 | |
ERROR LogLevel = 4 | |
) | |
var logName = map[LogLevel]string{ | |
DEBUG: "D", | |
INFO: "I", | |
WARN: "W", | |
ERROR: "E", | |
} | |
var logColor = map[LogLevel]func(string) string{ | |
DEBUG: Blue, | |
INFO: Green, | |
WARN: Yellow, | |
ERROR: Red, | |
} | |
type LogFactory struct { | |
sync.Mutex | |
level LogLevel | |
out io.Writer | |
buf []byte | |
colored bool | |
} | |
type Logger struct { | |
base *LogFactory | |
name string | |
} | |
var logIns *LogFactory | |
func New(level LogLevel, colored bool) *LogFactory { | |
logIns = &LogFactory{ | |
level: level, | |
out: os.Stdout, | |
colored: colored, | |
} | |
return logIns | |
} | |
func GetLogger(name string) *Logger { | |
return logIns.GetLogger(name) | |
} | |
func (l *LogFactory) GetLogger(name string) *Logger { | |
if l == nil { | |
New(INFO, true) | |
} | |
return &Logger{ | |
base: logIns, | |
name: name, | |
} | |
} | |
func (l *Logger) output(level LogLevel, msg string) { | |
if l.base.level > level { | |
return | |
} | |
l.base.Lock() | |
defer l.base.Unlock() | |
now := time.Now() | |
prefix := fmt.Sprintf( | |
"[%s %v-%v-%v %v:%v:%v %s] ", | |
logName[level], now.Year(), int(now.Month()), now.Day(), now.Hour(), now.Minute(), now.Second(), l.name) | |
l.base.buf = l.base.buf[:0] | |
if l.base.colored { | |
l.base.buf = append(l.base.buf, logColor[level](prefix)...) | |
} else { | |
l.base.buf = append(l.base.buf, []byte(prefix)...) | |
} | |
l.base.buf = append(l.base.buf, []byte(msg)...) | |
l.base.buf = append(l.base.buf, '\n') | |
l.base.out.Write(l.base.buf) | |
} | |
func (l *Logger) Debug(format string, v ...interface{}) { | |
msg := fmt.Sprintf(format, v...) | |
l.output(DEBUG, msg) | |
} | |
func (l *Logger) Info(format string, v ...interface{}) { | |
msg := fmt.Sprintf(format, v...) | |
l.output(INFO, msg) | |
} | |
func (l *Logger) Warn(format string, v ...interface{}) { | |
msg := fmt.Sprintf(format, v...) | |
l.output(WARN, msg) | |
} | |
func (l *Logger) Error(format string, v ...interface{}) { | |
msg := fmt.Sprintf(format, v...) | |
l.output(ERROR, msg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment