Skip to content

Commit aaab8ec

Browse files
author
Aaron Schlesinger
committed
ref(log.go): add global funcs for logging
also add godoc
1 parent fe847d6 commit aaab8ec

1 file changed

Lines changed: 48 additions & 5 deletions

File tree

log/log.go

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,24 @@ var (
4040
Green = Color(prettyprint.Colors["Green"])
4141
)
4242

43+
// Logger is the base logging struct from which all logging functionality stems
4344
type Logger struct {
4445
stdout io.Writer
4546
stderr io.Writer
47+
debug bool
4648
}
4749

48-
func NewLogger(stdout, stderr io.Writer) *Logger {
49-
return &Logger{stdout: stdout, stderr: stderr}
50+
// NewLogger creates a new logger bound to a stdout and stderr writer, which are most commonly os.Stdout and os.Stderr, respectively
51+
func NewLogger(stdout, stderr io.Writer, debug bool) *Logger {
52+
return &Logger{stdout: stdout, stderr: stderr, debug: debug}
5053
}
5154

52-
var defaultLogger = &Logger{stdout: os.Stdout, stderr: os.Stderr}
55+
// DefaultLogger is the default logging implementation. It's used in all top level funcs inside the log package, and represents the equivalent of NewLogger(os.Stdout, os.Stderr)
56+
var DefaultLogger *Logger
57+
58+
func init() {
59+
DefaultLogger = &Logger{stdout: Stdout, stderr: Stderr, debug: IsDebugging}
60+
}
5361

5462
// Msg passes through the formatter, but otherwise prints exactly as-is.
5563
//
@@ -58,6 +66,11 @@ func (l *Logger) Msg(format string, v ...interface{}) {
5866
fmt.Fprintf(l.stdout, appendNewLine(format), v...)
5967
}
6068

69+
// Msg is a convenience function for DefaultLogger.Msg(...)
70+
func Msg(format string, v ...interface{}) {
71+
DefaultLogger.Msg(format, v)
72+
}
73+
6174
// Die prints an error and then call os.Exit(1).
6275
func (l *Logger) Die(format string, v ...interface{}) {
6376
l.Err(format, v...)
@@ -67,42 +80,72 @@ func (l *Logger) Die(format string, v ...interface{}) {
6780
os.Exit(1)
6881
}
6982

83+
// Die is a convenience function for DefaultLogger.Die(...)
84+
func Die(format string, v ...interface{}) {
85+
DefaultLogger.Die(format, v...)
86+
}
87+
7088
// CleanExit prints a message and then exits with 0.
7189
func (l *Logger) CleanExit(format string, v ...interface{}) {
7290
l.Info(format, v...)
7391
os.Exit(0)
7492
}
7593

94+
// CleanExit is a convenience function for DefaultLogger.CleanExit(...)
95+
func CleanExit(format string, v ...interface{}) {
96+
DefaultLogger.CleanExit(format, v...)
97+
}
98+
7699
// Err prints an error message. It does not cause an exit.
77100
func (l *Logger) Err(format string, v ...interface{}) {
78101
fmt.Fprint(Stderr, addColor("[ERROR] ", Red))
79102
fmt.Fprintf(Stderr, appendNewLine(format), v...)
80103
}
81104

105+
// Err is a convenience function for DefaultLogger.Err(...)
106+
func Err(format string, v ...interface{}) {
107+
DefaultLogger.Err(format, v...)
108+
}
109+
82110
// Info prints a green-tinted message.
83111
func (l *Logger) Info(format string, v ...interface{}) {
84112
fmt.Fprint(l.stderr, addColor("---> ", Green))
85113
fmt.Fprintf(l.stderr, appendNewLine(format), v...)
86114
}
87115

116+
// Info is a convenience function for DefaultLogger.Info(...)
117+
func Info(format string, v ...interface{}) {
118+
DefaultLogger.Info(format, v...)
119+
}
120+
88121
// Debug prints a cyan-tinted message if IsDebugging is true.
89122
func (l *Logger) Debug(msg string, v ...interface{}) {
90-
if IsDebugging {
123+
if l.debug {
91124
fmt.Fprint(l.stderr, addColor("[DEBUG] ", Cyan))
92125
l.Msg(msg, v...)
93126
}
94127
}
95128

129+
// Debug is a convenience function for DefaultLogger.Debug(...)
130+
func Debug(msg string, v ...interface{}) {
131+
DefaultLogger.Debug(msg, v...)
132+
}
133+
96134
// Warn prints a yellow-tinted warning message.
97135
func (l *Logger) Warn(format string, v ...interface{}) {
98136
fmt.Fprint(l.stderr, addColor("[WARN] ", Yellow))
99137
l.Msg(format, v...)
100138
}
101139

140+
// Warn is a convenience function for DefaultLogger.Warn(...)
141+
func Warn(format string, v ...interface{}) {
142+
DefaultLogger.Warn(format, v...)
143+
}
144+
102145
func appendNewLine(format string) string {
103146
return format + "\n"
104147
}
105148

106149
func addColor(str string, color Color) string {
107-
return prettyprint.Colorize(fmt.Sprintf("{{.%s}}%s{{.%s}}", color.String(), str, Default.String()))
150+
return prettyprint.Colorize(fmt.Sprintf("%s%s%s", color.String(), str, Default.String()))
108151
}

0 commit comments

Comments
 (0)