Add "echo" as logger
This allow to test ouput, and specify ouput other then os.Stdout
This commit is contained in:
parent
23e4547e52
commit
5fd9bc851b
6 changed files with 315 additions and 117 deletions
72
echo/echo.go
Normal file
72
echo/echo.go
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
package echo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
colorReset = "\033[0m"
|
||||
colorRed = "\033[31m"
|
||||
colorGreen = "\033[32m"
|
||||
colorYellow = "\033[33m"
|
||||
colorBlue = "\033[34m"
|
||||
)
|
||||
|
||||
var (
|
||||
useColor bool = false
|
||||
output io.Writer = os.Stdout
|
||||
)
|
||||
|
||||
func Color(enabled bool) {
|
||||
useColor = enabled
|
||||
}
|
||||
|
||||
func Output(writer io.Writer) {
|
||||
output = writer
|
||||
}
|
||||
|
||||
func ErrorfMsg(format string, a ...interface{}) error {
|
||||
msg := fmt.Sprintf(format, a...)
|
||||
if useColor {
|
||||
msg = fmt.Sprintf("%vError:%v %v", colorRed, colorReset, msg)
|
||||
} else {
|
||||
msg = fmt.Sprintf("Error: %v", msg)
|
||||
}
|
||||
return write(msg)
|
||||
}
|
||||
|
||||
func InfoFMsg(format string, a ...interface{}) error {
|
||||
msg := fmt.Sprintf(format, a...)
|
||||
if useColor {
|
||||
msg = fmt.Sprintf("%vInfo:%v %v", colorBlue, colorReset, msg)
|
||||
} else {
|
||||
msg = fmt.Sprintf("Info: %v", msg)
|
||||
}
|
||||
return write(msg)
|
||||
}
|
||||
|
||||
func GreenMessageF(format string, a ...interface{}) error {
|
||||
return writeWithColor(fmt.Sprintf(format, a...), colorGreen)
|
||||
}
|
||||
|
||||
func YellowMessageF(format string, a ...interface{}) error {
|
||||
return writeWithColor(fmt.Sprintf(format, a...), colorYellow)
|
||||
}
|
||||
func RedMessageF(format string, a ...interface{}) error {
|
||||
return writeWithColor(fmt.Sprintf(format, a...), colorRed)
|
||||
}
|
||||
|
||||
func writeWithColor(msg string, color string) error {
|
||||
if useColor {
|
||||
return write(fmt.Sprintf("%v%v%v", color, msg, colorReset))
|
||||
}
|
||||
return write(msg)
|
||||
|
||||
}
|
||||
|
||||
func write(msg string) error {
|
||||
_, err := fmt.Fprintln(output, msg)
|
||||
return err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue