Skip to content

Commit fe847d6

Browse files
author
Aaron Schlesinger
committed
ref(log.go,glide.yaml): switching to use prettyprint
also, put log functionality into a struct. singleton funcs to come in a subsequent commit
1 parent 4ca7091 commit fe847d6

2 files changed

Lines changed: 54 additions & 22 deletions

File tree

glide.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,3 @@ import:
2020
repo: https://github.com/golang/protobuf
2121
- package: launchpad.net/gocheck
2222
repo: https://github.com/go-check/check
23-
- package: github.com/labstack/gommon
24-
repo: https://github.com/labstack/gommon/color
25-
subpackages:
26-
- color

log/log.go

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"io"
88
"os"
99

10-
"github.com/labstack/gommon/color"
10+
"github.com/deis/pkg/prettyprint"
1111
)
1212

1313
// Stdout is the logging destination for normal messages.
@@ -19,54 +19,90 @@ var Stderr io.Writer = os.Stderr
1919
// IsDebugging toggles whether or not to enable debug output and behavior.
2020
var IsDebugging = false
2121

22+
// Color is the representation of a color, to be used in Colorize
23+
type Color string
24+
25+
// String is the fmt.Stringer interface implementation
26+
func (c Color) String() string {
27+
return string(c)
28+
}
29+
30+
var (
31+
// Default resets the console color
32+
Default = Color(prettyprint.Colors["Default"])
33+
// Red sets the console color to red
34+
Red = Color(prettyprint.Colors["Red"])
35+
// Cyan sets the console color to cyan
36+
Cyan = Color(prettyprint.Colors["Cyan"])
37+
// Yellow sets the console color to yellow
38+
Yellow = Color(prettyprint.Colors["Yellow"])
39+
// Green sets the console color to green
40+
Green = Color(prettyprint.Colors["Green"])
41+
)
42+
43+
type Logger struct {
44+
stdout io.Writer
45+
stderr io.Writer
46+
}
47+
48+
func NewLogger(stdout, stderr io.Writer) *Logger {
49+
return &Logger{stdout: stdout, stderr: stderr}
50+
}
51+
52+
var defaultLogger = &Logger{stdout: os.Stdout, stderr: os.Stderr}
53+
2254
// Msg passes through the formatter, but otherwise prints exactly as-is.
2355
//
2456
// No prettification.
25-
func Msg(format string, v ...interface{}) {
26-
fmt.Fprintf(Stdout, appendNewLine(format), v...)
57+
func (l *Logger) Msg(format string, v ...interface{}) {
58+
fmt.Fprintf(l.stdout, appendNewLine(format), v...)
2759
}
2860

2961
// Die prints an error and then call os.Exit(1).
30-
func Die(format string, v ...interface{}) {
31-
Err(format, v...)
62+
func (l *Logger) Die(format string, v ...interface{}) {
63+
l.Err(format, v...)
3264
if IsDebugging {
3365
panic(fmt.Sprintf(format, v...))
3466
}
3567
os.Exit(1)
3668
}
3769

3870
// CleanExit prints a message and then exits with 0.
39-
func CleanExit(format string, v ...interface{}) {
40-
Info(format, v...)
71+
func (l *Logger) CleanExit(format string, v ...interface{}) {
72+
l.Info(format, v...)
4173
os.Exit(0)
4274
}
4375

4476
// 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] "))
77+
func (l *Logger) Err(format string, v ...interface{}) {
78+
fmt.Fprint(Stderr, addColor("[ERROR] ", Red))
4779
fmt.Fprintf(Stderr, appendNewLine(format), v...)
4880
}
4981

5082
// Info prints a green-tinted message.
51-
func Info(format string, v ...interface{}) {
52-
fmt.Fprint(Stderr, "---> ")
53-
fmt.Fprintf(Stderr, appendNewLine(format), v...)
83+
func (l *Logger) Info(format string, v ...interface{}) {
84+
fmt.Fprint(l.stderr, addColor("---> ", Green))
85+
fmt.Fprintf(l.stderr, appendNewLine(format), v...)
5486
}
5587

5688
// Debug prints a cyan-tinted message if IsDebugging is true.
57-
func Debug(msg string, v ...interface{}) {
89+
func (l *Logger) Debug(msg string, v ...interface{}) {
5890
if IsDebugging {
59-
fmt.Fprint(Stderr, color.Cyan("[DEBUG] "))
60-
Msg(msg, v...)
91+
fmt.Fprint(l.stderr, addColor("[DEBUG] ", Cyan))
92+
l.Msg(msg, v...)
6193
}
6294
}
6395

6496
// 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...)
97+
func (l *Logger) Warn(format string, v ...interface{}) {
98+
fmt.Fprint(l.stderr, addColor("[WARN] ", Yellow))
99+
l.Msg(format, v...)
68100
}
69101

70102
func appendNewLine(format string) string {
71103
return format + "\n"
72104
}
105+
106+
func addColor(str string, color Color) string {
107+
return prettyprint.Colorize(fmt.Sprintf("{{.%s}}%s{{.%s}}", color.String(), str, Default.String()))
108+
}

0 commit comments

Comments
 (0)