github.com/grailbio/base@v0.0.11/common/log/log_test.go (about)

     1  package log
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/google/uuid"
     9  )
    10  
    11  var (
    12  	requestID  uuid.UUID
    13  	ctx        context.Context
    14  	TestConfig = Config{
    15  		OutputPaths: []string{"stdout"},
    16  		Level:       DebugLevel,
    17  	}
    18  )
    19  
    20  func setup() {
    21  	requestID, _ = uuid.Parse("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee")
    22  	ctx = WithRequestID(context.Background(), requestID)
    23  	logger = NewLogger(TestConfig)
    24  	logger.now = func() time.Time {
    25  		return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
    26  	}
    27  }
    28  
    29  func ExampleDebug() {
    30  	setup()
    31  	Debug(ctx, "Hello, world!")
    32  	Debug(
    33  		ctx,
    34  		"Hello, world!",
    35  		"foo", "bar",
    36  		"abc", 123,
    37  		"time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC),
    38  	)
    39  	// Output:
    40  	// {"level":"debug","msg":"Hello, world!","caller":"log_test.go:31","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
    41  	// {"level":"debug","msg":"Hello, world!","caller":"log_test.go:32","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
    42  }
    43  
    44  func ExampleInfo() {
    45  	setup()
    46  	Info(ctx, "Hello, world!")
    47  	Info(
    48  		ctx,
    49  		"Hello, world!",
    50  		"foo", "bar",
    51  		"abc", 123,
    52  		"time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC),
    53  	)
    54  	// Output:
    55  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:46","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
    56  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:47","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
    57  }
    58  
    59  func ExampleWarn() {
    60  	setup()
    61  	Warn(ctx, "Hello, world!")
    62  	Warn(
    63  		ctx,
    64  		"Hello, world!",
    65  		"foo", "bar",
    66  		"abc", 123,
    67  		"time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC),
    68  	)
    69  	// Output:
    70  	// {"level":"warn","msg":"Hello, world!","caller":"log_test.go:61","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
    71  	// {"level":"warn","msg":"Hello, world!","caller":"log_test.go:62","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
    72  }
    73  
    74  func ExampleError() {
    75  	setup()
    76  	Error(ctx, "Hello, world!")
    77  	Error(
    78  		ctx,
    79  		"Hello, world!",
    80  		"foo", "bar",
    81  		"abc", 123,
    82  		"time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC),
    83  	)
    84  	// Output:
    85  	// {"level":"error","msg":"Hello, world!","caller":"log_test.go:76","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
    86  	// {"level":"error","msg":"Hello, world!","caller":"log_test.go:77","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
    87  }
    88  
    89  func ExampleDebugf() {
    90  	setup()
    91  	Debugf(ctx, "Hello, %s!", "world")
    92  	// Output:
    93  	// {"level":"debug","msg":"Hello, world!","caller":"log_test.go:91","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
    94  }
    95  
    96  func ExampleInfof() {
    97  	setup()
    98  	Infof(ctx, "Hello, %s!", "world")
    99  	// Output:
   100  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:98","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   101  }
   102  
   103  func ExampleWarnf() {
   104  	setup()
   105  	Warnf(ctx, "Hello, %s!", "world")
   106  	// Output:
   107  	// {"level":"warn","msg":"Hello, world!","caller":"log_test.go:105","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   108  }
   109  
   110  func ExampleErrorf() {
   111  	setup()
   112  	Errorf(ctx, "Hello, %s!", "world")
   113  	// Output:
   114  	// {"level":"error","msg":"Hello, world!","caller":"log_test.go:112","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   115  }
   116  
   117  func ExampleDebugv() {
   118  	setup()
   119  	Debugv(ctx, 0, "Hello, world!")
   120  	// Output:
   121  	// {"level":"debug","msg":"Hello, world!","caller":"log_test.go:119","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   122  }
   123  
   124  func ExampleInfov() {
   125  	setup()
   126  	Infov(ctx, 0, "Hello, world!")
   127  	// Output:
   128  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:126","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   129  }
   130  
   131  func ExampleWarnv() {
   132  	setup()
   133  	Warnv(ctx, 0, "Hello, world!")
   134  	// Output:
   135  	// {"level":"warn","msg":"Hello, world!","caller":"log_test.go:133","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   136  }
   137  
   138  func ExampleErrorv() {
   139  	setup()
   140  	Errorv(ctx, 0, "Hello, world!")
   141  	// Output:
   142  	// {"level":"error","msg":"Hello, world!","caller":"log_test.go:140","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   143  }
   144  
   145  func ExampleDebugNoCtx() {
   146  	setup()
   147  	DebugNoCtx("Hello, world!")
   148  	// Output:
   149  	// {"level":"debug","msg":"Hello, world!","caller":"log_test.go:147","ts":"2000-01-01T00:00:00.000000000Z"}
   150  }
   151  
   152  func ExampleInfoNoCtx() {
   153  	setup()
   154  	InfoNoCtx("Hello, world!")
   155  	// Output:
   156  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:154","ts":"2000-01-01T00:00:00.000000000Z"}
   157  }
   158  
   159  func ExampleWarnNoCtx() {
   160  	setup()
   161  	WarnNoCtx("Hello, world!")
   162  	// Output:
   163  	// {"level":"warn","msg":"Hello, world!","caller":"log_test.go:161","ts":"2000-01-01T00:00:00.000000000Z"}
   164  }
   165  
   166  func ExampleErrorNoCtx() {
   167  	setup()
   168  	ErrorNoCtx("Hello, world!")
   169  	// Output:
   170  	// {"level":"error","msg":"Hello, world!","caller":"log_test.go:168","ts":"2000-01-01T00:00:00.000000000Z"}
   171  }
   172  
   173  func Example_danglingKey() {
   174  	setup()
   175  	Info(context.Background(), "Hello, world!", "myDanglingKey")
   176  	// Output:
   177  	// {"level":"error","msg":"Ignored key without a value.","caller":"log_test.go:175","ts":"2000-01-01T00:00:00.000000000Z","ignored":"myDanglingKey"}
   178  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:175","ts":"2000-01-01T00:00:00.000000000Z"}
   179  }
   180  
   181  func ExampleDebug_logger() {
   182  	setup()
   183  	logger.Debug(ctx, "Hello, world!")
   184  	logger.Debug(
   185  		ctx,
   186  		"Hello, world!",
   187  		"foo", "bar",
   188  		"abc", 123,
   189  		"time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC),
   190  	)
   191  	// Output:
   192  	// {"level":"debug","msg":"Hello, world!","caller":"log_test.go:183","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   193  	// {"level":"debug","msg":"Hello, world!","caller":"log_test.go:184","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
   194  }
   195  
   196  func ExampleInfo_logger() {
   197  	setup()
   198  	logger.Info(ctx, "Hello, world!")
   199  	logger.Info(
   200  		ctx,
   201  		"Hello, world!",
   202  		"foo", "bar",
   203  		"abc", 123,
   204  		"time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC),
   205  	)
   206  	// Output:
   207  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:198","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   208  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:199","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
   209  }
   210  
   211  func ExampleWarn_logger() {
   212  	setup()
   213  	logger.Warn(ctx, "Hello, world!")
   214  	logger.Warn(
   215  		ctx,
   216  		"Hello, world!",
   217  		"foo", "bar",
   218  		"abc", 123,
   219  		"time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC),
   220  	)
   221  	// Output:
   222  	// {"level":"warn","msg":"Hello, world!","caller":"log_test.go:213","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   223  	// {"level":"warn","msg":"Hello, world!","caller":"log_test.go:214","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
   224  }
   225  
   226  func ExampleError_logger() {
   227  	setup()
   228  	logger.Error(ctx, "Hello, world!")
   229  	logger.Error(
   230  		ctx,
   231  		"Hello, world!",
   232  		"foo", "bar",
   233  		"abc", 123,
   234  		"time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC),
   235  	)
   236  	// Output:
   237  	// {"level":"error","msg":"Hello, world!","caller":"log_test.go:228","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   238  	// {"level":"error","msg":"Hello, world!","caller":"log_test.go:229","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
   239  }
   240  
   241  func ExampleDebugf_logger() {
   242  	setup()
   243  	logger.Debugf(ctx, "Hello, %s!", "world")
   244  	// Output:
   245  	// {"level":"debug","msg":"Hello, world!","caller":"log_test.go:243","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   246  }
   247  
   248  func ExampleInfof_logger() {
   249  	setup()
   250  	logger.Infof(ctx, "Hello, %s!", "world")
   251  	// Output:
   252  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:250","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   253  }
   254  
   255  func ExampleWarnf_logger() {
   256  	setup()
   257  	logger.Warnf(ctx, "Hello, %s!", "world")
   258  	// Output:
   259  	// {"level":"warn","msg":"Hello, world!","caller":"log_test.go:257","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   260  }
   261  
   262  func ExampleErrorf_logger() {
   263  	setup()
   264  	logger.Errorf(ctx, "Hello, %s!", "world")
   265  	// Output:
   266  	// {"level":"error","msg":"Hello, world!","caller":"log_test.go:264","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   267  }
   268  
   269  func ExampleDebugv_logger() {
   270  	setup()
   271  	logger.Debugv(ctx, 0, "Hello, world!")
   272  	// Output:
   273  	// {"level":"debug","msg":"Hello, world!","caller":"log_test.go:271","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   274  }
   275  
   276  func ExampleInfov_logger() {
   277  	setup()
   278  	logger.Infov(ctx, 0, "Hello, world!")
   279  	// Output:
   280  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:278","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   281  }
   282  
   283  func ExampleWarnv_logger() {
   284  	setup()
   285  	logger.Warnv(ctx, 0, "Hello, world!")
   286  	// Output:
   287  	// {"level":"warn","msg":"Hello, world!","caller":"log_test.go:285","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   288  }
   289  
   290  func ExampleErrorv_logger() {
   291  	setup()
   292  	logger.Errorv(ctx, 0, "Hello, world!")
   293  	// Output:
   294  	// {"level":"error","msg":"Hello, world!","caller":"log_test.go:292","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   295  }
   296  
   297  func ExampleDebugNoCtx_logger() {
   298  	setup()
   299  	logger.DebugNoCtx("Hello, world!")
   300  	// Output:
   301  	// {"level":"debug","msg":"Hello, world!","caller":"log_test.go:299","ts":"2000-01-01T00:00:00.000000000Z"}
   302  }
   303  
   304  func ExampleInfoNoCtx_logger() {
   305  	setup()
   306  	logger.InfoNoCtx("Hello, world!")
   307  	// Output:
   308  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:306","ts":"2000-01-01T00:00:00.000000000Z"}
   309  }
   310  
   311  func ExampleWarnNoCtx_logger() {
   312  	setup()
   313  	logger.WarnNoCtx("Hello, world!")
   314  	// Output:
   315  	// {"level":"warn","msg":"Hello, world!","caller":"log_test.go:313","ts":"2000-01-01T00:00:00.000000000Z"}
   316  }
   317  
   318  func ExampleErrorNoCtx_logger() {
   319  	setup()
   320  	logger.ErrorNoCtx("Hello, world!")
   321  	// Output:
   322  	// {"level":"error","msg":"Hello, world!","caller":"log_test.go:320","ts":"2000-01-01T00:00:00.000000000Z"}
   323  }
   324  
   325  func Example_level() {
   326  	setup()
   327  	logger = NewLogger(Config{
   328  		OutputPaths: []string{"stdout"},
   329  		Level:       InfoLevel,
   330  	})
   331  	logger.now = func() time.Time {
   332  		return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
   333  	}
   334  	Debug(ctx, "Hello, world!")
   335  	Info(ctx, "Hello, world!")
   336  	// Output:
   337  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:335","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   338  }
   339  
   340  func Example_envVarLogLevel() {
   341  	old := os.Getenv(LOG_LEVEL_ENV_VAR)
   342  	os.Setenv(LOG_LEVEL_ENV_VAR, "WARN")
   343  	setup()
   344  	Info(ctx, "Hello, world!")
   345  	Warn(ctx, "Hello, world!")
   346  	// Output:
   347  	// {"level":"warn","msg":"Hello, world!","caller":"log_test.go:345","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   348  	os.Setenv(LOG_LEVEL_ENV_VAR, old)
   349  }
   350  
   351  func Example_defaultFields() {
   352  	setup()
   353  	logger = NewLoggerWithDefaultFields(Config{
   354  		OutputPaths: []string{"stdout"},
   355  		Level:       InfoLevel,
   356  	}, []interface{}{"foo", "bar"})
   357  	logger.now = func() time.Time {
   358  		return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
   359  	}
   360  	logger.Info(ctx, "Hello, world!")
   361  	// Output:
   362  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:360","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar"}
   363  }
   364  
   365  func Example_defaultFieldsDanglingKey() {
   366  	setup()
   367  	logger = NewLoggerWithDefaultFields(Config{
   368  		OutputPaths: []string{"stdout"},
   369  		Level:       InfoLevel,
   370  	}, []interface{}{"foo", "bar", "foobar"})
   371  	logger.now = func() time.Time {
   372  		return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
   373  	}
   374  	logger.Info(ctx, "Hello, world!")
   375  	// Output:
   376  	// {"level":"error","msg":"defaultFields contains a key without a value.","ignored":"foobar"}
   377  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:374","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar"}
   378  }
   379  
   380  func ExampleDebugfNoCtx() {
   381  	setup()
   382  	DebugfNoCtx("Hello, %s!", "world")
   383  	// Output:
   384  	// {"level":"debug","msg":"Hello, world!","caller":"log_test.go:382","ts":"2000-01-01T00:00:00.000000000Z"}
   385  }
   386  
   387  func ExampleInfofNoCtx() {
   388  	setup()
   389  	InfofNoCtx("Hello, %s!", "world")
   390  	// Output:
   391  	// {"level":"info","msg":"Hello, world!","caller":"log_test.go:389","ts":"2000-01-01T00:00:00.000000000Z"}
   392  }
   393  
   394  func ExampleWarnfNoCtx() {
   395  	setup()
   396  	WarnfNoCtx("Hello, %s!", "world")
   397  	// Output:
   398  	// {"level":"warn","msg":"Hello, world!","caller":"log_test.go:396","ts":"2000-01-01T00:00:00.000000000Z"}
   399  }
   400  
   401  func ExampleErrorfNoCtx() {
   402  	setup()
   403  	ErrorfNoCtx("Hello, %s!", "world")
   404  	// Output:
   405  	// {"level":"error","msg":"Hello, world!","caller":"log_test.go:403","ts":"2000-01-01T00:00:00.000000000Z"}
   406  }
   407  
   408  func ExampleSetLoggerConfig() {
   409  	setup()
   410  	SetLoggerConfig(Config{
   411  		OutputPaths: TestConfig.OutputPaths,
   412  		Level:       InfoLevel,
   413  	})
   414  	logger.now = func() time.Time {
   415  		return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
   416  	}
   417  	Debug(ctx, "Hello, world!")
   418  	Info(ctx, "Goodbye, world!")
   419  	// Output:
   420  	// {"level":"info","msg":"Goodbye, world!","caller":"log_test.go:418","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
   421  }