Skip to content

Commit ebc9dde

Browse files
authored
chore(execd): replace logger with internal package (opensandbox-group#237)
1 parent 281b976 commit ebc9dde

8 files changed

Lines changed: 67 additions & 67 deletions

File tree

components/execd/Dockerfile

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,25 @@ FROM golang:1.24.0 AS builder
1616

1717
WORKDIR /build
1818

19-
COPY go.mod go.sum ./
19+
# Prepare local modules to satisfy replace directives.
20+
COPY components/internal/go.mod components/internal/go.sum ./components/internal/
21+
COPY components/execd/go.mod components/execd/go.sum ./components/execd/
2022

21-
RUN go mod download
23+
# Download deps with only mod files for better caching.
24+
RUN cd components/internal && go mod download
25+
RUN cd components/execd && go mod download
2226

23-
COPY . .
27+
# Copy sources.
28+
COPY components/internal ./components/internal
29+
COPY components/execd ./components/execd
30+
31+
WORKDIR /build/components/execd
2432

2533
RUN CGO_ENABLED=0 go build -o /build/execd ./main.go
2634

2735
FROM alpine:latest
2836

2937
COPY --from=builder /build/execd .
30-
COPY bootstrap.sh .
38+
COPY components/execd/bootstrap.sh ./bootstrap.sh
3139

3240
ENTRYPOINT ["./execd"]

components/execd/build.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ set -ex
1717

1818
TAG=${TAG:-latest}
1919

20+
REPO_ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
21+
2022
docker buildx rm execd-builder || true
2123

2224
docker buildx create --use --name execd-builder
@@ -30,4 +32,5 @@ docker buildx build \
3032
-t sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/execd:${TAG} \
3133
--platform linux/amd64,linux/arm64 \
3234
--push \
33-
.
35+
-f components/execd/Dockerfile \
36+
"${REPO_ROOT}"

components/execd/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/alibaba/opensandbox/execd
33
go 1.24.0
44

55
require (
6+
github.com/alibaba/opensandbox/internal v0.0.0
67
github.com/bmatcuk/doublestar/v4 v4.9.1
78
github.com/gin-gonic/gin v1.10.0
89
github.com/go-playground/validator/v10 v10.28.0
@@ -12,7 +13,6 @@ require (
1213
github.com/shirou/gopsutil v3.21.11+incompatible
1314
github.com/stretchr/testify v1.10.0
1415
go.uber.org/automaxprocs v1.6.0
15-
go.uber.org/zap v1.27.0
1616
k8s.io/apimachinery v0.34.2
1717
k8s.io/client-go v0.34.2
1818
)
@@ -49,6 +49,7 @@ require (
4949
github.com/x448/float16 v0.8.4 // indirect
5050
github.com/yusufpapurcu/wmi v1.2.4 // indirect
5151
go.uber.org/multierr v1.10.0 // indirect
52+
go.uber.org/zap v1.27.0 // indirect
5253
go.yaml.in/yaml/v2 v2.4.2 // indirect
5354
golang.org/x/arch v0.8.0 // indirect
5455
golang.org/x/crypto v0.45.0 // indirect
@@ -65,3 +66,5 @@ require (
6566
sigs.k8s.io/randfill v1.0.0 // indirect
6667
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 // indirect
6768
)
69+
70+
replace github.com/alibaba/opensandbox/internal => ../internal

components/execd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
func main() {
3131
flag.InitFlags()
3232

33-
log.SetLevel(flag.ServerLogLevel)
33+
log.Init(flag.ServerLogLevel)
3434

3535
controller.InitCodeRunner()
3636
engine := web.NewRouter(flag.ServerAccessToken)

components/execd/pkg/log/log.go

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,83 +15,66 @@
1515
package log
1616

1717
import (
18-
"fmt"
1918
"os"
2019

21-
"go.uber.org/zap"
22-
"go.uber.org/zap/zapcore"
20+
slogger "github.com/alibaba/opensandbox/internal/logger"
2321
)
2422

2523
const logFileEnvKey = "EXECD_LOG_FILE"
2624

27-
var (
28-
atomicLevel = zap.NewAtomicLevelAt(zap.InfoLevel)
29-
base *zap.Logger
30-
sugar *zap.SugaredLogger
31-
)
32-
33-
func init() {
34-
cfg := zap.NewProductionConfig()
35-
cfg.Level = atomicLevel
36-
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
37-
cfg.EncoderConfig.CallerKey = ""
38-
cfg.DisableCaller = true
39-
cfg.DisableStacktrace = true
40-
cfg.EncoderConfig.StacktraceKey = ""
41-
42-
logFile := os.Getenv(logFileEnvKey)
43-
if logFile != "" {
44-
cfg.OutputPaths = []string{logFile}
45-
cfg.ErrorOutputPaths = []string{logFile}
46-
} else {
47-
// outputs log to stdout pipe by default
48-
cfg.OutputPaths = []string{"stdout"}
49-
cfg.ErrorOutputPaths = []string{"stdout"}
50-
}
51-
52-
logger, err := cfg.Build()
53-
if err != nil {
54-
panic(fmt.Sprintf("failed to init logger: %v", err))
55-
}
56-
base = logger
57-
sugar = base.Sugar()
58-
}
25+
var current slogger.Logger
5926

60-
// SetLevel maps legacy Beego log levels to zap levels.
61-
// 0/1/2 => Fatal, 3 => Error, 4 => Warn, 5/6 => Info, 7+ => Debug.
62-
func SetLevel(level int) {
63-
atomicLevel.SetLevel(mapLevel(level))
27+
// Init constructs the singleton logger. Call once during startup.
28+
// Legacy levels: 0/1/2=fatal, 3=error, 4=warn, 5/6=info, 7+=debug.
29+
func Init(level int) {
30+
current = newLogger(mapLevel(level))
6431
}
6532

66-
func mapLevel(level int) zapcore.Level {
33+
func mapLevel(level int) string {
6734
switch {
6835
case level <= 2:
69-
return zapcore.FatalLevel
36+
return "fatal"
7037
case level == 3:
71-
return zapcore.ErrorLevel
38+
return "error"
7239
case level == 4:
73-
return zapcore.WarnLevel
40+
return "warn"
7441
case level == 5 || level == 6:
75-
return zapcore.InfoLevel
42+
return "info"
7643
default:
77-
return zapcore.DebugLevel
44+
return "debug"
7845
}
7946
}
8047

81-
func Sync() {
82-
_ = base.Sync()
48+
func newLogger(level string) slogger.Logger {
49+
cfg := slogger.Config{
50+
Level: level,
51+
}
52+
if logFile := os.Getenv(logFileEnvKey); logFile != "" {
53+
cfg.OutputPaths = []string{logFile}
54+
cfg.ErrorOutputPaths = cfg.OutputPaths
55+
}
56+
return slogger.MustNew(cfg)
57+
}
58+
59+
func getLogger() slogger.Logger {
60+
if current != nil {
61+
return current
62+
}
63+
l := newLogger("info")
64+
current = l
65+
return l
8366
}
8467

8568
func Debug(format string, args ...any) {
86-
sugar.Debugf(format, args...)
69+
getLogger().Debugf(format, args...)
8770
}
8871

8972
func Info(format string, args ...any) {
90-
sugar.Infof(format, args...)
73+
getLogger().Infof(format, args...)
9174
}
9275

9376
func Warn(format string, args ...any) {
94-
sugar.Warnf(format, args...)
77+
getLogger().Warnf(format, args...)
9578
}
9679

9780
// Warning is an alias to Warn for compatibility.
@@ -100,5 +83,5 @@ func Warning(format string, args ...any) {
10083
}
10184

10285
func Error(format string, args ...any) {
103-
sugar.Errorf(format, args...)
86+
getLogger().Errorf(format, args...)
10487
}

scripts/java-e2e.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ set -euxo pipefail
1717

1818
TAG=${TAG:-latest}
1919

20-
# build execd image locally
21-
cd components/execd && docker build -t opensandbox/execd:local .
22-
cd ../..
20+
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
21+
22+
# build execd image locally (context must include internal/)
23+
docker build -f components/execd/Dockerfile -t opensandbox/execd:local "${REPO_ROOT}"
2324

2425
# prepare required images from registry
2526
docker pull opensandbox/code-interpreter:${TAG}

scripts/javascript-e2e.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ set -euxo pipefail
1717

1818
TAG=${TAG:-latest}
1919

20-
# build execd image locally
21-
cd components/execd && docker build -t opensandbox/execd:local .
22-
cd ../..
20+
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
21+
22+
# build execd image locally (context must include internal/)
23+
docker build -f components/execd/Dockerfile -t opensandbox/execd:local "${REPO_ROOT}"
2324

2425
# prepare required images from registry
2526
docker pull opensandbox/code-interpreter:${TAG}

scripts/python-e2e.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ set -euxo pipefail
2121

2222
TAG=${TAG:-latest}
2323

24-
# build execd image locally
25-
cd components/execd && docker build -t opensandbox/execd:local .
26-
cd ../..
24+
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
25+
26+
# build execd image locally (context must include internal/)
27+
docker build -f components/execd/Dockerfile -t opensandbox/execd:local "${REPO_ROOT}"
2728

2829
# prepare required images from registry
2930
docker pull opensandbox/code-interpreter:${TAG}

0 commit comments

Comments
 (0)