加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
option_test.go 19.38 KB
一键复制 编辑 原始数据 按行查看 历史
水不要鱼 提交于 2022-10-24 20:28 . 增加 Err 方法
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
// Copyright 2022 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logit
import (
"context"
"os"
"testing"
"github.com/FishGoddess/logit/core/appender"
)
// go test -v -cover -run=^TestOptions$
func TestOptions(t *testing.T) {
opts := Options()
if opts != nil {
t.Errorf("Options returns wrong options %+v", opts)
}
}
// go test -v -cover -run=^TestOptionsWithDebugLevel$
func TestOptionsWithDebugLevel(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.level = offLevel
opts.WithDebugLevel().Apply(logger)
if logger.level != debugLevel {
t.Errorf("logger's level %s is wrong", logger.level)
}
}
// go test -v -cover -run=^TestOptionsWithInfoLevel$
func TestOptionsWithInfoLevel(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.level = offLevel
opts.WithInfoLevel().Apply(logger)
if logger.level != infoLevel {
t.Errorf("logger's level %s is wrong", logger.level)
}
}
// go test -v -cover -run=^TestOptionsWithWarnLevel$
func TestOptionsWithWarnLevel(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.level = offLevel
opts.WithWarnLevel().Apply(logger)
if logger.level != warnLevel {
t.Errorf("logger's level %s is wrong", logger.level)
}
}
// go test -v -cover -run=^TestOptionsWithErrorLevel$
func TestOptionsWithErrorLevel(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.level = offLevel
opts.WithErrorLevel().Apply(logger)
if logger.level != errorLevel {
t.Errorf("logger's level %s is wrong", logger.level)
}
}
// go test -v -cover -run=^TestOptionsWithPrintLevel$
func TestOptionsWithPrintLevel(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.level = offLevel
opts.WithPrintLevel().Apply(logger)
if logger.level != printLevel {
t.Errorf("logger's level %s is wrong", logger.level)
}
}
// go test -v -cover -run=^TestOptionsWithOffLevel$
func TestOptionsWithOffLevel(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.level = debugLevel
opts.WithOffLevel().Apply(logger)
if logger.level != offLevel {
t.Errorf("logger's level %s is wrong", logger.level)
}
}
// go test -v -cover -run=^TestOptionsWithAppender$
func TestOptionsWithAppender(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugAppender = nil
logger.infoAppender = nil
logger.warnAppender = nil
logger.errorAppender = nil
logger.printAppender = nil
opts.WithAppender(appender.Text()).Apply(logger)
if logger.debugAppender != appender.Text() {
t.Errorf("logger's debugAppender %s is wrong", logger.debugAppender)
}
if logger.infoAppender != appender.Text() {
t.Errorf("logger's infoAppender %s is wrong", logger.infoAppender)
}
if logger.warnAppender != appender.Text() {
t.Errorf("logger's warnAppender %s is wrong", logger.warnAppender)
}
if logger.errorAppender != appender.Text() {
t.Errorf("logger's errorAppender %s is wrong", logger.errorAppender)
}
if logger.printAppender != appender.Text() {
t.Errorf("logger's printAppender %s is wrong", logger.printAppender)
}
logger.debugAppender = nil
logger.infoAppender = nil
logger.warnAppender = nil
logger.errorAppender = nil
logger.printAppender = nil
opts.WithAppender(appender.Json()).Apply(logger)
if logger.debugAppender != appender.Json() {
t.Errorf("logger's debugAppender %s is wrong", logger.debugAppender)
}
if logger.infoAppender != appender.Json() {
t.Errorf("logger's infoAppender %s is wrong", logger.infoAppender)
}
if logger.warnAppender != appender.Json() {
t.Errorf("logger's warnAppender %s is wrong", logger.warnAppender)
}
if logger.errorAppender != appender.Json() {
t.Errorf("logger's errorAppender %s is wrong", logger.errorAppender)
}
if logger.printAppender != appender.Json() {
t.Errorf("logger's printAppender %s is wrong", logger.printAppender)
}
}
// go test -v -cover -run=^TestOptionsWithDebugAppender$
func TestOptionsWithDebugAppender(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugAppender = nil
logger.infoAppender = nil
logger.warnAppender = nil
logger.errorAppender = nil
logger.printAppender = nil
opts.WithDebugAppender(appender.Text()).Apply(logger)
if logger.debugAppender != appender.Text() {
t.Errorf("logger's debugAppender %s is wrong", logger.debugAppender)
}
if logger.infoAppender != nil {
t.Errorf("logger's infoAppender %s is wrong", logger.infoAppender)
}
if logger.warnAppender != nil {
t.Errorf("logger's warnAppender %s is wrong", logger.warnAppender)
}
if logger.errorAppender != nil {
t.Errorf("logger's errorAppender %s is wrong", logger.errorAppender)
}
if logger.printAppender != nil {
t.Errorf("logger's printAppender %s is wrong", logger.printAppender)
}
}
// go test -v -cover -run=^TestOptionsWithInfoAppender$
func TestOptionsWithInfoAppender(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugAppender = nil
logger.infoAppender = nil
logger.warnAppender = nil
logger.errorAppender = nil
logger.printAppender = nil
opts.WithInfoAppender(appender.Text()).Apply(logger)
if logger.debugAppender != nil {
t.Errorf("logger's debugAppender %s is wrong", logger.debugAppender)
}
if logger.infoAppender != appender.Text() {
t.Errorf("logger's infoAppender %s is wrong", logger.infoAppender)
}
if logger.warnAppender != nil {
t.Errorf("logger's warnAppender %s is wrong", logger.warnAppender)
}
if logger.errorAppender != nil {
t.Errorf("logger's errorAppender %s is wrong", logger.errorAppender)
}
if logger.printAppender != nil {
t.Errorf("logger's printAppender %s is wrong", logger.printAppender)
}
}
// go test -v -cover -run=^TestOptionsWithWarnAppender$
func TestOptionsWithWarnAppender(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugAppender = nil
logger.infoAppender = nil
logger.warnAppender = nil
logger.errorAppender = nil
logger.printAppender = nil
opts.WithWarnAppender(appender.Text()).Apply(logger)
if logger.debugAppender != nil {
t.Errorf("logger's debugAppender %s is wrong", logger.debugAppender)
}
if logger.infoAppender != nil {
t.Errorf("logger's infoAppender %s is wrong", logger.infoAppender)
}
if logger.warnAppender != appender.Text() {
t.Errorf("logger's warnAppender %s is wrong", logger.warnAppender)
}
if logger.errorAppender != nil {
t.Errorf("logger's errorAppender %s is wrong", logger.errorAppender)
}
if logger.printAppender != nil {
t.Errorf("logger's printAppender %s is wrong", logger.printAppender)
}
}
// go test -v -cover -run=^TestOptionsWithErrorAppender$
func TestOptionsWithErrorAppender(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugAppender = nil
logger.infoAppender = nil
logger.warnAppender = nil
logger.errorAppender = nil
logger.printAppender = nil
opts.WithErrorAppender(appender.Text()).Apply(logger)
if logger.debugAppender != nil {
t.Errorf("logger's debugAppender %s is wrong", logger.debugAppender)
}
if logger.infoAppender != nil {
t.Errorf("logger's infoAppender %s is wrong", logger.infoAppender)
}
if logger.warnAppender != nil {
t.Errorf("logger's warnAppender %s is wrong", logger.warnAppender)
}
if logger.errorAppender != appender.Text() {
t.Errorf("logger's errorAppender %s is wrong", logger.errorAppender)
}
if logger.printAppender != nil {
t.Errorf("logger's printAppender %s is wrong", logger.printAppender)
}
}
// go test -v -cover -run=^TestOptionsWithPrintAppender$
func TestOptionsWithPrintAppender(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugAppender = nil
logger.infoAppender = nil
logger.warnAppender = nil
logger.errorAppender = nil
logger.printAppender = nil
opts.WithPrintAppender(appender.Text()).Apply(logger)
if logger.debugAppender != nil {
t.Errorf("logger's debugAppender %s is wrong", logger.debugAppender)
}
if logger.infoAppender != nil {
t.Errorf("logger's infoAppender %s is wrong", logger.infoAppender)
}
if logger.warnAppender != nil {
t.Errorf("logger's warnAppender %s is wrong", logger.warnAppender)
}
if logger.errorAppender != nil {
t.Errorf("logger's errorAppender %s is wrong", logger.errorAppender)
}
if logger.printAppender != appender.Text() {
t.Errorf("logger's printAppender %s is wrong", logger.printAppender)
}
}
// go test -v -cover -run=^TestOptionsWithWriter$
func TestOptionsWithWriter(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugWriter = nil
logger.infoWriter = nil
logger.warnWriter = nil
logger.errorWriter = nil
logger.printWriter = nil
opts.WithWriter(os.Stdout).Apply(logger)
if logger.debugWriter == nil {
t.Errorf("logger's debugWriter %s is wrong", logger.debugWriter)
}
if logger.infoWriter == nil {
t.Errorf("logger's infoWriter %s is wrong", logger.infoWriter)
}
if logger.warnWriter == nil {
t.Errorf("logger's warnWriter %s is wrong", logger.warnWriter)
}
if logger.errorWriter == nil {
t.Errorf("logger's errorWriter %s is wrong", logger.errorWriter)
}
if logger.printWriter == nil {
t.Errorf("logger's printWriter %s is wrong", logger.printWriter)
}
}
// go test -v -cover -run=^TestOptionsWithBufferWriter$
func TestOptionsWithBufferWriter(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugWriter = nil
logger.infoWriter = nil
logger.warnWriter = nil
logger.errorWriter = nil
logger.printWriter = nil
opts.WithBufferWriter(os.Stdout).Apply(logger)
if logger.debugWriter == nil {
t.Errorf("logger's debugWriter %s is wrong", logger.debugWriter)
}
if logger.infoWriter == nil {
t.Errorf("logger's infoWriter %s is wrong", logger.infoWriter)
}
if logger.warnWriter == nil {
t.Errorf("logger's warnWriter %s is wrong", logger.warnWriter)
}
if logger.errorWriter == nil {
t.Errorf("logger's errorWriter %s is wrong", logger.errorWriter)
}
if logger.printWriter == nil {
t.Errorf("logger's printWriter %s is wrong", logger.printWriter)
}
}
// go test -v -cover -run=^TestOptionsWithBatchWriter$
func TestOptionsWithBatchWriter(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugWriter = nil
logger.infoWriter = nil
logger.warnWriter = nil
logger.errorWriter = nil
logger.printWriter = nil
opts.WithBatchWriter(os.Stdout).Apply(logger)
if logger.debugWriter == nil {
t.Errorf("logger's debugWriter %s is wrong", logger.debugWriter)
}
if logger.infoWriter == nil {
t.Errorf("logger's infoWriter %s is wrong", logger.infoWriter)
}
if logger.warnWriter == nil {
t.Errorf("logger's warnWriter %s is wrong", logger.warnWriter)
}
if logger.errorWriter == nil {
t.Errorf("logger's errorWriter %s is wrong", logger.errorWriter)
}
if logger.printWriter == nil {
t.Errorf("logger's printWriter %s is wrong", logger.printWriter)
}
}
// go test -v -cover -run=^TestOptionsWithDebugWriter$
func TestOptionsWithDebugWriter(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugWriter = nil
logger.infoWriter = nil
logger.warnWriter = nil
logger.errorWriter = nil
logger.printWriter = nil
opts.WithDebugWriter(os.Stdout).Apply(logger)
if logger.debugWriter == nil {
t.Errorf("logger's debugWriter %s is wrong", logger.debugWriter)
}
if logger.infoWriter != nil {
t.Errorf("logger's infoWriter %s is wrong", logger.infoWriter)
}
if logger.warnWriter != nil {
t.Errorf("logger's warnWriter %s is wrong", logger.warnWriter)
}
if logger.errorWriter != nil {
t.Errorf("logger's errorWriter %s is wrong", logger.errorWriter)
}
if logger.printWriter != nil {
t.Errorf("logger's printWriter %s is wrong", logger.printWriter)
}
}
// go test -v -cover -run=^TestOptionsWithInfoWriter$
func TestOptionsWithInfoWriter(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugWriter = nil
logger.infoWriter = nil
logger.warnWriter = nil
logger.errorWriter = nil
logger.printWriter = nil
opts.WithInfoWriter(os.Stdout).Apply(logger)
if logger.debugWriter != nil {
t.Errorf("logger's debugWriter %s is wrong", logger.debugWriter)
}
if logger.infoWriter == nil {
t.Errorf("logger's infoWriter %s is wrong", logger.infoWriter)
}
if logger.warnWriter != nil {
t.Errorf("logger's warnWriter %s is wrong", logger.warnWriter)
}
if logger.errorWriter != nil {
t.Errorf("logger's errorWriter %s is wrong", logger.errorWriter)
}
if logger.printWriter != nil {
t.Errorf("logger's printWriter %s is wrong", logger.printWriter)
}
}
// go test -v -cover -run=^TestOptionsWithWarnWriter$
func TestOptionsWithWarnWriter(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugWriter = nil
logger.infoWriter = nil
logger.warnWriter = nil
logger.errorWriter = nil
logger.printWriter = nil
opts.WithWarnWriter(os.Stdout).Apply(logger)
if logger.debugWriter != nil {
t.Errorf("logger's debugWriter %s is wrong", logger.debugWriter)
}
if logger.infoWriter != nil {
t.Errorf("logger's infoWriter %s is wrong", logger.infoWriter)
}
if logger.warnWriter == nil {
t.Errorf("logger's warnWriter %s is wrong", logger.warnWriter)
}
if logger.errorWriter != nil {
t.Errorf("logger's errorWriter %s is wrong", logger.errorWriter)
}
if logger.printWriter != nil {
t.Errorf("logger's printWriter %s is wrong", logger.printWriter)
}
}
// go test -v -cover -run=^TestOptionsWithErrorWriter$
func TestOptionsWithErrorWriter(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugWriter = nil
logger.infoWriter = nil
logger.warnWriter = nil
logger.errorWriter = nil
logger.printWriter = nil
opts.WithErrorWriter(os.Stdout).Apply(logger)
if logger.debugWriter != nil {
t.Errorf("logger's debugWriter %s is wrong", logger.debugWriter)
}
if logger.infoWriter != nil {
t.Errorf("logger's infoWriter %s is wrong", logger.infoWriter)
}
if logger.warnWriter != nil {
t.Errorf("logger's warnWriter %s is wrong", logger.warnWriter)
}
if logger.errorWriter == nil {
t.Errorf("logger's errorWriter %s is wrong", logger.errorWriter)
}
if logger.printWriter != nil {
t.Errorf("logger's printWriter %s is wrong", logger.printWriter)
}
}
// go test -v -cover -run=^TestOptionsWithPrintWriter$
func TestOptionsWithPrintWriter(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.debugWriter = nil
logger.infoWriter = nil
logger.warnWriter = nil
logger.errorWriter = nil
logger.printWriter = nil
opts.WithPrintWriter(os.Stdout).Apply(logger)
if logger.debugWriter != nil {
t.Errorf("logger's debugWriter %s is wrong", logger.debugWriter)
}
if logger.infoWriter != nil {
t.Errorf("logger's infoWriter %s is wrong", logger.infoWriter)
}
if logger.warnWriter != nil {
t.Errorf("logger's warnWriter %s is wrong", logger.warnWriter)
}
if logger.errorWriter != nil {
t.Errorf("logger's errorWriter %s is wrong", logger.errorWriter)
}
if logger.printWriter == nil {
t.Errorf("logger's printWriter %s is wrong", logger.printWriter)
}
}
// go test -v -cover -run=^TestOptionsWithPID$
func TestOptionsWithPID(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.withPID = false
opts.WithPID().Apply(logger)
if logger.withPID != true {
t.Errorf("logger's withPID %+v is wrong", logger.withPID)
}
}
// go test -v -cover -run=^TestOptionsWithCaller$
func TestOptionsWithCaller(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.withCaller = false
opts.WithCaller().Apply(logger)
if logger.withCaller != true {
t.Errorf("logger's withCaller %+v is wrong", logger.withCaller)
}
}
// go test -v -cover -run=^TestOptionsWithMsgKey$
func TestOptionsWithMsgKey(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.msgKey = ""
opts.WithMsgKey("msg").Apply(logger)
if logger.msgKey != "msg" {
t.Errorf("logger's msgKey %+v is wrong", logger.msgKey)
}
}
// go test -v -cover -run=^TestOptionsWithTimeKey$
func TestOptionsWithTimeKey(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.timeKey = ""
opts.WithTimeKey("time").Apply(logger)
if logger.timeKey != "time" {
t.Errorf("logger's timeKey %+v is wrong", logger.timeKey)
}
}
// go test -v -cover -run=^TestOptionsWithLevelKey$
func TestOptionsWithLevelKey(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.levelKey = ""
opts.WithLevelKey("level").Apply(logger)
if logger.levelKey != "level" {
t.Errorf("logger's levelKey %+v is wrong", logger.levelKey)
}
}
// go test -v -cover -run=^TestOptionsWithPIDKey$
func TestOptionsWithPIDKey(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.pidKey = ""
opts.WithPIDKey("pid").Apply(logger)
if logger.pidKey != "pid" {
t.Errorf("logger's pidKey %+v is wrong", logger.pidKey)
}
}
// go test -v -cover -run=^TestOptionsWithFileKey$
func TestOptionsWithFileKey(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.fileKey = ""
opts.WithFileKey("file").Apply(logger)
if logger.fileKey != "file" {
t.Errorf("logger's fileKey %+v is wrong", logger.fileKey)
}
}
// go test -v -cover -run=^TestOptionsWithLineKey$
func TestOptionsWithLineKey(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.lineKey = ""
opts.WithLineKey("line").Apply(logger)
if logger.lineKey != "line" {
t.Errorf("logger's lineKey %+v is wrong", logger.lineKey)
}
}
// go test -v -cover -run=^TestOptionsWithFuncKey$
func TestOptionsWithFuncKey(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.funcKey = ""
opts.WithFuncKey("func").Apply(logger)
if logger.funcKey != "func" {
t.Errorf("logger's funcKey %+v is wrong", logger.funcKey)
}
}
// go test -v -cover -run=^TestOptionsWithErrorKey$
func TestOptionsWithErrorKey(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.errorKey = ""
opts.WithErrorKey("err").Apply(logger)
if logger.errorKey != "err" {
t.Errorf("logger's errorKey %+v is wrong", logger.errorKey)
}
}
// go test -v -cover -run=^TestOptionsWithTimeFormat$
func TestOptionsWithTimeFormat(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.timeFormat = ""
opts.WithTimeFormat("20060102150405").Apply(logger)
if logger.timeFormat != "20060102150405" {
t.Errorf("logger's timeFormat %+v is wrong", logger.timeFormat)
}
}
// go test -v -cover -run=^TestOptionsWithCallerDepth$
func TestOptionsWithCallerDepth(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.callerDepth = 0
opts.WithCallerDepth(3).Apply(logger)
if logger.callerDepth != 3 {
t.Errorf("logger's callerDepth %d is wrong", logger.callerDepth)
}
}
// go test -v -cover -run=^TestOptionsWithInterceptors$
func TestOptionsWithInterceptors(t *testing.T) {
opts := Options()
logger := NewLogger()
logger.interceptors = nil
interceptors := []Interceptor{
func(ctx context.Context, log *Log) {},
func(ctx context.Context, log *Log) {},
func(ctx context.Context, log *Log) {},
}
opts.WithInterceptors(interceptors...).Apply(logger)
if len(logger.interceptors) != len(interceptors) {
t.Errorf("len(logger.interceptors) %d != len(interceptors) %d", len(logger.interceptors), len(interceptors))
}
logger.interceptors = nil
opts.WithInterceptors()
if len(logger.interceptors) != 0 {
t.Errorf("len(logger.interceptors) %d != 0", len(logger.interceptors))
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化