Files
tasks/common/log/zap_logger_test.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

165 lines
4.5 KiB
Go

package log
import (
"bytes"
"fmt"
"io"
"os"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/tests/testutils"
"go.uber.org/zap"
)
type LogSuite struct {
*require.Assertions
suite.Suite
}
func TestLogSuite(t *testing.T) {
suite.Run(t, new(LogSuite))
}
func (s *LogSuite) SetupTest() {
s.Assertions = require.New(s.T())
}
func (s *LogSuite) TestParseLogLevel() {
s.Equal(zap.DebugLevel, parseZapLevel("debug"))
s.Equal(zap.InfoLevel, parseZapLevel("info"))
s.Equal(zap.WarnLevel, parseZapLevel("warn"))
s.Equal(zap.ErrorLevel, parseZapLevel("error"))
s.Equal(zap.FatalLevel, parseZapLevel("fatal"))
s.Equal(zap.DPanicLevel, parseZapLevel("dpanic"))
s.Equal(zap.PanicLevel, parseZapLevel("panic"))
s.Equal(zap.InfoLevel, parseZapLevel("unknown"))
}
func (s *LogSuite) TestNewLogger() {
dir := testutils.MkdirTemp(s.T(), "", "config.testNewLogger")
cfg := Config{
Level: "info",
OutputFile: dir + "/test.log",
}
log := BuildZapLogger(cfg)
s.NotNil(log)
_, err := os.Stat(dir + "/test.log")
s.Nil(err)
log.DPanic("Development default is false; should not panic here!")
s.Panics(nil, func() {
log.Panic("Must Panic")
})
cfg = Config{
Level: "info",
OutputFile: dir + "/test.log",
Development: true,
}
log = BuildZapLogger(cfg)
s.NotNil(log)
_, err = os.Stat(dir + "/test.log")
s.Nil(err)
s.Panics(nil, func() {
log.DPanic("Must panic!")
})
s.Panics(nil, func() {
log.Panic("Must panic!")
})
}
func TestDefaultLogger(t *testing.T) {
old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w
outC := make(chan string)
// copy the output in a separate goroutine so logging can't block indefinitely
go func() {
var buf bytes.Buffer
_, err := io.Copy(&buf, r)
assert.NoError(t, err)
outC <- buf.String()
}()
logger := NewZapLogger(zap.NewExample())
preCaller := caller(1)
logger.With(tag.Error(fmt.Errorf("test error"))).Info("test info", tag.WorkflowActionWorkflowStarted)
// back to normal state
require.Nil(t, w.Close())
os.Stdout = old // restoring the real stdout
out := <-outC
sps := strings.Split(preCaller, ":")
par, err := strconv.Atoi(sps[1])
assert.Nil(t, err)
lineNum := fmt.Sprintf("%v", par+1)
assert.Regexp(t, `{"level":"info","msg":"test info","error":"test error","wf-action":"add-workflow-started-event","logging-call-at":".*zap_logger_test.go:`+lineNum+`"}`+"\n", out)
}
func TestThrottleLogger(t *testing.T) {
old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w
outC := make(chan string)
// copy the output in a separate goroutine so logging can't block indefinitely
go func() {
var buf bytes.Buffer
_, err := io.Copy(&buf, r)
assert.NoError(t, err)
outC <- buf.String()
}()
logger := NewThrottledLogger(NewZapLogger(zap.NewExample()),
func() float64 { return 1 })
preCaller := caller(1)
With(With(logger, tag.Error(fmt.Errorf("test error"))), tag.ComponentShardContext).Info("test info", tag.WorkflowActionWorkflowStarted)
// back to normal state
require.Nil(t, w.Close())
os.Stdout = old // restoring the real stdout
out := <-outC
sps := strings.Split(preCaller, ":")
par, err := strconv.Atoi(sps[1])
assert.Nil(t, err)
lineNum := fmt.Sprintf("%v", par+1)
fmt.Println(out, lineNum)
assert.Regexp(t, `{"level":"info","msg":"test info","error":"test error","component":"shard-context","wf-action":"add-workflow-started-event","logging-call-at":".*zap_logger_test.go:`+lineNum+`"}`+"\n", out)
}
func TestEmptyMsg(t *testing.T) {
old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w
outC := make(chan string)
// copy the output in a separate goroutine so logging can't block indefinitely
go func() {
var buf bytes.Buffer
_, err := io.Copy(&buf, r)
assert.NoError(t, err)
outC <- buf.String()
}()
logger := NewZapLogger(zap.NewExample())
preCaller := caller(1)
logger.With(tag.Error(fmt.Errorf("test error"))).Info("", tag.WorkflowActionWorkflowStarted)
// back to normal state
require.Nil(t, w.Close())
os.Stdout = old // restoring the real stdout
out := <-outC
sps := strings.Split(preCaller, ":")
par, err := strconv.Atoi(sps[1])
assert.Nil(t, err)
lineNum := fmt.Sprintf("%v", par+1)
fmt.Println(out, lineNum)
assert.Regexp(t, `{"level":"info","msg":"`+defaultMsgForEmpty+`","error":"test error","wf-action":"add-workflow-started-event","logging-call-at":".*zap_logger_test.go:`+lineNum+`"}`+"\n", out)
}