Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 800ac57

Browse files
committed
Initial commit
0 parents  commit 800ac57

File tree

6 files changed

+109
-0
lines changed

6 files changed

+109
-0
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# flog
2+
3+
`flog` is a minimal, formatted, pretty logging package for Go.
4+
5+
It's primarily for command line tools, and it's optimized for human readability.
6+
7+
[Uber's Zap](https://github.com/uber-go/zap) is recommended for most robust logging.
8+
9+
## Install
10+
11+
`go get go.coder.com/flog`
12+
13+
## Usage
14+
15+
```go
16+
flog.Log(flog.INFO, "hello %.3f", 1/3.0)
17+
flog.Log(flog.SUCCESS, "finished that")
18+
flog.Log(flog.ERROR, "oops")
19+
20+
log := flog.New().WithPrefix("user %v: ", 500)
21+
22+
log.Log(flog.ERROR, "didn't work out")
23+
```
24+
25+
produces
26+
27+
![example](docs/usage.png)
28+
29+

docs/usage.png

7.71 KB
Loading

go.mod

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module go.coder.com/flog
2+
3+
require (
4+
github.com/fatih/color v1.7.0
5+
github.com/mattn/go-colorable v0.0.9 // indirect
6+
github.com/mattn/go-isatty v0.0.4 // indirect
7+
)

go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
2+
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
3+
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
4+
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
5+
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=

log.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package flog
2+
3+
import (
4+
"fmt"
5+
"github.com/fatih/color"
6+
"io"
7+
"os"
8+
"time"
9+
)
10+
11+
type Level string
12+
13+
var (
14+
INFO = Level(color.HiBlueString("INFO"))
15+
SUCCESS = Level(color.HiGreenString("SUCCESS"))
16+
ERROR = Level(color.RedString("ERROR"))
17+
FATAL = Level(color.RedString("FATAL"))
18+
)
19+
20+
type Logger struct {
21+
W io.Writer
22+
Prefix string
23+
}
24+
25+
func (l *Logger) WithPrefix(p string, args ...interface{}) *Logger {
26+
ll := *l
27+
ll.Prefix += fmt.Sprintf(p, args...)
28+
return &ll
29+
}
30+
31+
func (l *Logger) Log(lvl Level, msg string, args ...interface{}) {
32+
fmt.Fprintf(
33+
l.W,
34+
fmt.Sprintf("%v %v\t", time.Now().Format(`2006-01-02 15:04:05`), lvl)+
35+
l.Prefix + msg+"\n", args...,
36+
)
37+
if lvl == FATAL {
38+
os.Exit(1)
39+
}
40+
}
41+
42+
// New returns a new logger.
43+
func New() *Logger {
44+
return &Logger{
45+
W: os.Stderr,
46+
}
47+
}
48+
49+
// Log logs a message with the default logger.
50+
func Log(l Level, m string, args ...interface{}) {
51+
New().Log(l, m, args...)
52+
}

log_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package flog_test
2+
3+
import (
4+
"go.coder.com/flog"
5+
"testing"
6+
)
7+
8+
func TestLogger(t *testing.T) {
9+
flog.Log(flog.INFO, "hello %.3f", 1/3.0)
10+
flog.Log(flog.SUCCESS, "finished that")
11+
flog.Log(flog.ERROR, "oops")
12+
13+
log := flog.New().WithPrefix("user %v: ", 500)
14+
15+
log.Log(flog.ERROR, "didn't work out")
16+
}

0 commit comments

Comments
 (0)