-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Custom implementation of glog for user-friendly messages, with colors #1301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
fabianofranz
wants to merge
1
commit into
openshift:master
from
fabianofranz:custom_glog_for_end_users
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| // A logger implementation with signature compatible with "github.com/golang/glog", but | ||
| // specifically targeted for pretty-printing for end users. Falls back to standard glog. | ||
| // Loosely coupled to glog, some features are not implemented and some new ones were added. | ||
| // Import as | ||
| // glog "github.com/openshift/origin/pkg/cmd/util/log" | ||
| package log | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/golang/glog" | ||
| "github.com/openshift/origin/pkg/cmd/util" | ||
| ) | ||
|
|
||
| const FATAL_EXIT_CODE = 255 | ||
|
|
||
| // Info logs to the INFO log. | ||
| // Arguments are handled in the manner of fmt.Print; a newline is appended if missing. | ||
| func Info(args ...interface{}) { | ||
| log(infoln, glog.Info, args...) | ||
| } | ||
|
|
||
| // Infoln logs to the INFO log. | ||
| // Arguments are handled in the manner of fmt.Println; a newline is appended if missing. | ||
| func Infoln(args ...interface{}) { | ||
| log(infoln, glog.Infoln, args...) | ||
| } | ||
|
|
||
| // Infof logs to the INFO log. | ||
| // Arguments are handled in the manner of fmt.Printf; a newline is appended if missing. | ||
| func Infof(format string, args ...interface{}) { | ||
| logf(infof, glog.Infof, format, args...) | ||
| } | ||
|
|
||
| // Success logs to the INFO log. | ||
| // Arguments are handled in the manner of fmt.Print; a newline is appended if missing. | ||
| func Success(args ...interface{}) { | ||
| log(successln, glog.Info, args...) | ||
| } | ||
|
|
||
| // Successln logs to the INFO log. | ||
| // Arguments are handled in the manner of fmt.Println; a newline is appended if missing. | ||
| func Successln(args ...interface{}) { | ||
| log(successln, glog.Infoln, args...) | ||
| } | ||
|
|
||
| // Successf logs to the INFO log. | ||
| // Arguments are handled in the manner of fmt.Printf; a newline is appended if missing. | ||
| func Successf(format string, args ...interface{}) { | ||
| logf(successf, glog.Infof, format, args...) | ||
| } | ||
|
|
||
| // Warning logs to the WARNING and INFO logs. | ||
| // Arguments are handled in the manner of fmt.Print; a newline is appended if missing. | ||
| func Warning(args ...interface{}) { | ||
| log(warnln, glog.Warning, args...) | ||
| } | ||
|
|
||
| // Warningln logs to the WARNING and INFO logs. | ||
| // Arguments are handled in the manner of fmt.Println; a newline is appended if missing. | ||
| func Warningln(args ...interface{}) { | ||
| log(warnln, glog.Warningln, args...) | ||
| } | ||
|
|
||
| // Warningf logs to the WARNING and INFO logs. | ||
| // Arguments are handled in the manner of fmt.Printf; a newline is appended if missing. | ||
| func Warningf(format string, args ...interface{}) { | ||
| logf(warnf, glog.Warningf, format, args...) | ||
| } | ||
|
|
||
| // Error logs to the ERROR, WARNING, and INFO logs. | ||
| // Arguments are handled in the manner of fmt.Print; a newline is appended if missing. | ||
| func Error(args ...interface{}) { | ||
| log(errorln, glog.Error, args...) | ||
| } | ||
|
|
||
| // Errorln logs to the ERROR, WARNING, and INFO logs. | ||
| // Arguments are handled in the manner of fmt.Println; a newline is appended if missing. | ||
| func Errorln(args ...interface{}) { | ||
| log(errorln, glog.Errorln, args...) | ||
| } | ||
|
|
||
| // Errorf logs to the ERROR, WARNING, and INFO logs. | ||
| // Arguments are handled in the manner of fmt.Printf; a newline is appended if missing. | ||
| func Errorf(format string, args ...interface{}) { | ||
| logf(errorf, glog.Errorf, format, args...) | ||
| } | ||
|
|
||
| // Fatal logs to the FATAL, ERROR, WARNING, and INFO logs, | ||
| // including a stack trace of all running goroutines, then calls os.Exit(255). | ||
| // Arguments are handled in the manner of fmt.Print; a newline is appended if missing. | ||
| func Fatal(args ...interface{}) { | ||
| log(fatalln, glog.Fatal, args...) | ||
| } | ||
|
|
||
| // Fatalln logs to the FATAL, ERROR, WARNING, and INFO logs, | ||
| // including a stack trace of all running goroutines, then calls os.Exit(255). | ||
| // Arguments are handled in the manner of fmt.Println; a newline is appended if missing. | ||
| func Fatalln(args ...interface{}) { | ||
| log(fatalln, glog.Fatalln, args...) | ||
| } | ||
|
|
||
| // Fatalf logs to the FATAL, ERROR, WARNING, and INFO logs, | ||
| // including a stack trace of all running goroutines, then calls os.Exit(255). | ||
| // Arguments are handled in the manner of fmt.Printf; a newline is appended if missing. | ||
| func Fatalf(format string, args ...interface{}) { | ||
| logf(fatalf, glog.Fatalf, format, args...) | ||
| } | ||
|
|
||
| // FatalDepth acts as Fatal but uses depth to determine which call frame to log. | ||
| // FatalDepth(0, "msg") is the same as Fatal("msg"). | ||
| func FatalDepth(depth int, args ...interface{}) { | ||
| log(fatalln, func(args ...interface{}) { | ||
| glog.FatalDepth(depth, args...) | ||
| }, args...) | ||
| } | ||
|
|
||
| // logging with level specified is always raw | ||
| func V(level glog.Level) glog.Verbose { | ||
| return glog.V(level) | ||
| } | ||
|
|
||
| func log(onTerminal func(args ...interface{}), fallback func(args ...interface{}), args ...interface{}) { | ||
| out := os.Stdout | ||
| if util.IsTerminal(out) { | ||
| onTerminal(args...) | ||
| } else { | ||
| fallback(args...) | ||
| } | ||
| } | ||
|
|
||
| func logf(onTerminal func(format string, args ...interface{}), fallback func(format string, args ...interface{}), format string, args ...interface{}) { | ||
| out := os.Stdout | ||
| if util.IsTerminal(out) { | ||
| onTerminal(format, args...) | ||
| } else { | ||
| fallback(format, args...) | ||
| } | ||
| } | ||
|
|
||
| func infoln(args ...interface{}) { | ||
| s := fmt.Sprint(args...) | ||
| fmt.Println(ANSI_DEFAULT + s + ANSI_RESET) | ||
| } | ||
|
|
||
| func infof(format string, args ...interface{}) { | ||
| s := fmt.Sprintf(format, args...) | ||
| fmt.Println(ANSI_DEFAULT + s + ANSI_RESET) | ||
| } | ||
|
|
||
| func successln(args ...interface{}) { | ||
| s := fmt.Sprint(args...) | ||
| fmt.Println(ANSI_GREEN + s + ANSI_RESET) | ||
| } | ||
|
|
||
| func successf(format string, args ...interface{}) { | ||
| s := fmt.Sprintf(format, args...) | ||
| fmt.Println(ANSI_GREEN + s + ANSI_RESET) | ||
| } | ||
|
|
||
| func warnln(args ...interface{}) { | ||
| s := fmt.Sprint(args...) | ||
| fmt.Println(ANSI_YELLOW + s + ANSI_RESET) | ||
| } | ||
|
|
||
| func warnf(format string, args ...interface{}) { | ||
| s := fmt.Sprintf(format, args...) | ||
| fmt.Println(ANSI_YELLOW + s + ANSI_RESET) | ||
| } | ||
|
|
||
| func errorln(args ...interface{}) { | ||
| s := fmt.Sprint(args...) | ||
| fmt.Println(ANSI_RED + s + ANSI_RESET) | ||
| } | ||
|
|
||
| func errorf(format string, args ...interface{}) { | ||
| s := fmt.Sprintf(format, args...) | ||
| fmt.Println(ANSI_RED + s + ANSI_RESET) | ||
| } | ||
|
|
||
| func fatalln(args ...interface{}) { | ||
| errorln(args...) | ||
| os.Exit(FATAL_EXIT_CODE) | ||
| } | ||
|
|
||
| func fatalf(format string, args ...interface{}) { | ||
| errorf(format, args...) | ||
| os.Exit(FATAL_EXIT_CODE) | ||
| } | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // +build linux,cgo darwin,cgo | ||
|
|
||
| package log | ||
|
|
||
| const ( | ||
| ANSI_DEFAULT = "\033[39m" | ||
| ANSI_RED = "\033[31m" | ||
| ANSI_GREEN = "\033[32m" | ||
| ANSI_YELLOW = "\033[33m" | ||
| ANSI_BLUE = "\033[34m" | ||
| ANSI_RESET = "\033[0m" | ||
| ) |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // +build !linux,!darwin !cgo | ||
|
|
||
| package log | ||
|
|
||
| const ( | ||
| ANSI_DEFAULT = "" | ||
| ANSI_RED = "" | ||
| ANSI_GREEN = "" | ||
| ANSI_YELLOW = "" | ||
| ANSI_BLUE = "" | ||
| ANSI_RESET = "" | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessarily, but it keeps the signature of everything in this pkg closer to what's in
github.com/golang/glog, making it simpler to replace one with another.On the other hand if we decide that's not an issue, a few funcs here could be removed.