pax_global_header 0000666 0000000 0000000 00000000064 14566127654 0014532 g ustar 00root root 0000000 0000000 52 comment=5a8b2d6cdae13ccb9404eb870de8cd1d4bf3208c
golang-github-schollz-logger-1.2.0/ 0000775 0000000 0000000 00000000000 14566127654 0017212 5 ustar 00root root 0000000 0000000 golang-github-schollz-logger-1.2.0/.gitignore 0000664 0000000 0000000 00000000300 14566127654 0021173 0 ustar 00root root 0000000 0000000 # 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.yml 0000664 0000000 0000000 00000000032 14566127654 0021316 0 ustar 00root root 0000000 0000000 language: go
go:
- tip
golang-github-schollz-logger-1.2.0/LICENSE 0000664 0000000 0000000 00000002045 14566127654 0020220 0 ustar 00root root 0000000 0000000 MIT 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.md 0000664 0000000 0000000 00000002661 14566127654 0020476 0 ustar 00root root 0000000 0000000 # logger
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.mod 0000664 0000000 0000000 00000000052 14566127654 0020315 0 ustar 00root root 0000000 0000000 module github.com/schollz/logger
go 1.12
golang-github-schollz-logger-1.2.0/go.sum 0000664 0000000 0000000 00000000000 14566127654 0020333 0 ustar 00root root 0000000 0000000 golang-github-schollz-logger-1.2.0/logger.go 0000664 0000000 0000000 00000006731 14566127654 0021027 0 ustar 00root root 0000000 0000000 package 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.go 0000664 0000000 0000000 00000002117 14566127654 0022060 0 ustar 00root root 0000000 0000000 package 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)
}
}
}
}