...

Source file src/github.com/rs/zerolog/binary_test.go

Documentation: github.com/rs/zerolog

     1  // +build binary_log
     2  
     3  package zerolog
     4  
     5  import (
     6  	"bytes"
     7  	"errors"
     8  	"fmt"
     9  
    10  	//	"io/ioutil"
    11  	stdlog "log"
    12  	"time"
    13  )
    14  
    15  func ExampleBinaryNew() {
    16  	dst := bytes.Buffer{}
    17  	log := New(&dst)
    18  
    19  	log.Info().Msg("hello world")
    20  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
    21  	// Output: {"level":"info","message":"hello world"}
    22  }
    23  
    24  func ExampleLogger_With() {
    25  	dst := bytes.Buffer{}
    26  	log := New(&dst).
    27  		With().
    28  		Str("foo", "bar").
    29  		Logger()
    30  
    31  	log.Info().Msg("hello world")
    32  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
    33  
    34  	// Output: {"level":"info","foo":"bar","message":"hello world"}
    35  }
    36  
    37  func ExampleLogger_Level() {
    38  	dst := bytes.Buffer{}
    39  	log := New(&dst).Level(WarnLevel)
    40  
    41  	log.Info().Msg("filtered out message")
    42  	log.Error().Msg("kept message")
    43  
    44  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
    45  	// Output: {"level":"error","message":"kept message"}
    46  }
    47  
    48  func ExampleLogger_Sample() {
    49  	dst := bytes.Buffer{}
    50  	log := New(&dst).Sample(&BasicSampler{N: 2})
    51  
    52  	log.Info().Msg("message 1")
    53  	log.Info().Msg("message 2")
    54  	log.Info().Msg("message 3")
    55  	log.Info().Msg("message 4")
    56  
    57  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
    58  	// Output: {"level":"info","message":"message 1"}
    59  	// {"level":"info","message":"message 3"}
    60  }
    61  
    62  type LevelNameHook1 struct{}
    63  
    64  func (h LevelNameHook1) Run(e *Event, l Level, msg string) {
    65  	if l != NoLevel {
    66  		e.Str("level_name", l.String())
    67  	} else {
    68  		e.Str("level_name", "NoLevel")
    69  	}
    70  }
    71  
    72  type MessageHook string
    73  
    74  func (h MessageHook) Run(e *Event, l Level, msg string) {
    75  	e.Str("the_message", msg)
    76  }
    77  
    78  func ExampleLogger_Hook() {
    79  	var levelNameHook LevelNameHook1
    80  	var messageHook MessageHook = "The message"
    81  
    82  	dst := bytes.Buffer{}
    83  	log := New(&dst).Hook(levelNameHook).Hook(messageHook)
    84  
    85  	log.Info().Msg("hello world")
    86  
    87  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
    88  	// Output: {"level":"info","level_name":"info","the_message":"hello world","message":"hello world"}
    89  }
    90  
    91  func ExampleLogger_Print() {
    92  	dst := bytes.Buffer{}
    93  	log := New(&dst)
    94  
    95  	log.Print("hello world")
    96  
    97  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
    98  	// Output: {"level":"debug","message":"hello world"}
    99  }
   100  
   101  func ExampleLogger_Printf() {
   102  	dst := bytes.Buffer{}
   103  	log := New(&dst)
   104  
   105  	log.Printf("hello %s", "world")
   106  
   107  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   108  	// Output: {"level":"debug","message":"hello world"}
   109  }
   110  
   111  func ExampleLogger_Trace() {
   112  	dst := bytes.Buffer{}
   113  	log := New(&dst)
   114  
   115  	log.Trace().
   116  		Str("foo", "bar").
   117  		Int("n", 123).
   118  		Msg("hello world")
   119  
   120  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   121  	// Output: {"level":"trace","foo":"bar","n":123,"message":"hello world"}
   122  }
   123  
   124  func ExampleLogger_Debug() {
   125  	dst := bytes.Buffer{}
   126  	log := New(&dst)
   127  
   128  	log.Debug().
   129  		Str("foo", "bar").
   130  		Int("n", 123).
   131  		Msg("hello world")
   132  
   133  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   134  	// Output: {"level":"debug","foo":"bar","n":123,"message":"hello world"}
   135  }
   136  
   137  func ExampleLogger_Info() {
   138  	dst := bytes.Buffer{}
   139  	log := New(&dst)
   140  
   141  	log.Info().
   142  		Str("foo", "bar").
   143  		Int("n", 123).
   144  		Msg("hello world")
   145  
   146  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   147  	// Output: {"level":"info","foo":"bar","n":123,"message":"hello world"}
   148  }
   149  
   150  func ExampleLogger_Warn() {
   151  	dst := bytes.Buffer{}
   152  	log := New(&dst)
   153  
   154  	log.Warn().
   155  		Str("foo", "bar").
   156  		Msg("a warning message")
   157  
   158  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   159  	// Output: {"level":"warn","foo":"bar","message":"a warning message"}
   160  }
   161  
   162  func ExampleLogger_Error() {
   163  	dst := bytes.Buffer{}
   164  	log := New(&dst)
   165  
   166  	log.Error().
   167  		Err(errors.New("some error")).
   168  		Msg("error doing something")
   169  
   170  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   171  	// Output: {"level":"error","error":"some error","message":"error doing something"}
   172  }
   173  
   174  func ExampleLogger_WithLevel() {
   175  	dst := bytes.Buffer{}
   176  	log := New(&dst)
   177  
   178  	log.WithLevel(InfoLevel).
   179  		Msg("hello world")
   180  
   181  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   182  	// Output: {"level":"info","message":"hello world"}
   183  }
   184  
   185  func ExampleLogger_Write() {
   186  	dst := bytes.Buffer{}
   187  	log := New(&dst).With().
   188  		Str("foo", "bar").
   189  		Logger()
   190  
   191  	stdlog.SetFlags(0)
   192  	stdlog.SetOutput(log)
   193  
   194  	stdlog.Print("hello world")
   195  
   196  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   197  	// Output: {"foo":"bar","message":"hello world"}
   198  }
   199  
   200  func ExampleLogger_Log() {
   201  	dst := bytes.Buffer{}
   202  	log := New(&dst)
   203  
   204  	log.Log().
   205  		Str("foo", "bar").
   206  		Str("bar", "baz").
   207  		Msg("")
   208  
   209  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   210  	// Output: {"foo":"bar","bar":"baz"}
   211  }
   212  
   213  func ExampleEvent_Dict() {
   214  	dst := bytes.Buffer{}
   215  	log := New(&dst)
   216  
   217  	log.Log().
   218  		Str("foo", "bar").
   219  		Dict("dict", Dict().
   220  			Str("bar", "baz").
   221  			Int("n", 1),
   222  		).
   223  		Msg("hello world")
   224  
   225  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   226  	// Output: {"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"}
   227  }
   228  
   229  type User struct {
   230  	Name    string
   231  	Age     int
   232  	Created time.Time
   233  }
   234  
   235  func (u User) MarshalZerologObject(e *Event) {
   236  	e.Str("name", u.Name).
   237  		Int("age", u.Age).
   238  		Time("created", u.Created)
   239  }
   240  
   241  type Users []User
   242  
   243  func (uu Users) MarshalZerologArray(a *Array) {
   244  	for _, u := range uu {
   245  		a.Object(u)
   246  	}
   247  }
   248  
   249  func ExampleEvent_Array() {
   250  	dst := bytes.Buffer{}
   251  	log := New(&dst)
   252  
   253  	log.Log().
   254  		Str("foo", "bar").
   255  		Array("array", Arr().
   256  			Str("baz").
   257  			Int(1),
   258  		).
   259  		Msg("hello world")
   260  
   261  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   262  	// Output: {"foo":"bar","array":["baz",1],"message":"hello world"}
   263  }
   264  
   265  func ExampleEvent_Array_object() {
   266  	dst := bytes.Buffer{}
   267  	log := New(&dst)
   268  
   269  	// Users implements LogArrayMarshaler
   270  	u := Users{
   271  		User{"John", 35, time.Time{}},
   272  		User{"Bob", 55, time.Time{}},
   273  	}
   274  
   275  	log.Log().
   276  		Str("foo", "bar").
   277  		Array("users", u).
   278  		Msg("hello world")
   279  
   280  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   281  	// Output: {"foo":"bar","users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bob","age":55,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
   282  }
   283  
   284  func ExampleEvent_Object() {
   285  	dst := bytes.Buffer{}
   286  	log := New(&dst)
   287  
   288  	// User implements LogObjectMarshaler
   289  	u := User{"John", 35, time.Time{}}
   290  
   291  	log.Log().
   292  		Str("foo", "bar").
   293  		Object("user", u).
   294  		Msg("hello world")
   295  
   296  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   297  	// Output: {"foo":"bar","user":{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},"message":"hello world"}
   298  }
   299  
   300  func ExampleEvent_EmbedObject() {
   301  	price := Price{val: 6449, prec: 2, unit: "$"}
   302  
   303  	dst := bytes.Buffer{}
   304  	log := New(&dst)
   305  
   306  	log.Log().
   307  		Str("foo", "bar").
   308  		EmbedObject(price).
   309  		Msg("hello world")
   310  
   311  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   312  	// Output: {"foo":"bar","price":"$64.49","message":"hello world"}
   313  }
   314  
   315  func ExampleEvent_Interface() {
   316  	dst := bytes.Buffer{}
   317  	log := New(&dst)
   318  
   319  	obj := struct {
   320  		Name string `json:"name"`
   321  	}{
   322  		Name: "john",
   323  	}
   324  
   325  	log.Log().
   326  		Str("foo", "bar").
   327  		Interface("obj", obj).
   328  		Msg("hello world")
   329  
   330  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   331  	// Output: {"foo":"bar","obj":{"name":"john"},"message":"hello world"}
   332  }
   333  
   334  func ExampleEvent_Dur() {
   335  	d := time.Duration(10 * time.Second)
   336  
   337  	dst := bytes.Buffer{}
   338  	log := New(&dst)
   339  
   340  	log.Log().
   341  		Str("foo", "bar").
   342  		Dur("dur", d).
   343  		Msg("hello world")
   344  
   345  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   346  	// Output: {"foo":"bar","dur":10000,"message":"hello world"}
   347  }
   348  
   349  func ExampleEvent_Durs() {
   350  	d := []time.Duration{
   351  		time.Duration(10 * time.Second),
   352  		time.Duration(20 * time.Second),
   353  	}
   354  
   355  	dst := bytes.Buffer{}
   356  	log := New(&dst)
   357  
   358  	log.Log().
   359  		Str("foo", "bar").
   360  		Durs("durs", d).
   361  		Msg("hello world")
   362  
   363  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   364  	// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
   365  }
   366  
   367  func ExampleEvent_Fields_map() {
   368  	fields := map[string]interface{}{
   369  		"bar": "baz",
   370  		"n":   1,
   371  	}
   372  
   373  	dst := bytes.Buffer{}
   374  	log := New(&dst)
   375  
   376  	log.Log().
   377  		Str("foo", "bar").
   378  		Fields(fields).
   379  		Msg("hello world")
   380  
   381  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   382  	// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
   383  }
   384  
   385  func ExampleEvent_Fields_slice() {
   386  	fields := []interface{}{
   387  		"bar", "baz",
   388  		"n", 1,
   389  	}
   390  
   391  	dst := bytes.Buffer{}
   392  	log := New(&dst)
   393  
   394  	log.Log().
   395  		Str("foo", "bar").
   396  		Fields(fields).
   397  		Msg("hello world")
   398  
   399  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   400  	// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
   401  }
   402  
   403  func ExampleContext_Dict() {
   404  	dst := bytes.Buffer{}
   405  	log := New(&dst).With().
   406  		Str("foo", "bar").
   407  		Dict("dict", Dict().
   408  			Str("bar", "baz").
   409  			Int("n", 1),
   410  		).Logger()
   411  
   412  	log.Log().Msg("hello world")
   413  
   414  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   415  	// Output: {"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"}
   416  }
   417  
   418  func ExampleContext_Array() {
   419  	dst := bytes.Buffer{}
   420  	log := New(&dst).With().
   421  		Str("foo", "bar").
   422  		Array("array", Arr().
   423  			Str("baz").
   424  			Int(1),
   425  		).Logger()
   426  
   427  	log.Log().Msg("hello world")
   428  
   429  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   430  	// Output: {"foo":"bar","array":["baz",1],"message":"hello world"}
   431  }
   432  
   433  func ExampleContext_Array_object() {
   434  	// Users implements LogArrayMarshaler
   435  	u := Users{
   436  		User{"John", 35, time.Time{}},
   437  		User{"Bob", 55, time.Time{}},
   438  	}
   439  
   440  	dst := bytes.Buffer{}
   441  	log := New(&dst).With().
   442  		Str("foo", "bar").
   443  		Array("users", u).
   444  		Logger()
   445  
   446  	log.Log().Msg("hello world")
   447  
   448  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   449  	// Output: {"foo":"bar","users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bob","age":55,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
   450  }
   451  
   452  type Price struct {
   453  	val  uint64
   454  	prec int
   455  	unit string
   456  }
   457  
   458  func (p Price) MarshalZerologObject(e *Event) {
   459  	denom := uint64(1)
   460  	for i := 0; i < p.prec; i++ {
   461  		denom *= 10
   462  	}
   463  	result := []byte(p.unit)
   464  	result = append(result, fmt.Sprintf("%d.%d", p.val/denom, p.val%denom)...)
   465  	e.Str("price", string(result))
   466  }
   467  
   468  func ExampleContext_EmbedObject() {
   469  	price := Price{val: 6449, prec: 2, unit: "$"}
   470  
   471  	dst := bytes.Buffer{}
   472  	log := New(&dst).With().
   473  		Str("foo", "bar").
   474  		EmbedObject(price).
   475  		Logger()
   476  
   477  	log.Log().Msg("hello world")
   478  
   479  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   480  	// Output: {"foo":"bar","price":"$64.49","message":"hello world"}
   481  }
   482  func ExampleContext_Object() {
   483  	// User implements LogObjectMarshaler
   484  	u := User{"John", 35, time.Time{}}
   485  
   486  	dst := bytes.Buffer{}
   487  	log := New(&dst).With().
   488  		Str("foo", "bar").
   489  		Object("user", u).
   490  		Logger()
   491  
   492  	log.Log().Msg("hello world")
   493  
   494  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   495  	// Output: {"foo":"bar","user":{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},"message":"hello world"}
   496  }
   497  
   498  func ExampleContext_Interface() {
   499  	obj := struct {
   500  		Name string `json:"name"`
   501  	}{
   502  		Name: "john",
   503  	}
   504  
   505  	dst := bytes.Buffer{}
   506  	log := New(&dst).With().
   507  		Str("foo", "bar").
   508  		Interface("obj", obj).
   509  		Logger()
   510  
   511  	log.Log().Msg("hello world")
   512  
   513  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   514  	// Output: {"foo":"bar","obj":{"name":"john"},"message":"hello world"}
   515  }
   516  
   517  func ExampleContext_Dur() {
   518  	d := time.Duration(10 * time.Second)
   519  
   520  	dst := bytes.Buffer{}
   521  	log := New(&dst).With().
   522  		Str("foo", "bar").
   523  		Dur("dur", d).
   524  		Logger()
   525  
   526  	log.Log().Msg("hello world")
   527  
   528  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   529  	// Output: {"foo":"bar","dur":10000,"message":"hello world"}
   530  }
   531  
   532  func ExampleContext_Durs() {
   533  	d := []time.Duration{
   534  		time.Duration(10 * time.Second),
   535  		time.Duration(20 * time.Second),
   536  	}
   537  
   538  	dst := bytes.Buffer{}
   539  	log := New(&dst).With().
   540  		Str("foo", "bar").
   541  		Durs("durs", d).
   542  		Logger()
   543  
   544  	log.Log().Msg("hello world")
   545  
   546  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   547  	// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
   548  }
   549  
   550  func ExampleContext_Fields_map() {
   551  	fields := map[string]interface{}{
   552  		"bar": "baz",
   553  		"n":   1,
   554  	}
   555  
   556  	dst := bytes.Buffer{}
   557  	log := New(&dst).With().
   558  		Str("foo", "bar").
   559  		Fields(fields).
   560  		Logger()
   561  
   562  	log.Log().Msg("hello world")
   563  
   564  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   565  	// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
   566  }
   567  
   568  func ExampleContext_Fields_slice() {
   569  	fields := []interface{}{
   570  		"bar", "baz",
   571  		"n", 1,
   572  	}
   573  
   574  	dst := bytes.Buffer{}
   575  	log := New(&dst).With().
   576  		Str("foo", "bar").
   577  		Fields(fields).
   578  		Logger()
   579  
   580  	log.Log().Msg("hello world")
   581  
   582  	fmt.Println(decodeIfBinaryToString(dst.Bytes()))
   583  	// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
   584  }
   585  

View as plain text