pax_global_header00006660000000000000000000000064145661276540014532gustar00rootroot0000000000000052 comment=5a8b2d6cdae13ccb9404eb870de8cd1d4bf3208c golang-github-schollz-logger-1.2.0/000077500000000000000000000000001456612765400172125ustar00rootroot00000000000000golang-github-schollz-logger-1.2.0/.gitignore000066400000000000000000000003001456612765400211730ustar00rootroot00000000000000# Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, build with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out golang-github-schollz-logger-1.2.0/.travis.yml000066400000000000000000000000321456612765400213160ustar00rootroot00000000000000language: go go: - tip golang-github-schollz-logger-1.2.0/LICENSE000066400000000000000000000020451456612765400202200ustar00rootroot00000000000000MIT License Copyright (c) 2019 Zack Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-schollz-logger-1.2.0/README.md000066400000000000000000000026611456612765400204760ustar00rootroot00000000000000# logger Code coverage Build Status Go Doc Simplistic, opinionated logging for Golang - Zero dependencies - Global logger (with optional local logger) - Leveled - Useful defaults / i.e. zero-config - Simple API - Colors on Linux (Windows colors are horrible and unnessecary) - Set leveling via environmental variables `LOGGER=trace|debug|info|warn|error` ``` [trace] 20:04:57.670116 logger.go:125: trace shows granular timestamp and line info [debug] 20:04:57 logger.go:129: debug shows regular timestamp and line info [info] 2019/05/08 20:04:57 info shows timestamp [warn] 2019/05/08 20:04:57 warn shows timestamp [error] 2019/05/08 20:04:57 logger.go:141: error shows timestamp and line info ``` ## Install ``` go get github.com/schollz/logger ``` ## Usage ```golang package main import ( log "github.com/schollz/logger" ) func main() { log.SetLevel("debug") log.Debug("hello, world") } ``` ## Contributing Pull requests are welcome. Feel free to... - Revise documentation - Add new features - Fix bugs - Suggest improvements ## License MIT golang-github-schollz-logger-1.2.0/go.mod000066400000000000000000000000521456612765400203150ustar00rootroot00000000000000module github.com/schollz/logger go 1.12 golang-github-schollz-logger-1.2.0/go.sum000066400000000000000000000000001456612765400203330ustar00rootroot00000000000000golang-github-schollz-logger-1.2.0/logger.go000066400000000000000000000067311456612765400210270ustar00rootroot00000000000000package logger import ( "fmt" "io" "log" "os" "runtime" "strings" "time" ) var l *Logger type writer struct { io.Writer timeFormat string } func (w writer) Write(b []byte) (n int, err error) { return w.Writer.Write(append([]byte(time.Now().Format(w.timeFormat)), b...)) } const ( red = "\033[0;31;1m" yellow = "\033[0;33m" white = "\033[0;37m" cyan = "\033[0;36m" blue = "\033[0;34;1m" end = "\033[0m" ) func init() { l = New() } type Logger struct { T, D, I, W, E *log.Logger t, d, i, w, e bool } func New() (l *Logger) { l = &Logger{ T: log.New(os.Stdout, "[trace]\t", log.Ltime|log.Lmicroseconds|log.Lshortfile), D: log.New(os.Stdout, "[debug]\t", log.Ltime|log.Lshortfile), I: log.New(os.Stdout, "[info]\t", log.Ldate|log.Ltime), W: log.New(os.Stdout, "[warn]\t", log.Ldate|log.Ltime), E: log.New(os.Stdout, "[error]\t", log.Ldate|log.Ltime|log.Lshortfile), t: true, d: true, i: true, w: true, e: true, } if runtime.GOOS == "linux" { l.T.SetPrefix(blue + l.T.Prefix() + end) l.D.SetPrefix(cyan + l.D.Prefix() + end) l.I.SetPrefix(white + l.I.Prefix() + end) l.W.SetPrefix(yellow + l.W.Prefix() + end) l.E.SetPrefix(red + l.E.Prefix() + end) } if strings.TrimSpace(strings.ToLower(os.Getenv("LOGGER"))) != "" { l.SetLevel(strings.TrimSpace(strings.ToLower(os.Getenv("LOGGER")))) } return } func SetOutput(w io.Writer) { l.SetOutput(w) } func SetLevel(s string) { // LOGGER enviromental variable takes precedence if strings.TrimSpace(strings.ToLower(os.Getenv("LOGGER"))) != "" { return } l.SetLevel(s) } func (l *Logger) SetOutput(w io.Writer) { l.T.SetOutput(w) l.D.SetOutput(w) l.I.SetOutput(w) l.W.SetOutput(w) l.E.SetOutput(w) } func (l *Logger) SetLevel(s string) { l.t = true l.d = true l.i = true l.w = true l.e = true switch s { case "debug": l.t = false case "info": l.t = false l.d = false case "warn": l.t = false l.d = false l.i = false case "error": l.t = false l.d = false l.i = false l.w = false } } func GetLevel() (s string) { return l.GetLevel() } func (l *Logger) GetLevel() (s string) { if l.t { return "trace" } else if l.d { return "debug" } else if l.i { return "info" } else if l.w { return "warn" } return "error" } func Tracef(format string, v ...interface{}) { l.Tracef(format, v...) } func Debugf(format string, v ...interface{}) { l.Debugf(format, v...) } func Infof(format string, v ...interface{}) { l.Infof(format, v...) } func Warnf(format string, v ...interface{}) { l.Warnf(format, v...) } func Errorf(format string, v ...interface{}) { l.Errorf(format, v...) } func Trace(v ...interface{}) { l.Tracef(fmt.Sprint(v...)) } func Debug(v ...interface{}) { l.Debugf(fmt.Sprint(v...)) } func Info(v ...interface{}) { l.Infof(fmt.Sprint(v...)) } func Warn(v ...interface{}) { l.Warnf(fmt.Sprint(v...)) } func Error(v ...interface{}) { l.Errorf(fmt.Sprint(v...)) } func (l *Logger) Tracef(format string, v ...interface{}) { if l.t { l.T.Output(3, fmt.Sprintf(format, v...)) } } func (l *Logger) Debugf(format string, v ...interface{}) { if l.d { l.D.Output(3, fmt.Sprintf(format, v...)) } } func (l *Logger) Infof(format string, v ...interface{}) { if l.i { l.I.Output(3, fmt.Sprintf(format, v...)) } } func (l *Logger) Warnf(format string, v ...interface{}) { if l.w { l.W.Output(3, fmt.Sprintf(format, v...)) } } func (l *Logger) Errorf(format string, v ...interface{}) { if l.e { l.E.Output(3, fmt.Sprintf(format, v...)) } } golang-github-schollz-logger-1.2.0/logger_test.go000066400000000000000000000021171456612765400220600ustar00rootroot00000000000000package logger import ( "bytes" "fmt" "strings" "testing" ) func TestTrace(t *testing.T) { var buf bytes.Buffer SetOutput(&buf) SetLevel("trace") funcs := []func(...interface{}){Trace, Debug, Info, Warn, Error} for i, s := range []string{"trace", "debug", "info", "warn", "error"} { funcs[i](s) fmt.Print(buf.String()) if !strings.Contains(buf.String(), s) { t.Error("bad " + s) } buf.Reset() } } func TestLogging(t *testing.T) { logger := New() var buf bytes.Buffer logger.SetOutput(&buf) levels := []string{"trace", "debug", "info", "warn", "error"} funcs := []func(string, ...interface{}){logger.Tracef, logger.Debugf, logger.Infof, logger.Warnf, logger.Errorf} for level, levelName := range levels { logger.SetLevel(levelName) for i, s := range levels { funcs[i](s) if i < level { if buf.String() != "" { t.Error("bad " + s) } } else { if !strings.Contains(buf.String(), s) { t.Error("bad " + s) } } buf.Reset() if logger.GetLevel() != levelName { t.Error("could not get level name: " + levelName) } } } }