github.com/shogo82148/goa-v1@v1.6.2/logging_test.go (about)

     1  package goa_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"log"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/shogo82148/goa-v1"
    11  )
    12  
    13  var _ = Describe("Info", func() {
    14  	Context("with a nil Log", func() {
    15  		It("doesn't log and doesn't crash", func() {
    16  			Ω(func() { goa.LogInfo(context.Background(), "foo", "bar") }).ShouldNot(Panic())
    17  		})
    18  	})
    19  })
    20  
    21  var _ = Describe("Warn", func() {
    22  	Context("with a nil Log", func() {
    23  		It("doesn't log and doesn't crash", func() {
    24  			Ω(func() { goa.LogWarn(context.Background(), "foo", "bar") }).ShouldNot(Panic())
    25  		})
    26  	})
    27  })
    28  
    29  var _ = Describe("Error", func() {
    30  	Context("with a nil Log", func() {
    31  		It("doesn't log and doesn't crash", func() {
    32  			Ω(func() { goa.LogError(context.Background(), "foo", "bar") }).ShouldNot(Panic())
    33  		})
    34  	})
    35  })
    36  
    37  var _ = Describe("LogAdapter", func() {
    38  	Context("with a valid Log", func() {
    39  		var logger goa.LogAdapter
    40  		const msg = "message"
    41  		data := []interface{}{"data", "foo"}
    42  
    43  		var out bytes.Buffer
    44  
    45  		BeforeEach(func() {
    46  			stdlogger := log.New(&out, "", log.LstdFlags)
    47  			logger = goa.NewLogger(stdlogger)
    48  		})
    49  
    50  		It("Info logs", func() {
    51  			logger.Info(msg, data...)
    52  			Ω(out.String()).Should(ContainSubstring(msg + " data=foo"))
    53  		})
    54  
    55  		It("Warn logs", func() {
    56  			logger := logger.(goa.WarningLogAdapter)
    57  			logger.Warn(msg, data...)
    58  			Ω(out.String()).Should(ContainSubstring(msg + " data=foo"))
    59  		})
    60  
    61  		It("Error logs", func() {
    62  			logger.Error(msg, data...)
    63  			Ω(out.String()).Should(ContainSubstring(msg + " data=foo"))
    64  		})
    65  	})
    66  })