github.com/go-spring/spring-base@v1.1.3/log/plugin_layout_test.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package log_test
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/go-spring/spring-base/assert"
    26  	"github.com/go-spring/spring-base/clock"
    27  	"github.com/go-spring/spring-base/code"
    28  	"github.com/go-spring/spring-base/knife"
    29  	"github.com/go-spring/spring-base/log"
    30  )
    31  
    32  func TestParseColorStyle(t *testing.T) {
    33  
    34  	v, err := log.ParseColorStyle("abc")
    35  	assert.Error(t, err, "invalid color style 'abc'")
    36  
    37  	v, err = log.ParseColorStyle("none")
    38  	assert.Nil(t, err)
    39  	assert.Equal(t, v, log.ColorStyleNone)
    40  
    41  	v, err = log.ParseColorStyle("normal")
    42  	assert.Nil(t, err)
    43  	assert.Equal(t, v, log.ColorStyleNormal)
    44  
    45  	v, err = log.ParseColorStyle("bright")
    46  	assert.Nil(t, err)
    47  	assert.Equal(t, v, log.ColorStyleBright)
    48  }
    49  
    50  func TestPatternLayout(t *testing.T) {
    51  
    52  	layout := log.PatternLayout{
    53  		ColorStyle: log.ColorStyleNormal,
    54  		Pattern:    "[:level][:time][:fileline][:msg]",
    55  	}
    56  	err := layout.Init()
    57  	assert.Nil(t, err)
    58  
    59  	ctx, _ := knife.New(context.Background())
    60  	ctx = context.WithValue(ctx, "traceKey", "123456789")
    61  	_ = clock.SetFixedTime(ctx, time.Date(2022, 9, 30, 8, 0, 0, 0, time.UTC))
    62  
    63  	e := &log.Event{
    64  		//Entry: new(log.ContextEntry).WithTag("tagABC").WithContext(ctx),
    65  		File:  code.File(),
    66  		Line:  code.Line(),
    67  		Level: log.InfoLevel,
    68  		Fields: []log.Field{
    69  			log.String("field_a", "abc"),
    70  			log.Int("field_b", 5),
    71  		},
    72  	}
    73  
    74  	e.Level = log.TraceLevel
    75  	b, err := layout.ToBytes(e)
    76  	assert.Nil(t, err)
    77  	fmt.Print(string(b))
    78  	//assert.Equal(t, string(b), "[TRACE][2022-09-30T08:00:00.000][...ring/spring-base/log/plugin_layout_test.go:43] tagABC||field_a=abc||field_b=5\n")
    79  
    80  	e.Level = log.DebugLevel
    81  	b, err = layout.ToBytes(e)
    82  	assert.Nil(t, err)
    83  	fmt.Print(string(b))
    84  	//assert.Equal(t, string(b), "[DEBUG][2022-09-30T08:00:00.000][...ring/spring-base/log/plugin_layout_test.go:43] tagABC||field_a=abc||field_b=5\n")
    85  
    86  	e.Level = log.InfoLevel
    87  	b, err = layout.ToBytes(e)
    88  	assert.Nil(t, err)
    89  	fmt.Print(string(b))
    90  	//assert.Equal(t, string(b), "[INFO][2022-09-30T08:00:00.000][...ring/spring-base/log/plugin_layout_test.go:43] tagABC||field_a=abc||field_b=5\n")
    91  
    92  	e.Level = log.WarnLevel
    93  	b, err = layout.ToBytes(e)
    94  	assert.Nil(t, err)
    95  	fmt.Print(string(b))
    96  	//assert.Equal(t, string(b), "[WARN][2022-09-30T08:00:00.000][...ring/spring-base/log/plugin_layout_test.go:43] tagABC||field_a=abc||field_b=5\n")
    97  
    98  	e.Level = log.ErrorLevel
    99  	b, err = layout.ToBytes(e)
   100  	assert.Nil(t, err)
   101  	fmt.Print(string(b))
   102  	//assert.Equal(t, string(b), "[ERROR][2022-09-30T08:00:00.000][...ring/spring-base/log/plugin_layout_test.go:43] tagABC||field_a=abc||field_b=5\n")
   103  
   104  	e.Level = log.PanicLevel
   105  	b, err = layout.ToBytes(e)
   106  	assert.Nil(t, err)
   107  	fmt.Print(string(b))
   108  	//assert.Equal(t, string(b), "[PANIC][2022-09-30T08:00:00.000][...ring/spring-base/log/plugin_layout_test.go:43] tagABC||field_a=abc||field_b=5\n")
   109  
   110  	e.Level = log.FatalLevel
   111  	b, err = layout.ToBytes(e)
   112  	assert.Nil(t, err)
   113  	fmt.Print(string(b))
   114  	//assert.Equal(t, string(b), "[FATAL][2022-09-30T08:00:00.000][...ring/spring-base/log/plugin_layout_test.go:43] tagABC||field_a=abc||field_b=5\n")
   115  }
   116  
   117  func TestJSONLayout(t *testing.T) {
   118  
   119  	layout := log.JSONLayout{}
   120  
   121  	ctx, _ := knife.New(context.Background())
   122  	ctx = context.WithValue(ctx, "traceKey", "123456789")
   123  	_ = clock.SetFixedTime(ctx, time.Date(2022, 9, 30, 8, 0, 0, 0, time.UTC))
   124  
   125  	e := &log.Event{
   126  		//Entry: new(log.ContextEntry).WithTag("tagABC").WithContext(ctx),
   127  		File:  code.File(),
   128  		Line:  code.Line(),
   129  		Level: log.InfoLevel,
   130  		Fields: []log.Field{
   131  			log.String("field_a", "abc"),
   132  			log.Int("field_b", 5),
   133  		},
   134  	}
   135  
   136  	e.Level = log.TraceLevel
   137  	b, err := layout.ToBytes(e)
   138  	assert.Nil(t, err)
   139  	fmt.Print(string(b))
   140  	//assert.Equal(t, string(b), "{\"field_a\":\"abc\",\"field_b\":5}\n")
   141  
   142  	e.Level = log.DebugLevel
   143  	b, err = layout.ToBytes(e)
   144  	assert.Nil(t, err)
   145  	fmt.Print(string(b))
   146  	//assert.Equal(t, string(b), "{\"field_a\":\"abc\",\"field_b\":5}\n")
   147  
   148  	e.Level = log.InfoLevel
   149  	b, err = layout.ToBytes(e)
   150  	assert.Nil(t, err)
   151  	fmt.Print(string(b))
   152  	//assert.Equal(t, string(b), "{\"field_a\":\"abc\",\"field_b\":5}\n")
   153  
   154  	e.Level = log.WarnLevel
   155  	b, err = layout.ToBytes(e)
   156  	assert.Nil(t, err)
   157  	fmt.Print(string(b))
   158  	//assert.Equal(t, string(b), "{\"field_a\":\"abc\",\"field_b\":5}\n")
   159  
   160  	e.Level = log.ErrorLevel
   161  	b, err = layout.ToBytes(e)
   162  	assert.Nil(t, err)
   163  	fmt.Print(string(b))
   164  	//assert.Equal(t, string(b), "{\"field_a\":\"abc\",\"field_b\":5}\n")
   165  
   166  	e.Level = log.PanicLevel
   167  	b, err = layout.ToBytes(e)
   168  	assert.Nil(t, err)
   169  	fmt.Print(string(b))
   170  	//assert.Equal(t, string(b), "{\"field_a\":\"abc\",\"field_b\":5}\n")
   171  
   172  	e.Level = log.FatalLevel
   173  	b, err = layout.ToBytes(e)
   174  	assert.Nil(t, err)
   175  	fmt.Print(string(b))
   176  	//assert.Equal(t, string(b), "{\"field_a\":\"abc\",\"field_b\":5}\n")
   177  }