mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## 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`.
273 lines
6.8 KiB
Go
273 lines
6.8 KiB
Go
package log
|
|
|
|
import (
|
|
"os"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"go.temporal.io/server/common/log/tag"
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zapcore"
|
|
)
|
|
|
|
const (
|
|
skipForZapLogger = 3
|
|
// we put a default message when it is empty so that the log can be searchable/filterable
|
|
defaultMsgForEmpty = "none"
|
|
// TODO: once `NewTestLogger` has been removed, move these vars into testlogger.TestLogger
|
|
TestLogFormatEnvVar = "TEMPORAL_TEST_LOG_FORMAT" // set to "json" for json logs in tests
|
|
TestLogLevelEnvVar = "TEMPORAL_TEST_LOG_LEVEL" // set to "debug" for debug level logs in tests
|
|
)
|
|
|
|
type (
|
|
// zapLogger is logger backed up by zap.Logger.
|
|
zapLogger struct {
|
|
zl *zap.Logger
|
|
skip int
|
|
}
|
|
)
|
|
|
|
var _ Logger = (*zapLogger)(nil)
|
|
|
|
// NewTestLogger returns a logger for tests
|
|
// Deprecated: Use testlogger.TestLogger instead.
|
|
func NewTestLogger() *zapLogger {
|
|
format := os.Getenv(TestLogFormatEnvVar)
|
|
if format == "" {
|
|
format = "console"
|
|
}
|
|
|
|
logger := BuildZapLogger(Config{
|
|
Level: os.Getenv(TestLogLevelEnvVar),
|
|
Format: format,
|
|
Development: true,
|
|
})
|
|
|
|
// Don't include stack traces for warnings during tests. Only include them for logs with level error and above.
|
|
logger = logger.WithOptions(zap.AddStacktrace(zap.ErrorLevel))
|
|
|
|
return NewZapLogger(logger)
|
|
}
|
|
|
|
// NewCLILogger returns a logger at debug level and log into STDERR for logging from within CLI tools
|
|
func NewCLILogger() *zapLogger {
|
|
return NewZapLogger(buildCLIZapLogger())
|
|
}
|
|
|
|
// NewZapLogger returns a new zap based logger from zap.Logger
|
|
func NewZapLogger(zl *zap.Logger) *zapLogger {
|
|
return &zapLogger{
|
|
zl: zl,
|
|
skip: skipForZapLogger,
|
|
}
|
|
}
|
|
|
|
// BuildZapLogger builds and returns a new zap.Logger for this logging configuration
|
|
func BuildZapLogger(cfg Config) *zap.Logger {
|
|
return buildZapLogger(cfg, true)
|
|
}
|
|
|
|
func caller(skip int) string {
|
|
_, path, line, ok := runtime.Caller(skip)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return path + ":" + strconv.Itoa(line)
|
|
}
|
|
|
|
func (l *zapLogger) buildFieldsWithCallAt(tags []tag.Tag) []zap.Field {
|
|
fields := make([]zap.Field, len(tags)+1)
|
|
l.fillFields(tags, fields)
|
|
fields[len(fields)-1] = zap.String(tag.LoggingCallAtKey, caller(l.skip))
|
|
return fields
|
|
}
|
|
|
|
// fillFields fill fields parameter with fields read from tags. Optimized for performance.
|
|
func (l *zapLogger) fillFields(tags []tag.Tag, fields []zap.Field) {
|
|
for i, t := range tags {
|
|
if zt, ok := t.(tag.ZapTag); ok {
|
|
fields[i] = zt.Field()
|
|
} else {
|
|
fields[i] = zap.Any(t.Key(), t.Value())
|
|
}
|
|
}
|
|
}
|
|
|
|
func setDefaultMsg(msg string) string {
|
|
if msg == "" {
|
|
return defaultMsgForEmpty
|
|
}
|
|
return msg
|
|
}
|
|
|
|
func (l *zapLogger) Debug(msg string, tags ...tag.Tag) {
|
|
if l.zl.Core().Enabled(zap.DebugLevel) {
|
|
msg = setDefaultMsg(msg)
|
|
fields := l.buildFieldsWithCallAt(tags)
|
|
l.zl.Debug(msg, fields...)
|
|
}
|
|
}
|
|
|
|
func (l *zapLogger) Info(msg string, tags ...tag.Tag) {
|
|
if l.zl.Core().Enabled(zap.InfoLevel) {
|
|
msg = setDefaultMsg(msg)
|
|
fields := l.buildFieldsWithCallAt(tags)
|
|
l.zl.Info(msg, fields...)
|
|
}
|
|
}
|
|
|
|
func (l *zapLogger) Warn(msg string, tags ...tag.Tag) {
|
|
if l.zl.Core().Enabled(zap.WarnLevel) {
|
|
msg = setDefaultMsg(msg)
|
|
fields := l.buildFieldsWithCallAt(tags)
|
|
l.zl.Warn(msg, fields...)
|
|
}
|
|
}
|
|
|
|
func (l *zapLogger) Error(msg string, tags ...tag.Tag) {
|
|
if l.zl.Core().Enabled(zap.ErrorLevel) {
|
|
msg = setDefaultMsg(msg)
|
|
fields := l.buildFieldsWithCallAt(tags)
|
|
l.zl.Error(msg, fields...)
|
|
}
|
|
}
|
|
|
|
func (l *zapLogger) DPanic(msg string, tags ...tag.Tag) {
|
|
if l.zl.Core().Enabled(zap.DPanicLevel) {
|
|
msg = setDefaultMsg(msg)
|
|
fields := l.buildFieldsWithCallAt(tags)
|
|
l.zl.DPanic(msg, fields...)
|
|
}
|
|
}
|
|
|
|
func (l *zapLogger) Panic(msg string, tags ...tag.Tag) {
|
|
if l.zl.Core().Enabled(zap.PanicLevel) {
|
|
msg = setDefaultMsg(msg)
|
|
fields := l.buildFieldsWithCallAt(tags)
|
|
l.zl.Panic(msg, fields...)
|
|
}
|
|
}
|
|
|
|
func (l *zapLogger) Fatal(msg string, tags ...tag.Tag) {
|
|
if l.zl.Core().Enabled(zap.FatalLevel) {
|
|
msg = setDefaultMsg(msg)
|
|
fields := l.buildFieldsWithCallAt(tags)
|
|
l.zl.Fatal(msg, fields...)
|
|
}
|
|
}
|
|
|
|
func (l *zapLogger) With(tags ...tag.Tag) Logger {
|
|
fields := make([]zap.Field, len(tags))
|
|
l.fillFields(tags, fields)
|
|
zl := l.zl.With(fields...)
|
|
return &zapLogger{
|
|
zl: zl,
|
|
skip: l.skip,
|
|
}
|
|
}
|
|
|
|
func (l *zapLogger) Skip(extraSkip int) Logger {
|
|
return &zapLogger{
|
|
zl: l.zl,
|
|
skip: l.skip + extraSkip,
|
|
}
|
|
}
|
|
|
|
func buildZapLogger(cfg Config, disableCaller bool) *zap.Logger {
|
|
encodeConfig := zapcore.EncoderConfig{
|
|
TimeKey: "ts",
|
|
LevelKey: "level",
|
|
NameKey: "logger",
|
|
CallerKey: zapcore.OmitKey, // we use our own caller
|
|
FunctionKey: zapcore.OmitKey,
|
|
MessageKey: "msg",
|
|
StacktraceKey: "stacktrace",
|
|
LineEnding: zapcore.DefaultLineEnding,
|
|
EncodeLevel: zapcore.LowercaseLevelEncoder,
|
|
EncodeTime: zapcore.ISO8601TimeEncoder,
|
|
EncodeDuration: zapcore.SecondsDurationEncoder,
|
|
EncodeCaller: zapcore.ShortCallerEncoder,
|
|
}
|
|
if disableCaller {
|
|
encodeConfig.CallerKey = zapcore.OmitKey
|
|
encodeConfig.EncodeCaller = nil
|
|
}
|
|
|
|
outputPath := "stderr"
|
|
if len(cfg.OutputFile) > 0 {
|
|
outputPath = cfg.OutputFile
|
|
}
|
|
if cfg.Stdout {
|
|
outputPath = "stdout"
|
|
}
|
|
encoding := "json"
|
|
if cfg.Format == "console" {
|
|
encoding = "console"
|
|
}
|
|
config := zap.Config{
|
|
Level: zap.NewAtomicLevelAt(parseZapLevel(cfg.Level)),
|
|
Development: cfg.Development,
|
|
Sampling: nil,
|
|
Encoding: encoding,
|
|
EncoderConfig: encodeConfig,
|
|
OutputPaths: []string{outputPath},
|
|
ErrorOutputPaths: []string{outputPath},
|
|
DisableCaller: disableCaller,
|
|
}
|
|
logger, _ := config.Build()
|
|
return logger
|
|
}
|
|
|
|
func buildCLIZapLogger() *zap.Logger {
|
|
encodeConfig := zapcore.EncoderConfig{
|
|
TimeKey: "ts",
|
|
LevelKey: "level",
|
|
NameKey: "logger",
|
|
CallerKey: zapcore.OmitKey, // we use our own caller
|
|
FunctionKey: zapcore.OmitKey,
|
|
MessageKey: "msg",
|
|
StacktraceKey: "stacktrace",
|
|
LineEnding: zapcore.DefaultLineEnding,
|
|
EncodeLevel: zapcore.CapitalColorLevelEncoder,
|
|
EncodeTime: zapcore.ISO8601TimeEncoder,
|
|
EncodeDuration: zapcore.SecondsDurationEncoder,
|
|
EncodeCaller: nil,
|
|
}
|
|
|
|
config := zap.Config{
|
|
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
|
|
Development: false,
|
|
DisableStacktrace: os.Getenv("TEMPORAL_CLI_SHOW_STACKS") == "",
|
|
Sampling: nil,
|
|
Encoding: "console",
|
|
EncoderConfig: encodeConfig,
|
|
OutputPaths: []string{"stderr"},
|
|
ErrorOutputPaths: []string{"stderr"},
|
|
DisableCaller: true,
|
|
}
|
|
logger, _ := config.Build()
|
|
return logger
|
|
}
|
|
|
|
func parseZapLevel(level string) zapcore.Level {
|
|
switch strings.ToLower(level) {
|
|
case "debug":
|
|
return zap.DebugLevel
|
|
case "info":
|
|
return zap.InfoLevel
|
|
case "warn":
|
|
return zap.WarnLevel
|
|
case "error":
|
|
return zap.ErrorLevel
|
|
case "dpanic":
|
|
return zap.DPanicLevel
|
|
case "panic":
|
|
return zap.PanicLevel
|
|
case "fatal":
|
|
return zap.FatalLevel
|
|
default:
|
|
return zap.InfoLevel
|
|
}
|
|
}
|