...

Source file src/github.com/cybertec-postgresql/pgwatch/v3/internal/log/log_test.go

Documentation: github.com/cybertec-postgresql/pgwatch/v3/internal/log

     1  package log_test
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/cybertec-postgresql/pgwatch/v3/internal/log"
     9  	"github.com/jackc/pgx/v5/tracelog"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestInit(t *testing.T) {
    14  	assert.NotNil(t, log.Init(log.CmdOpts{LogLevel: "debug"}))
    15  	l := log.Init(log.CmdOpts{LogLevel: "foobar"})
    16  	pgxl := log.NewPgxLogger(l)
    17  	assert.NotNil(t, pgxl)
    18  	ctx := log.WithLogger(context.Background(), l)
    19  	assert.True(t, log.GetLogger(ctx) == l)
    20  	assert.True(t, log.GetLogger(context.Background()) == log.FallbackLogger)
    21  }
    22  
    23  func TestFileLogger(t *testing.T) {
    24  	l := log.Init(log.CmdOpts{LogLevel: "debug", LogFile: "test.log", LogFileFormat: "text"})
    25  	l.Info("test")
    26  	assert.FileExists(t, "test.log", "Log file should be created")
    27  	_ = os.Remove("test.log")
    28  }
    29  
    30  func TestPgxLog(_ *testing.T) {
    31  	pgxl := log.NewPgxLogger(log.Init(log.CmdOpts{LogLevel: "trace"}))
    32  	var level tracelog.LogLevel
    33  	for level = tracelog.LogLevelNone; level <= tracelog.LogLevelTrace; level++ {
    34  		pgxl.Log(context.Background(), level, "foo", map[string]interface{}{"func": "TestPgxLog"})
    35  	}
    36  }
    37