Skip to content

Commit f51480c

Browse files
arschlesAaron Schlesinger
authored andcommitted
feat(log): add a log package
this package should be extracted into deis/pkg. PR forthcoming for that cc/ @technosophos
1 parent 5c88ae2 commit f51480c

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

pkg/log/log.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Package log is a convenience wrapper for logging messages of various levels (associated colors to come)
2+
// to the terminal. Much of this code has been shamelessly stolen from https://github.com/helm/helm/blob/master/log/log.go
3+
package log
4+
5+
import (
6+
"fmt"
7+
"io"
8+
"os"
9+
10+
"github.com/labstack/gommon/color"
11+
)
12+
13+
// Stdout is the logging destination for normal messages.
14+
var Stdout io.Writer = os.Stdout
15+
16+
// Stderr is the logging destination for error messages.
17+
var Stderr io.Writer = os.Stderr
18+
19+
// IsDebugging toggles whether or not to enable debug output and behavior.
20+
var IsDebugging = false
21+
22+
// Msg passes through the formatter, but otherwise prints exactly as-is.
23+
//
24+
// No prettification.
25+
func Msg(format string, v ...interface{}) {
26+
fmt.Fprintf(Stdout, appendNewLine(format), v...)
27+
}
28+
29+
// Die prints an error and then call os.Exit(1).
30+
func Die(format string, v ...interface{}) {
31+
Err(format, v...)
32+
if IsDebugging {
33+
panic(fmt.Sprintf(format, v...))
34+
}
35+
os.Exit(1)
36+
}
37+
38+
// CleanExit prints a message and then exits with 0.
39+
func CleanExit(format string, v ...interface{}) {
40+
Info(format, v...)
41+
os.Exit(0)
42+
}
43+
44+
// Err prints an error message. It does not cause an exit.
45+
func Err(format string, v ...interface{}) {
46+
fmt.Fprint(Stderr, color.Red("[ERROR] "))
47+
fmt.Fprintf(Stderr, appendNewLine(format), v...)
48+
}
49+
50+
// Info prints a green-tinted message.
51+
func Info(format string, v ...interface{}) {
52+
fmt.Fprint(Stderr, "---> ")
53+
fmt.Fprintf(Stderr, appendNewLine(format), v...)
54+
}
55+
56+
// Debug prints a cyan-tinted message if IsDebugging is true.
57+
func Debug(msg string, v ...interface{}) {
58+
if IsDebugging {
59+
fmt.Fprint(Stderr, color.Cyan("[DEBUG] "))
60+
Msg(msg, v...)
61+
}
62+
}
63+
64+
// Warn prints a yellow-tinted warning message.
65+
func Warn(format string, v ...interface{}) {
66+
fmt.Fprint(Stderr, color.Yellow("[WARN] "))
67+
Msg(format, v...)
68+
}
69+
70+
func appendNewLine(format string) string {
71+
return format + "\n"
72+
}

0 commit comments

Comments
 (0)