Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions log/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ type actLogger struct {
}

func (l actLogger) Info(m Map) {
if !isOff.Load() {
m["traceID"] = l.trcId
m["identifier"] = l.identifier
actZapLog.Info("", zap.Any("payload", m))
}
m["traceID"] = l.trcId
m["identifier"] = l.identifier
actZapLog.Info("", zap.Any("payload", m))
}

// Activity start ActLogger.
Expand All @@ -34,6 +32,11 @@ func Activity(c context.Context) ActLogger {
c = context.Background()
}

// use the no-op logger instead if the context contain off signal
if v, ok := c.Value(logOffKey).(bool); ok && v {
return noop{}
}

actOnce.Do(func() {
// setup log writer
actLogWriter := &writer{wr: setupLog("activity")}
Expand Down
25 changes: 11 additions & 14 deletions log/log.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package log

import (
"context"
"fmt"
"io"
"os"
"strings"
"sync/atomic"

"github.com/bytedance/sonic"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type logCtxKey struct{}

var logOffKey = logCtxKey{}

// Off give signal to logger to not write log. Simply inject this to Activity,
// Runtime or Worker.
func Off() context.Context {
return context.WithValue(context.Background(), logOffKey, true)
}

const logDir = "./storage/logs"

var (
cleanupTasks []func()
isOff atomic.Bool
output io.WriteCloser
)

Expand All @@ -36,18 +45,6 @@ func Cleanup() {
}
}

// Off to not print the log. Useful when unit testing, since we mostly won't
// need it in unit test.
func Off() {
isOff.Store(true)
}

// On print the log. By default, there is no need to call this function unless
// Off is ever called.
func On() {
isOff.Store(false)
}

type writer struct {
wr io.WriteCloser
}
Expand Down
7 changes: 7 additions & 0 deletions log/noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package log

type noop struct{}

func (noop) Warning(_ Map) {}
func (noop) Error(_ Map) {}
func (noop) Info(_ Map) {}
32 changes: 14 additions & 18 deletions log/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,19 @@ type runLogger struct {
}

func (l runLogger) Info(m Map) {
if !isOff.Load() {
// add request id
m["traceID"] = l.trcId
m["identifier"] = l.identifier
runZapLog.Info("", zap.Any("payload", m))
}
m["traceID"] = l.trcId
m["identifier"] = l.identifier
runZapLog.Info("", zap.Any("payload", m))
}
func (l runLogger) Warning(m Map) {
if !isOff.Load() {
// add request id
m["traceID"] = l.trcId
m["identifier"] = l.identifier
runZapLog.Warn("", zap.Any("payload", m))
}
m["traceID"] = l.trcId
m["identifier"] = l.identifier
runZapLog.Warn("", zap.Any("payload", m))
}
func (l runLogger) Error(m Map) {
if !isOff.Load() {
// add request id
m["traceID"] = l.trcId
m["identifier"] = l.identifier
runZapLog.Error("", zap.Any("payload", m))
}
m["traceID"] = l.trcId
m["identifier"] = l.identifier
runZapLog.Error("", zap.Any("payload", m))
}

// Runtime start RunLogger.
Expand All @@ -51,6 +42,11 @@ func Runtime(c context.Context) RunLogger {
c = context.Background()
}

// use the no-op logger instead if the context contain off signal
if v, ok := c.Value(logOffKey).(bool); ok && v {
return noop{}
}

runOnce.Do(func() {
// setup log writer
runLogWriter := &writer{wr: setupLog("runtime")}
Expand Down
14 changes: 8 additions & 6 deletions log/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ type wrkLogger struct {
}

func (l wrkLogger) Info(m Map) {
if !isOff.Load() {
// add request id
m["traceID"] = l.trcId
m["identifier"] = l.identifier
wrkZapLog.Info("", zap.Any("payload", m))
}
m["traceID"] = l.trcId
m["identifier"] = l.identifier
wrkZapLog.Info("", zap.Any("payload", m))
}

// Worker start WorkLogger.
Expand All @@ -35,6 +32,11 @@ func Worker(c context.Context) WorkLogger {
c = context.Background()
}

// use the no-op logger instead if the context contain off signal
if v, ok := c.Value(logOffKey).(bool); ok && v {
return noop{}
}

wrkOnce.Do(func() {
// setup log writer
wrkLogWriter := &writer{wr: setupLog("worker")}
Expand Down