Skip to content

Commit

Permalink
用例测试里去掉了fatal,panic
Browse files Browse the repository at this point in the history
  • Loading branch information
yezihack committed Mar 8, 2019
1 parent 181ec79 commit be8fbe6
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 46 deletions.
4 changes: 2 additions & 2 deletions conf/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ const LogNormalType = "normal"
const (
Filename string = "./log/default.log" //日志保存路径 //需要设置程序当前运行路径
LogLevel Level = DebugLevel //日志记录级别
MaxSize int = 100 //日志分割的尺寸 MB
MaxAge int = 30 //分割日志保存的时间 day
MaxSize int = 100 //日志分割的尺寸 MB
MaxAge int = 30 //分割日志保存的时间 day
Stacktrace Level = PanicLevel //记录堆栈的级别
IsStdOut bool = true //是否标准输出console输出
ProjectName string = "ZeLog" //项目名称
Expand Down
2 changes: 2 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ var l Loger = zaplog.New()
func SetLogger(opts ...conf.Option) {
l = zaplog.New(opts...)
}

//快捷使用,开发使用
func NewDevelopment(projectName, filePath string) {
SetLogger(conf.WithProjectName(projectName),
conf.WithFilename(filePath),
conf.WithLogType(conf.LogJsontype),
conf.WithIsStdOut(true))
}

//快捷使用,生产使用
func NewProduction(projectName, filePath string) {
SetLogger(conf.WithProjectName(projectName),
Expand Down
54 changes: 23 additions & 31 deletions init_test.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
package k3log

import (
"testing"
"github.com/ThreeKing2018/k3log/conf"
"fmt"
"log"
"runtime"
"testing"

"github.com/ThreeKing2018/k3log/conf"
)

func Test_logge(t *testing.T) {
defer Sync()
func TestLogger(t *testing.T) {
SetLogger(conf.WithLogType(conf.LogJsontype), //打印json格式
conf.WithProjectName("k3日志"), //设置项目名称
conf.WithFilename("log.txt"), //设置输出文件名,或输出的路径
conf.WithLogLevel(conf.ErrorLevel), //设置日志级别,默认debug
conf.WithMaxAge(30), //日志保存天数,默认30天
conf.WithMaxSize(512), //多少M进行分隔日志,默认100M
conf.WithProjectName("k3日志"), //设置项目名称
conf.WithFilename("log.txt"), //设置输出文件名,或输出的路径
conf.WithLogLevel(conf.ErrorLevel), //设置日志级别,默认debug
conf.WithMaxAge(30), //日志保存天数,默认30天
conf.WithMaxSize(512), //多少M进行分隔日志,默认100M
//conf.WithStacktrace(conf.PanicLevel), //设置堆栈级别
conf.WithIsStdOut(true)) //是否同时输出控制台
defer Sync()
Debug("debug日志", 1)
Info("info日志", 2)
Warn("warn日志", 3)
Error("error日志", 4)
Panic("panic", 5)
Fatal("fatal", 6)
//Panic("panic", 5)
//Fatal("fatal", 6)
}

func BenchmarkInfo(t *testing.B) {
t.ResetTimer()
runtime.GOMAXPROCS(runtime.NumCPU())
SetLogger(conf.WithLogType(conf.LogJsontype), //打印json格式
conf.WithProjectName("k3日志"), //设置项目名称
conf.WithFilename("log.txt"), //设置输出文件名,或输出的路径
conf.WithLogLevel(conf.InfoLevel), //设置日志级别,默认debug
conf.WithMaxAge(30), //日志保存天数,默认30天
conf.WithMaxSize(10), //多少M进行分隔日志,默认100M
conf.WithStacktrace(2), //设置堆栈级别
conf.WithIsStdOut(false)) //是否同时输出控制台
conf.WithProjectName("k3日志"), //设置项目名称
conf.WithFilename("log.txt"), //设置输出文件名,或输出的路径
conf.WithLogLevel(conf.InfoLevel), //设置日志级别,默认debug
conf.WithMaxAge(30), //日志保存天数,默认30天
conf.WithMaxSize(10), //多少M进行分隔日志,默认100M
conf.WithStacktrace(2), //设置堆栈级别
conf.WithIsStdOut(false)) //是否同时输出控制台
defer Sync()
t.StartTimer()
for i := 0; i < t.N; i++ {
Info("测试日志", "打印结果", 100)
}
Expand All @@ -50,15 +52,6 @@ func TestError(t *testing.T) {
Error("error", 1)
fmt.Println("继续执行")
}
func TestFatal(t *testing.T) {
Fatal("fatal", 1)
fmt.Println("程序中止")
log.Fatalln()
}
func TestPanic(t *testing.T) {
Panic("panic", 2)
fmt.Println("程序中止")
}
func TestInfo2(t *testing.T) {
Info("aa", 11)
SetLogLevel(conf.InfoLevel)
Expand All @@ -71,9 +64,8 @@ func TestInfo2(t *testing.T) {
func TestDump(t *testing.T) {
type s struct {
Name string
Age int
Age int
}
SetLogger(conf.WithIsStdOut(true))
Dump("name", "dump", "s", s{Name:"k3", Age: 2})
Dump("name", "dump", "s", s{Name: "k3", Age: 2})
}

11 changes: 11 additions & 0 deletions log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{"level":"ERROR","time":"2019-03-08T14:49:01.318+0800","file":"k3log/init_test.go:25","msg":"","service":"k3日志","error日志":4}
{"level":"PANIC","time":"2019-03-08T14:49:01.319+0800","file":"k3log/init_test.go:26","msg":"","service":"k3日志","panic":5,"stack":"github.com/ThreeKing2018/k3log/plugins/zaplog.(*Log).Panic\n\t/Users/wangzl/go-work/src/github.com/ThreeKing2018/k3log/plugins/zaplog/zaplog.go:143\ngithub.com/ThreeKing2018/k3log.Panic\n\t/Users/wangzl/go-work/src/github.com/ThreeKing2018/k3log/init.go:61\ngithub.com/ThreeKing2018/k3log.Test_logge\n\t/Users/wangzl/go-work/src/github.com/ThreeKing2018/k3log/init_test.go:26\ntesting.tRunner\n\t/usr/local/go/src/testing/testing.go:777"}
{"level":"ERROR","time":"2019-03-08T14:50:01.558+0800","file":"k3log/init_test.go:25","msg":"","service":"k3日志","error日志":4}
{"level":"PANIC","time":"2019-03-08T14:50:01.558+0800","file":"k3log/init_test.go:26","msg":"","service":"k3日志","panic":5,"stack":"github.com/ThreeKing2018/k3log/plugins/zaplog.(*Log).Panic\n\t/Users/wangzl/go-work/src/github.com/ThreeKing2018/k3log/plugins/zaplog/zaplog.go:143\ngithub.com/ThreeKing2018/k3log.Panic\n\t/Users/wangzl/go-work/src/github.com/ThreeKing2018/k3log/init.go:61\ngithub.com/ThreeKing2018/k3log.Test_logge\n\t/Users/wangzl/go-work/src/github.com/ThreeKing2018/k3log/init_test.go:26\ntesting.tRunner\n\t/usr/local/Cellar/go/1.10.3/libexec/src/testing/testing.go:777"}
{"level":"ERROR","time":"2019-03-08T14:50:36.022+0800","file":"k3log/init_test.go:25","msg":"","service":"k3日志","error日志":4}
{"level":"PANIC","time":"2019-03-08T14:50:36.023+0800","file":"k3log/init_test.go:26","msg":"","service":"k3日志","panic":5,"stack":"github.com/ThreeKing2018/k3log/plugins/zaplog.(*Log).Panic\n\t/Users/wangzl/go-work/src/github.com/ThreeKing2018/k3log/plugins/zaplog/zaplog.go:143\ngithub.com/ThreeKing2018/k3log.Panic\n\t/Users/wangzl/go-work/src/github.com/ThreeKing2018/k3log/init.go:61\ngithub.com/ThreeKing2018/k3log.Test_logge\n\t/Users/wangzl/go-work/src/github.com/ThreeKing2018/k3log/init_test.go:26\ntesting.tRunner\n\t/usr/local/Cellar/go/1.10.3/libexec/src/testing/testing.go:777"}
{"level":"ERROR","time":"2019-03-08T14:50:49.132+0800","file":"k3log/init_test.go:25","msg":"","service":"k3日志","error日志":4}
{"level":"ERROR","time":"2019-03-08T14:51:00.748+0800","file":"k3log/init_test.go:25","msg":"","service":"k3日志","error日志":4}
{"level":"ERROR","time":"2019-03-08T14:51:09.562+0800","file":"k3log/init_test.go:25","msg":"","service":"k3日志","error日志":4}
{"level":"ERROR","time":"2019-03-08T14:51:57.376+0800","file":"k3log/init_test.go:25","msg":"","service":"k3日志","error日志":4}
{"level":"ERROR","time":"2019-03-08T14:52:19.426+0800","file":"k3log/init_test.go:24","msg":"","service":"k3日志","error日志":4}
12 changes: 12 additions & 0 deletions log/default.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
2019-02-18T14:59:19.803+0800 DEBUG k3log/init_test.go:77 Dump {"name": "(string) (len=4) \"dump\"", "s": "(k3log.s) { Name: (string) (len=2) \"k3\", Age: (int) 2}"}
2019-02-18T14:59:46.105+0800 DEBUG k3log/init_test.go:77 Dump {"name": "(string) (len=4) \"dump\"", "s": "(k3log.s) { Name: (string) (len=2) \"k3\", Age: (int) 2}"}
2019-02-18T15:03:39.207+0800 DEBUG k3log/init_test.go:74 Dump {"default": "(bytes.Buffer) "}
{"level":"DEBUG","time":"2019-03-08T14:51:09.562+0800","file":"k3log/init_test.go:50","msg":"","ddd":200,"aa":2001,"default":"bb"}
{"level":"ERROR","time":"2019-03-08T14:51:09.564+0800","file":"k3log/init_test.go:53","msg":"","error":1}
{"level":"FATAL","time":"2019-03-08T14:51:09.564+0800","file":"k3log/init_test.go:57","msg":"","fatal":1,"stack":"github.com/ThreeKing2018/k3log/plugins/zaplog.(*Log).Fatal\n\t/Users/wangzl/go-work/src/github.com/ThreeKing2018/k3log/plugins/zaplog/zaplog.go:146\ngithub.com/ThreeKing2018/k3log.Fatal\n\t/Users/wangzl/go-work/src/github.com/ThreeKing2018/k3log/init.go:65\ngithub.com/ThreeKing2018/k3log.TestFatal\n\t/Users/wangzl/go-work/src/github.com/ThreeKing2018/k3log/init_test.go:57\ntesting.tRunner\n\t/usr/local/go/src/testing/testing.go:777"}
{"level":"DEBUG","time":"2019-03-08T14:51:57.376+0800","file":"k3log/init_test.go:50","msg":"","ddd":200,"aa":2001,"default":"bb"}
{"level":"ERROR","time":"2019-03-08T14:51:57.376+0800","file":"k3log/init_test.go:53","msg":"","error":1}
{"level":"DEBUG","time":"2019-03-08T14:52:19.426+0800","file":"k3log/init_test.go:49","msg":"","ddd":200,"aa":2001,"default":"bb"}
{"level":"ERROR","time":"2019-03-08T14:52:19.426+0800","file":"k3log/init_test.go:52","msg":"","error":1}
{"level":"INFO","time":"2019-03-08T14:52:19.426+0800","file":"k3log/init_test.go:56","msg":"","aa":11}
{"level":"INFO","time":"2019-03-08T14:52:19.426+0800","file":"k3log/init_test.go:58","msg":"","info":100}
{"level":"WARN","time":"2019-03-08T14:52:19.426+0800","file":"k3log/init_test.go:59","msg":"","warn":200}
{"level":"ERROR","time":"2019-03-08T14:52:19.426+0800","file":"k3log/init_test.go:62","msg":"","err":400}
2019-03-08T14:52:19.426+0800 DEBUG k3log/init_test.go:70 Dump {"name": "(string) (len=4) \"dump\"", "s": "(k3log.s) { Name: (string) (len=2) \"k3\", Age: (int) 2}"}
2 changes: 1 addition & 1 deletion loger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Loger interface {
Error(...interface{}) //错误的
Panic(...interface{}) //恐慌的
Fatal(...interface{}) //致命的
Dump(...interface{}) //详细结构类型,调试利器
Dump(...interface{}) //详细结构类型,调试利器
Sync() //同步
SetLogLevel(conf.Level) //可以随机设置日志级别的
}
23 changes: 11 additions & 12 deletions plugins/zaplog/zaplog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package zaplog

import (
"os"
"strings"

"github.com/ThreeKing2018/k3log/conf"
"github.com/davecgh/go-spew/spew"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"github.com/davecgh/go-spew/spew"
"strings"
)

type Log struct {
logger *zap.SugaredLogger
atom zap.AtomicLevel
options *conf.Options
logger *zap.SugaredLogger
atom zap.AtomicLevel
options *conf.Options
}

func parseLevel(level conf.Level) zapcore.Level {
Expand Down Expand Up @@ -47,7 +48,7 @@ var encoderConfig = zapcore.EncoderConfig{
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}

func New(opts ...conf.Option) *Log {
Expand All @@ -62,7 +63,6 @@ func New(opts ...conf.Option) *Log {
LogType: conf.LogNormalType,
}


for _, opt := range opts {
opt(o)
}
Expand All @@ -74,12 +74,11 @@ func New(opts ...conf.Option) *Log {
MaxBackups: 3,
MaxAge: o.MaxAge, // days
LocalTime: true,
Compress:false,
Compress: false,
})
if o.IsStdOut {
writers = append(writers, os.Stdout)
}

writers = append(writers, osfileout)
w := zapcore.NewMultiWriteSyncer(writers...)

Expand Down Expand Up @@ -108,7 +107,7 @@ func New(opts ...conf.Option) *Log {
logger = logger.With(zap.String(conf.ProjectKey, o.ProjectName))
}
loggerSugar := logger.Sugar()
return &Log{logger: loggerSugar, atom: atom, options:o}
return &Log{logger: loggerSugar, atom: atom, options: o}

}

Expand Down Expand Up @@ -149,11 +148,11 @@ func (l *Log) Fatal(keysAndValues ...interface{}) {
func (l *Log) Dump(keysAndValues ...interface{}) {
arr := CoupArray(keysAndValues)
for k, v := range arr {
if k % 2 == 0 {
if k%2 == 0 {
arr[k] = v
} else {
arr[k] = strings.Replace(spew.Sdump(v), "\n", "", -1)
}
}
l.logger.Debugw("Dump", arr...)
}
}

0 comments on commit be8fbe6

Please sign in to comment.