Skip to content

Commit 7509c5f

Browse files
author
yusukato
committed
glg release
0 parents  commit 7509c5f

12 files changed

+1073
-0
lines changed

.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
2+
*.o
3+
*.a
4+
*.so
5+
6+
# Folders
7+
_obj
8+
_test
9+
10+
# Architecture specific extensions/prefixes
11+
*.[568vq]
12+
[568vq].out
13+
14+
*.cgo1.go
15+
*.cgo2.c
16+
_cgo_defun.c
17+
_cgo_gotypes.go
18+
_cgo_export.*
19+
20+
_testmain.go
21+
22+
*.exe
23+
*.test
24+
*.prof

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Yusuke Kato
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
GO_VERSION:=$(shell go version)
2+
3+
.PHONY: bench profile
4+
5+
all: install
6+
7+
bench:
8+
go test -count=5 -run=NONE -bench . -benchmem
9+
10+
profile:
11+
mkdir bench
12+
go test -count=10 -run=NONE -bench . -benchmem -o pprof/test.bin -cpuprofile pprof/cpu.out -memprofile pprof/mem.out
13+
go tool pprof --svg pprof/test.bin pprof/mem.out > mem.svg
14+
go tool pprof --svg pprof/test.bin pprof/cpu.out > cpu.svg
15+
rm -rf pprof
16+
go test -count=10 -run=NONE -bench=BenchmarkGlg -benchmem -o pprof/test.bin -cpuprofile pprof/cpu-glg.out -memprofile pprof/mem-glg.out
17+
go tool pprof --svg pprof/test.bin pprof/cpu-glg.out > cpu-glg.svg
18+
go tool pprof --svg pprof/test.bin pprof/mem-glg.out > mem-glg.svg
19+
rm -rf pprof
20+
go test -count=10 -run=NONE -bench=BenchmarkDefaultLog -benchmem -o pprof/test.bin -cpuprofile pprof/cpu-def.out -memprofile pprof/mem-def.out
21+
go tool pprof --svg pprof/test.bin pprof/mem-def.out > mem-def.svg
22+
go tool pprof --svg pprof/test.bin pprof/cpu-def.out > cpu-def.svg
23+
rm -rf pprof
24+
mv ./*.svg bench/

README.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# glg
2+
glg is simple golang logging library
3+
4+
## Requirement
5+
Go 1.8
6+
7+
## Installation
8+
```shell
9+
go get github.com/kpango/glg
10+
```
11+
12+
## Example
13+
```go
14+
infolog := glg.FileWriter("/tmp/info.log", 0666)
15+
defer infolog.Close()
16+
17+
customLevel := "FINE"
18+
customErrLevel := "CRIT"
19+
20+
glg.Get().
21+
SetMode(glg.BOTH). // default is STD
22+
// SetMode(glg.NONE). //nothing
23+
// SetMode(glg.WRITER). // io.Writer logging
24+
// SetMode(glg.BOTH). // stdout and file logging
25+
// InitWriter(). // initialize glg logger writer
26+
// AddWriter(customWriter). // add stdlog output destination
27+
// SetWriter(customWriter). // overwrite stdlog output destination
28+
// AddLevelWriter(glg.LOG, customWriter). // add LOG output destination
29+
// AddLevelWriter(glg.INFO, customWriter). // add INFO log output destination
30+
// AddLevelWriter(glg.WARN, customWriter). // add WARN log output destination
31+
// AddLevelWriter(glg.ERR, customWriter). // add ERR log output destination
32+
// SetLevelWriter(glg.LOG, customWriter). // overwrite LOG output destination
33+
// SetLevelWriter(glg.INFO, customWriter). // overwrite INFO log output destination
34+
// SetLevelWriter(glg.WARN, customWriter). // overwrite WARN log output destination
35+
// SetLevelWriter(glg.ERR, customWriter). // overwrite ERR log output destination
36+
AddLevelWriter(glg.INFO, infolog). // add info log file destination
37+
// AddLevelWriter(glg.INFO, glg.FileWriter("/tmp/info.log", 0666)). // add info log file destination
38+
// AddLevelWriter(glg.ERR, glg.FileWriter("/tmp/errors.log", 0666)). // add error log file destination
39+
AddStdLevel(customLevel). //user custom log level
40+
AddErrLevel(customErrLevel). // user custom error log level
41+
SetLevelColor(customErrLevel, glg.Red) // set color output to user custom level
42+
43+
glg.Info("info")
44+
glg.Infof("%s : %s", "info", "formatted")
45+
glg.Log("log")
46+
glg.Logf("%s : %s", "info", "formatted")
47+
glg.Debug("debug")
48+
glg.Debugf("%s : %s", "info", "formatted")
49+
glg.Warn("warn")
50+
glg.Warnf("%s : %s", "info", "formatted")
51+
glg.Error("error")
52+
glg.Errorf("%s : %s", "info", "formatted")
53+
glg.Success("ok")
54+
glg.Successf("%s : %s", "info", "formatted")
55+
glg.Fail("fail")
56+
glg.Failf("%s : %s", "info", "formatted")
57+
glg.Print("Print")
58+
glg.Println("Println")
59+
glg.Printf("%s : %s", "printf", "formatted")
60+
61+
glg.CustomLog(customLevel, "custom logging")
62+
glg.CustomLog(customErrLevel, "custom error logging")
63+
64+
// HTTP Handler Logger
65+
http.Handle("/glg", glg.HTTPLoggerFunc("glg sample", func(w http.ResponseWriter, r *http.Request) {
66+
67+
glg.Info("glg HTTP server logger sample")
68+
fmt.Fprint(w, "glg HTTP server logger sample")
69+
70+
}))
71+
72+
http.ListenAndServe(":8080", nil)
73+
74+
// fatal logging
75+
glg.Fatalln("fatal")
76+
```
77+
78+
![Sample Logs](https://github.com/kpango/glg/raw/master/images/sample.png)
79+
80+
## Benchmarks
81+
82+
![Bench](https://github.com/kpango/glg/raw/master/images/bench.png)
83+
84+
## Contribution
85+
1. Fork it ( https://github.com/kpango/glg/fork )
86+
2. Create your feature branch (git checkout -b my-new-feature)
87+
3. Commit your changes (git commit -am 'Add some feature')
88+
4. Push to the branch (git push origin my-new-feature)
89+
5. Create new Pull Request
90+
91+
## Author
92+
[kpango](https://github.com/kpango)
93+
94+
## LICENSE
95+
glg released under MIT license, refer [LICENSE](https://github.com/kpango/glg/blob/master/LICENSE) file.

circle.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# circle.yml
2+
machine:
3+
environment:
4+
GOROOT: ""
5+
GOPATH: "${HOME}/.go_project"
6+
PATH: "${GOPATH}/bin:${PATH}"
7+
BUILD_PATH: "${GOPATH}/src/github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"
8+
GO15VENDOREXPERIMENT: 1
9+
GODIST: "go1.8.3.linux-amd64.tar.gz"
10+
post:
11+
- mkdir -p downloads
12+
- test -e downloads/$GODIST || curl -o downloads/$GODIST https://storage.googleapis.com/golang/$GODIST
13+
- sudo rm -rf /usr/local/go
14+
- sudo tar -C /usr/local -xzf downloads/$GODIST
15+
dependencies:
16+
pre:
17+
- pwd
18+
- echo "${PROJECT_PATH}"
19+
- go get -u -v github.com/Masterminds/glide
20+
- glide init --non-interactive
21+
- glide install
22+
override:
23+
- mkdir -p ~/.go_project/src/github.com/${CIRCLE_PROJECT_USERNAME}
24+
- ln -s ${HOME}/${CIRCLE_PROJECT_REPONAME} ${BUILD_PATH}
25+
- cd $BUILD_PATH && go build -ldflags="-s -w" -v
26+
test:
27+
override:
28+
- cd $BUILD_PATH && ./coverage.sh
29+
post:
30+
- cd $BUILD_PATH && bash <(curl -s https://codecov.io/bash) -f coverage.out
31+
deployment:
32+
release:
33+
tag: /v(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*)){2}/
34+
commands:
35+
- go get github.com/mitchellh/gox
36+
- go get github.com/tcnksm/ghr
37+
- gox -osarch "linux/amd64 linux/arm darwin/amd64 windows/amd64" -output "dist/{{.Dir}}_{{.OS}}_{{.Arch}}"
38+
- ghr -t ${GITHUB_TOKEN} -u ${USERNAME} -r ${CIRCLE_PROJECT_REPONAME} --replace $(cat release_tag) dist/

coverage.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
echo "" > coverage.out
5+
6+
for d in $(go list ./... | grep -v vendor); do
7+
go test -v -race -coverprofile=profile.out -covermode=atomic $d
8+
if [ -f profile.out ]; then
9+
cat profile.out >> coverage.out
10+
rm profile.out
11+
fi
12+
done
13+

example/main.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/kpango/glg"
7+
)
8+
9+
// NetWorkLogger sample network logger
10+
type NetWorkLogger struct{}
11+
12+
func (n NetWorkLogger) Write(b []byte) (int, error) {
13+
// http.Post("localhost:8080/log", "", bytes.NewReader(b))
14+
http.Get("http://127.0.0.1:8080/log")
15+
// glg.Success("Requested")
16+
return 1, nil
17+
}
18+
19+
func main() {
20+
21+
// var errWriter io.Writer
22+
// var customWriter io.Writer
23+
infolog := glg.FileWriter("/tmp/info.log", 0666)
24+
25+
customLevel := "FINE"
26+
customErrLevel := "CRIT"
27+
28+
// errlog := glg.FileWriter("/tmp/error.log", 0666)
29+
defer infolog.Close()
30+
// defer errlog.Close()
31+
glg.Get().
32+
SetMode(glg.BOTH). // default is STD
33+
// DisableColor().
34+
// SetMode(glg.NONE).
35+
// SetMode(glg.WRITER).
36+
// SetMode(glg.BOTH).
37+
// InitWriter().
38+
// AddWriter(customWriter).
39+
// SetWriter(customWriter).
40+
// AddLevelWriter(glg.LOG, customWriter).
41+
// AddLevelWriter(glg.INFO, customWriter).
42+
// AddLevelWriter(glg.WARN, customWriter).
43+
// AddLevelWriter(glg.ERR, customWriter).
44+
// SetLevelWriter(glg.LOG, customWriter).
45+
// SetLevelWriter(glg.INFO, customWriter).
46+
// SetLevelWriter(glg.WARN, customWriter).
47+
// SetLevelWriter(glg.ERR, customWriter).
48+
AddLevelWriter(glg.INFO, infolog). // add info log file destination
49+
// AddLevelWriter(glg.ERR, errlog). // add error log file destination
50+
AddStdLevel(customLevel). //user custom log level
51+
AddErrLevel(customErrLevel). // user custom error log level
52+
SetLevelColor(customErrLevel, glg.Red) // set color output to user custom level
53+
54+
glg.Info("info")
55+
glg.Infof("%s : %s", "info", "formatted")
56+
glg.Log("log")
57+
glg.Logf("%s : %s", "info", "formatted")
58+
glg.Debug("debug")
59+
glg.Debugf("%s : %s", "info", "formatted")
60+
glg.Warn("warn")
61+
glg.Warnf("%s : %s", "info", "formatted")
62+
glg.Error("error")
63+
glg.Errorf("%s : %s", "info", "formatted")
64+
glg.Success("ok")
65+
glg.Successf("%s : %s", "info", "formatted")
66+
glg.Fail("fail")
67+
glg.Failf("%s : %s", "info", "formatted")
68+
glg.Print("Print")
69+
glg.Println("Println")
70+
glg.Printf("%s : %s", "printf", "formatted")
71+
glg.CustomLog(customLevel, "custom logging")
72+
glg.CustomLog(customErrLevel, "custom error logging")
73+
74+
// glg.Get().AddLevelWriter(glg.DEBG, NetWorkLogger{}) // add info log file destination
75+
76+
http.Handle("/glg", glg.HTTPLoggerFunc("glg sample", func(w http.ResponseWriter, r *http.Request) {
77+
glg.Info("glg HTTP server logger sample")
78+
}))
79+
80+
http.Handle("/log", glg.HTTPLoggerFunc("log", func(w http.ResponseWriter, r *http.Request) {
81+
glg.Info("received")
82+
}))
83+
84+
http.ListenAndServe(":8080", nil)
85+
86+
// fatal logging
87+
glg.Fatalln("fatal")
88+
}

0 commit comments

Comments
 (0)