Commit 1d330f4f authored by AlexStocks's avatar AlexStocks

Add: colorful and pretty log

parent cc62a113
......@@ -3,8 +3,8 @@
[![Build Status](https://travis-ci.org/dubbogo/gost.png?branch=master)](https://travis-ci.org/dubbogo/gost)
[![codecov](https://codecov.io/gh/dubbogo/gost/branch/master/graph/badge.svg)](https://codecov.io/gh/dubbogo/gost)
[![GoDoc](https://godoc.org/github.com/dubbogo/gost?status.svg)](https://godoc.org/github.com/dubbogo/gost)
[![Go Report Card](https://goreportcard.com/badge/github.com/dubbogo/gost)](https://goreportcard.com/report/github.com/dubbogo/gost)
![license](https://img.shields.io/badge/license-Apache--2.0-green.svg)
[![Go Report Card](https://goreportcard.com/badge/github.com/dubbogo/gost)](https://goreportcard.com/report/github.com/dubbogo/gost)
![license](https://img.shields.io/badge/license-Apache--2.0-green.svg)
A go sdk for [Apache Dubbo-go](github.com/apache/dubbo-go).
......@@ -14,7 +14,7 @@ A go sdk for [Apache Dubbo-go](github.com/apache/dubbo-go).
> bytes.Buffer pool
* SlicePool
> slice pool
> slice pool
## container
......@@ -24,24 +24,31 @@ A go sdk for [Apache Dubbo-go](github.com/apache/dubbo-go).
* set
> HashSet
## log
> output log with color and provides pretty format string
## math
* Decimal
## net
* GetLocalIP() (string, error)
* IsSameAddr(addr1, addr2 net.Addr) bool
* ListenOnTCPRandomPort(ip string) (*net.TCPListener, error)
* ListenOnUDPRandomPort(ip string) (*net.UDPConn, error)
## runtime
* GoSafely
> Using `go` in a safe way.
* GoUnterminated
> Run a goroutine in a safe way whose task is long live as the whole process life time.
## net
* GetLocalIP() (string, error)
* IsSameAddr(addr1, addr2 net.Addr) bool
* ListenOnTCPRandomPort(ip string) (*net.TCPListener, error)
* ListenOnUDPRandomPort(ip string) (*net.UDPConn, error)
## page
> Page for pagination. It contains the most common functions like offset, pagesize.
## runtime
* GoSafely
> Using `go` in a safe way.
* GoUnterminated
> Run a goroutine in a safe way whose task is long live as the whole process life time.
## runtime
......@@ -60,10 +67,4 @@ A go sdk for [Apache Dubbo-go](github.com/apache/dubbo-go).
> check a var is nil or not.
## time
Timer optimization through time-wheel.
## page
Page for pagination. It contains the most common functions like offset, pagesize.
> Timer optimization through time-wheel.
module github.com/dubbogo/gost
require (
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.4.0
github.com/davecgh/go-spew v1.1.1
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/k0kubun/pp v3.0.1+incompatible
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/mattn/go-isatty v0.0.12
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
)
go 1.11
go 1.13
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw=
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
// Copyright 2016 ~ 2018 AlexStocks(https://github.com/AlexStocks).
// All rights reserved. Use of this source code is
// governed by Apache License 2.0.
// package gxlog is based on log4go.
// color.go provides colorful terminal log output functions.
package gxlog
import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"
"github.com/mattn/go-isatty"
)
var (
// Normal colors
// NORMAL = []byte{'\033', '0', 'm'}
NORMAL = []byte{'\033', '0'}
NBlack = []byte{'\033', '[', '3', '0', 'm'}
NRed = []byte{'\033', '[', '3', '1', 'm'}
NGreen = []byte{'\033', '[', '3', '2', 'm'}
NYellow = []byte{'\033', '[', '3', '3', 'm'}
NBlue = []byte{'\033', '[', '3', '4', 'm'}
NMagenta = []byte{'\033', '[', '3', '5', 'm'}
NCyan = []byte{'\033', '[', '3', '6', 'm'}
NWhite = []byte{'\033', '[', '3', '7', 'm'}
// Bright colors
BBlack = []byte{'\033', '[', '3', '0', ';', '1', 'm'}
BRed = []byte{'\033', '[', '3', '1', ';', '1', 'm'}
BGreen = []byte{'\033', '[', '3', '2', ';', '1', 'm'}
BYellow = []byte{'\033', '[', '3', '3', ';', '1', 'm'}
BBlue = []byte{'\033', '[', '3', '4', ';', '1', 'm'}
BMagenta = []byte{'\033', '[', '3', '5', ';', '1', 'm'}
BCyan = []byte{'\033', '[', '3', '6', ';', '1', 'm'}
BWhite = []byte{'\033', '[', '3', '7', ';', '1', 'm'}
UnderlineTwinkleHighLight = []byte{'\033', '[', '1', ';', '6', ';', '4', '0', 'm'}
reset = []byte{'\033', '[', '0', 'm'}
)
func funcFileLine() string {
tm := time.Unix(time.Now().Unix(), 0)
funcName, file, line, _ := runtime.Caller(3)
return "[" + tm.Format("2006-01-02/15:04:05 ") +
runtime.FuncForPC(funcName).Name() +
": " + filepath.Base(file) +
": " + fmt.Sprintf("%d", line) +
"] "
}
func CPrintf(color []byte, format string, args ...interface{}) {
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Fprintf(os.Stdout, string(color)+funcFileLine()+fmt.Sprintf(format, args...)+string(reset))
} else {
fmt.Fprintf(os.Stdout, fmt.Sprintf(format, args...))
}
}
func CPrintfln(color []byte, format string, args ...interface{}) {
logStr := fmt.Sprintf(format, args...)
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Fprintf(os.Stdout, string(color)+funcFileLine()+"%s"+string(reset)+"\n", logStr)
} else {
fmt.Fprintf(os.Stdout, "%s\n", logStr)
}
}
func CEPrintf(color []byte, format string, args ...interface{}) {
logStr := fmt.Sprintf(format, args...)
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Fprintf(os.Stderr, string(color)+funcFileLine()+"%s"+string(reset), logStr)
} else {
fmt.Fprintf(os.Stderr, "%s", logStr)
}
}
func CEPrintfln(color []byte, format string, args ...interface{}) {
logStr := fmt.Sprintf(format, args...)
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Fprintf(os.Stderr, string(color)+funcFileLine()+"%s"+string(reset)+"\n", logStr)
} else {
fmt.Fprintf(os.Stderr, "%s\n", logStr)
}
}
func CDebug(format string, args ...interface{}) {
CPrintfln(NORMAL, format, args...)
}
func CInfo(format string, args ...interface{}) {
CPrintfln(NGreen, format, args...)
}
func CWarn(format string, args ...interface{}) {
CEPrintfln(BMagenta, format, args...)
}
func CError(format string, args ...interface{}) {
CEPrintfln(NRed, format, args...)
}
func CFatal(format string, args ...interface{}) {
CEPrintfln(BRed, format, args...)
}
/* log_test.go - test for log.go */
package gxlog
import (
"testing"
)
func TestColorLog(t *testing.T) {
CDebug("Debug")
CInfo("Info")
// CWarn("Warn")
CWarn("%s", "/test/group%3Dbjtelecom%26protocol%3Dpb%26role%3DSRT_Provider%26service%3Dshopping%26version%3D1.0.1")
CError("Error")
}
// Copyright 2016 ~ 2018 AlexStocks(https://github.com/AlexStocks).
// All rights reserved. Use of this source code is
// governed by Apache License 2.0.
// package gxlog is based on log4go.
// pretty.go provides pretty format string
package gxlog
import (
"github.com/davecgh/go-spew/spew"
"github.com/k0kubun/pp"
)
func PrettyString(i interface{}) string {
return spew.Sdump(i)
}
func ColorSprint(i interface{}) string {
return pp.Sprint(i)
}
func ColorSprintln(i interface{}) string {
return pp.Sprintln(i)
}
func ColorSprintf(fmt string, args ...interface{}) string {
return pp.Sprintf(fmt, args...)
}
func ColorPrint(i interface{}) {
pp.Print(i)
}
func ColorPrintln(i interface{}) {
pp.Println(i)
}
func ColorPrintf(fmt string, args ...interface{}) {
pp.Printf(fmt, args...)
}
/* log_test.go - test for log.go */
package gxlog
import (
"fmt"
"testing"
)
type info struct {
name string
age float32
m map[string]string
}
func TestPrettyString(t *testing.T) {
var i = info{name: "hello", age: 23.5, m: map[string]string{"h": "w", "hello": "world"}}
fmt.Println(PrettyString(i))
}
func TestColorPrint(t *testing.T) {
var i = info{name: "hello", age: 23.5, m: map[string]string{"h": "w", "hello": "world"}}
ColorPrintln(i)
}
func TestColorPrintf(t *testing.T) {
var i = info{name: "hello", age: 23.5, m: map[string]string{"h": "w", "hello": "world"}}
ColorPrintf("exapmle format:%s\n", i)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment