├── Readme.md ├── examples └── simple.go └── log.go /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # go-cli-log 3 | 4 | Colored / keyed CLI logging for Go command-line programs. 5 | 6 | ``` 7 | install : package express@3.2.1 8 | fetch : tarball for express 9 | unpack : tarball to node_modules/express 10 | unpack : express/Readme.md 11 | unpack : express/lib/application.js 12 | unpack : express/lib/request.js 13 | unpack : express/lib/response.js 14 | warn : duplicate express@3.x package found 15 | error : something exploded 16 | ``` 17 | 18 | View the [docs](https://godoc.org/github.com/visionmedia/go-cli-log). 19 | 20 | ## Example 21 | 22 | ```go 23 | package main 24 | 25 | import "github.com/visionmedia/go-cli-log" 26 | import "errors" 27 | import "fmt" 28 | 29 | func main() { 30 | fmt.Println() 31 | log.Verbose = true 32 | log.Info("install", "package %s@%s", "express", "3.2.1") 33 | log.Debug("fetch", "tarball for express") 34 | log.Info("unpack", "tarball to node_modules/express") 35 | log.Debug("unpack", "express/Readme.md") 36 | log.Debug("unpack", "express/lib/application.js") 37 | log.Debug("unpack", "express/lib/request.js") 38 | log.Debug("unpack", "express/lib/response.js") 39 | log.Warn("duplicate %s package found", "express@3.x") 40 | log.Error(errors.New("something exploded")) 41 | fmt.Println() 42 | } 43 | ``` 44 | 45 | # License 46 | 47 | MIT -------------------------------------------------------------------------------- /examples/simple.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/tj/go-cli-log" 4 | import "errors" 5 | import "fmt" 6 | 7 | func main() { 8 | fmt.Println() 9 | log.Verbose = true 10 | log.Info("install", "package %s@%s", "express", "3.2.1") 11 | log.Debug("fetch", "tarball for express") 12 | log.Info("unpack", "tarball to node_modules/express") 13 | log.Debug("unpack", "express/Readme.md") 14 | log.Debug("unpack", "express/lib/application.js") 15 | log.Debug("unpack", "express/lib/request.js") 16 | log.Debug("unpack", "express/lib/response.js") 17 | log.Warn("duplicate %s package found", "express@3.x") 18 | log.Error(errors.New("something exploded")) 19 | fmt.Println() 20 | } 21 | -------------------------------------------------------------------------------- /log.go: -------------------------------------------------------------------------------- 1 | package log 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // Verbose enables debug. 9 | var Verbose = false 10 | 11 | // Width of column. 12 | var Width = "14" 13 | 14 | // Info. 15 | func Info(key string, format string, args ...interface{}) { 16 | p := fmt.Sprintf("\033[36m%"+Width+"s \033[90m:\033[0m ", key) 17 | fmt.Printf(p+format+"\n", args...) 18 | } 19 | 20 | // Debug. 21 | func Debug(key string, format string, args ...interface{}) { 22 | if Verbose { 23 | p := fmt.Sprintf("\033[96m%"+Width+"s \033[90m:\033[0m ", key) 24 | fmt.Printf(p+format+"\n", args...) 25 | } 26 | } 27 | 28 | // Warning. 29 | func Warn(format string, args ...interface{}) { 30 | p := fmt.Sprintf("\033[33m%"+Width+"s \033[90m:\033[0m ", "warn") 31 | fmt.Printf(p+format+"\n", args...) 32 | } 33 | 34 | // Error. 35 | func Error(err error) { 36 | p := fmt.Sprintf("\033[31m%"+Width+"s \033[90m:\033[0m ", "error") 37 | fmt.Fprintf(os.Stderr, p+"%s\n", err.Error()) 38 | } 39 | --------------------------------------------------------------------------------