github.com/holochain/holochain-proto@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/log_test.go (about)

     1  package holochain
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	. "github.com/smartystreets/goconvey/convey"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  // needed to setup the holochain environment, not really a test.
    12  func TestNewLog(t *testing.T) {
    13  
    14  	Convey("it should log according format string", t, func() {
    15  		var buf bytes.Buffer
    16  		l1 := Logger{Enabled: true}
    17  		err := l1.New(&buf)
    18  		So(err, ShouldBeNil)
    19  
    20  		l2 := Logger{
    21  			Enabled: true,
    22  			Format:  "L2:%{message}",
    23  		}
    24  		err = l2.New(&buf)
    25  		So(err, ShouldBeNil)
    26  		l1.Log("fish")
    27  		l2.Logf("%d blue", 2)
    28  		So(buf.String(), ShouldEqual, "fish\nL2:2 blue\n")
    29  	})
    30  
    31  	Convey("it should handle time", t, func() {
    32  		var buf bytes.Buffer
    33  		l := Logger{
    34  			Enabled: true,
    35  			Format:  "%{time}:%{message}",
    36  		}
    37  
    38  		tf, f := l.setupTime("X:%{message}")
    39  		So(tf, ShouldEqual, "")
    40  		So(f, ShouldEqual, "X:%{message}")
    41  
    42  		tf, f = l.setupTime("x%{time}y")
    43  		So(f, ShouldEqual, "x%{time}y")
    44  		So(tf, ShouldEqual, "Jan _2 15:04:05")
    45  
    46  		tf, f = l.setupTime("x%{time:xxy}y")
    47  		So(f, ShouldEqual, "x%{time}y")
    48  		So(tf, ShouldEqual, "xxy")
    49  
    50  		l.New(&buf)
    51  		now := time.Unix(1, 1)
    52  		So(l._parse("fish", &now), ShouldEqual, now.Format(time.Stamp)+":fish")
    53  	})
    54  
    55  	Convey("it should handle color", t, func() {
    56  		var buf bytes.Buffer
    57  		l := Logger{
    58  			Enabled: true,
    59  			Format:  "%{color:blue}%{time}:%{message}",
    60  		}
    61  
    62  		c, f := l.setupColor("x")
    63  		So(c, ShouldBeNil)
    64  		So(f, ShouldEqual, "x")
    65  
    66  		c, f = l.setupColor("prefix%{color:red}%{message}")
    67  		So(fmt.Sprintf("%v", c), ShouldEqual, "&{[31] <nil>}")
    68  		So(f, ShouldEqual, "prefix%{message}")
    69  
    70  		l.New(&buf)
    71  		now := time.Unix(1, 1)
    72  		So(l._parse("fish", &now), ShouldEqual, now.Format(time.Stamp)+":fish")
    73  	})
    74  
    75  	Convey("it should log with a prefix", t, func() {
    76  		var buf bytes.Buffer
    77  		l := Logger{
    78  			Enabled: true,
    79  		}
    80  		l.New(&buf)
    81  
    82  		l.Log("onefish")
    83  		l.SetPrefix("[PREFIX]")
    84  		l.Log("twofish")
    85  		l.SetPrefix("[COLOR PREFIX]")
    86  		l.Log("threefish")
    87  		So(buf.String(), ShouldEqual, "onefish\n[PREFIX]twofish\n[COLOR PREFIX]threefish\n")
    88  	})
    89  
    90  	Convey("it should handle file name and line number", t, func() {
    91  		var buf bytes.Buffer
    92  		l := Logger{
    93  			Enabled: true,
    94  			Format:  "%{file}.%{line}:%{message}",
    95  		}
    96  		l.New(&buf)
    97  		doDebug(l, "fish")
    98  		So(buf.String(), ShouldEqual, "log_test.go.97:fish\n")
    99  	})
   100  
   101  }
   102  
   103  // we do this because in the case of file & line needs to know how many
   104  // calls back up the stack to use for calculating the line number and we
   105  // allways have a wrapper function around the log
   106  func doDebug(l Logger, m string) {
   107  	l.Log(m)
   108  }