From 0ae96c2caa31901c21653c978e7f0cef37fd9609 Mon Sep 17 00:00:00 2001 From: gwind Date: Thu, 23 Apr 2026 11:51:23 +0800 Subject: [PATCH] refactor: replace external logrus shim with internal logger package --- conn_test.go | 2 +- ...26\350\257\221\346\214\207\345\215\227.md" | 81 ++++++++++++ example/tcp/client/client.go | 2 +- example/tcp/server/server.go | 2 +- example/udp/client/client.go | 2 +- example/udp/server/server.go | 2 +- go.mod | 3 + link/handler.go | 2 +- link/link.go | 2 +- link/request.go | 2 +- logger/logger.go | 115 ++++++++++++++++++ proto/udp/udp.go | 2 +- proto/udp/udp_test.go | 2 +- scripts/build_multi_platform.sh | 18 +++ session/manager.go | 2 +- test/common.go | 2 +- test/session_test.go | 2 +- test/tunnel_test.go | 2 +- tunnel/channel/tcp.go | 2 +- tunnel/channel/udp.go | 2 +- tunnel/channel/util.go | 2 +- tunnel/listen_pool.go | 2 +- tunnel/manager.go | 2 +- tunnel/tunnel.go | 2 +- tunnel/tunnel_pool.go | 2 +- 25 files changed, 238 insertions(+), 21 deletions(-) create mode 100644 "docs/zh_CN/Go\347\216\257\345\242\203\344\270\216\345\244\232\345\271\263\345\217\260\347\274\226\350\257\221\346\214\207\345\215\227.md" create mode 100644 go.mod create mode 100644 logger/logger.go create mode 100755 scripts/build_multi_platform.sh diff --git a/conn_test.go b/conn_test.go index 90ec1b3..083fe7d 100644 --- a/conn_test.go +++ b/conn_test.go @@ -9,7 +9,7 @@ import ( "time" "github.com/ooclab/es/ecrypt" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" ) func testEcho(conn Conn) error { diff --git "a/docs/zh_CN/Go\347\216\257\345\242\203\344\270\216\345\244\232\345\271\263\345\217\260\347\274\226\350\257\221\346\214\207\345\215\227.md" "b/docs/zh_CN/Go\347\216\257\345\242\203\344\270\216\345\244\232\345\271\263\345\217\260\347\274\226\350\257\221\346\214\207\345\215\227.md" new file mode 100644 index 0000000..c9ffbbb --- /dev/null +++ "b/docs/zh_CN/Go\347\216\257\345\242\203\344\270\216\345\244\232\345\271\263\345\217\260\347\274\226\350\257\221\346\214\207\345\215\227.md" @@ -0,0 +1,81 @@ +# Go 环境与多平台编译指南 + +本文档用于在本地用较新的 Go 版本重新构建 `ooclab/es`,并给出与 `ooclab/otunnel` 联调时的建议流程。 + +## 1. 安装 Go(建议) + +建议使用 **Go 1.25+**(本仓库已使用模块化管理)。 + +### macOS (Homebrew) + +```bash +brew update +brew install go +``` + +### Ubuntu/Debian + +```bash +sudo apt-get update +sudo apt-get install -y golang-go +``` + +### 验证 + +```bash +go version +go env GOPATH GOMOD +``` + +## 2. 获取并构建 es + +```bash +git clone https://github.com/ooclab/es.git +cd es +go mod tidy +go test ./... +``` + +> 当前仓库已内置 `github.com/ooclab/es/logger` 日志适配包,不依赖外部日志模块。 + +## 3. 多平台编译(es) + +仓库提供了脚本: + +```bash +./scripts/build_multi_platform.sh +``` + +脚本会对以下目标执行 `go build ./...`: + +- linux/amd64 +- linux/arm64 +- darwin/amd64 +- darwin/arm64 +- windows/amd64 + +## 4. 与 otunnel 联调(本地依赖 es) + +当 `otunnel` 依赖本地修改版 `es` 时,在 `otunnel/go.mod` 中增加: + +```go +replace github.com/ooclab/es => /path/to/your/es +``` + +然后在 `otunnel` 仓库执行: + +```bash +go mod tidy +go test ./... +GOOS=linux GOARCH=amd64 go build ./... +GOOS=linux GOARCH=arm64 go build ./... +GOOS=darwin GOARCH=amd64 go build ./... +GOOS=darwin GOARCH=arm64 go build ./... +GOOS=windows GOARCH=amd64 go build ./... +``` + +## 5. 依赖升级建议 + +- 先在网络可访问环境中执行:`go get -u ./... && go mod tidy` +- 对核心依赖逐项验证行为兼容性(尤其是日志、网络与加解密链路) +- 升级后执行全量测试与多平台构建再发布 diff --git a/example/tcp/client/client.go b/example/tcp/client/client.go index 7c64104..3570e74 100644 --- a/example/tcp/client/client.go +++ b/example/tcp/client/client.go @@ -9,7 +9,7 @@ import ( "strings" "time" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" "github.com/ooclab/es" "github.com/ooclab/es/link" diff --git a/example/tcp/server/server.go b/example/tcp/server/server.go index 7622d7a..022761c 100644 --- a/example/tcp/server/server.go +++ b/example/tcp/server/server.go @@ -7,7 +7,7 @@ import ( "github.com/ooclab/es" "github.com/ooclab/es/link" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" ) func main() { diff --git a/example/udp/client/client.go b/example/udp/client/client.go index b91d56e..5869014 100644 --- a/example/udp/client/client.go +++ b/example/udp/client/client.go @@ -9,8 +9,8 @@ import ( "os" "time" + logrus "github.com/ooclab/es/logger" "github.com/ooclab/es/proto/udp" - "github.com/sirupsen/logrus" ) func init() { diff --git a/example/udp/server/server.go b/example/udp/server/server.go index 22049ac..ff4f84f 100644 --- a/example/udp/server/server.go +++ b/example/udp/server/server.go @@ -7,8 +7,8 @@ import ( "net" "os" + logrus "github.com/ooclab/es/logger" "github.com/ooclab/es/proto/udp" - "github.com/sirupsen/logrus" ) func init() { diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..20322e5 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/ooclab/es + +go 1.25.0 diff --git a/link/handler.go b/link/handler.go index 0a69e37..9b05317 100644 --- a/link/handler.go +++ b/link/handler.go @@ -3,9 +3,9 @@ package link import ( "encoding/json" + logrus "github.com/ooclab/es/logger" "github.com/ooclab/es/session" "github.com/ooclab/es/tunnel" - "github.com/sirupsen/logrus" ) type requestHandler struct { diff --git a/link/link.go b/link/link.go index af04f43..a56069c 100644 --- a/link/link.go +++ b/link/link.go @@ -9,9 +9,9 @@ import ( "time" "github.com/ooclab/es" + logrus "github.com/ooclab/es/logger" "github.com/ooclab/es/session" "github.com/ooclab/es/tunnel" - "github.com/sirupsen/logrus" ) // Define error diff --git a/link/request.go b/link/request.go index da4b9d0..c48e1bc 100644 --- a/link/request.go +++ b/link/request.go @@ -4,9 +4,9 @@ import ( "encoding/json" "errors" + logrus "github.com/ooclab/es/logger" "github.com/ooclab/es/session" "github.com/ooclab/es/tunnel" - "github.com/sirupsen/logrus" ) func defaultOpenTunnel(sessionManager *session.Manager, tunnelManager *tunnel.Manager) OpenTunnelFunc { diff --git a/logger/logger.go b/logger/logger.go new file mode 100644 index 0000000..762a57c --- /dev/null +++ b/logger/logger.go @@ -0,0 +1,115 @@ +package logger + +import ( + "fmt" + "log" + "os" + "sort" + "strings" +) + +// Fields is a set of key/value pairs attached to a log message. +type Fields map[string]interface{} + +// Entry is a logger with bound fields. +type Entry struct { + fields Fields +} + +var std = log.New(os.Stderr, "", log.LstdFlags) + +// Level keeps compatibility with existing SetLevel call sites. +type Level uint32 + +const ( + DebugLevel Level = iota +) + +func SetLevel(_ Level) {} + +func Debug(args ...interface{}) { std.Print(args...) } +func Debugf(format string, args ...interface{}) { std.Printf(format, args...) } +func Infof(format string, args ...interface{}) { std.Printf(format, args...) } +func Warn(args ...interface{}) { std.Print(args...) } +func Warnf(format string, args ...interface{}) { std.Printf(format, args...) } +func Error(args ...interface{}) { std.Print(args...) } +func Errorf(format string, args ...interface{}) { std.Printf(format, args...) } +func Fatalln(args ...interface{}) { std.Fatalln(args...) } + +func WithField(key string, value interface{}) *Entry { + return &Entry{fields: Fields{key: value}} +} + +func WithFields(fields Fields) *Entry { + cp := make(Fields, len(fields)) + for k, v := range fields { + cp[k] = v + } + return &Entry{fields: cp} +} + +func (e *Entry) WithField(key string, value interface{}) *Entry { + fields := e.copyFields() + fields[key] = value + return &Entry{fields: fields} +} + +func (e *Entry) WithFields(extra Fields) *Entry { + fields := e.copyFields() + for k, v := range extra { + fields[k] = v + } + return &Entry{fields: fields} +} + +func (e *Entry) Debug(args ...interface{}) { + std.Print(e.prefix() + fmt.Sprint(args...)) +} + +func (e *Entry) Debugf(format string, args ...interface{}) { + std.Print(e.prefix() + fmt.Sprintf(format, args...)) +} + +func (e *Entry) Warn(args ...interface{}) { + std.Print(e.prefix() + fmt.Sprint(args...)) +} + +func (e *Entry) Error(args ...interface{}) { + std.Print(e.prefix() + fmt.Sprint(args...)) +} + +func (e *Entry) Errorf(format string, args ...interface{}) { + std.Print(e.prefix() + fmt.Sprintf(format, args...)) +} + +func (e *Entry) Warnf(format string, args ...interface{}) { + std.Print(e.prefix() + fmt.Sprintf(format, args...)) +} + +func (e *Entry) Infof(format string, args ...interface{}) { + std.Print(e.prefix() + fmt.Sprintf(format, args...)) +} + +func (e *Entry) copyFields() Fields { + cp := make(Fields, len(e.fields)) + for k, v := range e.fields { + cp[k] = v + } + return cp +} + +func (e *Entry) prefix() string { + if len(e.fields) == 0 { + return "" + } + keys := make([]string, 0, len(e.fields)) + for k := range e.fields { + keys = append(keys, k) + } + sort.Strings(keys) + parts := make([]string, 0, len(keys)) + for _, k := range keys { + parts = append(parts, fmt.Sprintf("%s=%v", k, e.fields[k])) + } + return "[" + strings.Join(parts, " ") + "] " +} diff --git a/proto/udp/udp.go b/proto/udp/udp.go index a97042c..c93e23b 100644 --- a/proto/udp/udp.go +++ b/proto/udp/udp.go @@ -11,7 +11,7 @@ import ( "sync" "time" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" ) const ( diff --git a/proto/udp/udp_test.go b/proto/udp/udp_test.go index 2b2b7f3..6bfde35 100644 --- a/proto/udp/udp_test.go +++ b/proto/udp/udp_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" ) func init() { diff --git a/scripts/build_multi_platform.sh b/scripts/build_multi_platform.sh new file mode 100755 index 0000000..c04db3a --- /dev/null +++ b/scripts/build_multi_platform.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -euo pipefail + +TARGETS=( + "linux amd64" + "linux arm64" + "darwin amd64" + "darwin arm64" + "windows amd64" +) + +for target in "${TARGETS[@]}"; do + IFS=' ' read -r GOOS GOARCH <<<"$target" + echo "==> building packages for ${GOOS}/${GOARCH}" + CGO_ENABLED=0 GOOS="$GOOS" GOARCH="$GOARCH" go build ./... +done + +echo "all targets built successfully" diff --git a/session/manager.go b/session/manager.go index df27e60..0bdedd4 100644 --- a/session/manager.go +++ b/session/manager.go @@ -4,7 +4,7 @@ import ( "errors" "github.com/ooclab/es" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" ) type Manager struct { diff --git a/test/common.go b/test/common.go index 60eee5a..6a706e2 100644 --- a/test/common.go +++ b/test/common.go @@ -9,7 +9,7 @@ import ( "github.com/ooclab/es" "github.com/ooclab/es/link" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" ) // For test diff --git a/test/session_test.go b/test/session_test.go index 0ac9e9f..49f15a9 100644 --- a/test/session_test.go +++ b/test/session_test.go @@ -4,7 +4,7 @@ import ( "crypto/rand" "testing" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" "github.com/ooclab/es/session" ) diff --git a/test/tunnel_test.go b/test/tunnel_test.go index 210daf1..1d9cb1c 100644 --- a/test/tunnel_test.go +++ b/test/tunnel_test.go @@ -3,7 +3,7 @@ package test import ( "testing" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" ) func init() { diff --git a/tunnel/channel/tcp.go b/tunnel/channel/tcp.go index 2582c06..b45c91b 100644 --- a/tunnel/channel/tcp.go +++ b/tunnel/channel/tcp.go @@ -9,8 +9,8 @@ import ( "sync/atomic" "github.com/ooclab/es" + logrus "github.com/ooclab/es/logger" "github.com/ooclab/es/util" - "github.com/sirupsen/logrus" tcommon "github.com/ooclab/es/tunnel/common" ) diff --git a/tunnel/channel/udp.go b/tunnel/channel/udp.go index 2308cbb..ccd0778 100644 --- a/tunnel/channel/udp.go +++ b/tunnel/channel/udp.go @@ -10,7 +10,7 @@ import ( "time" "github.com/ooclab/es" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" tcommon "github.com/ooclab/es/tunnel/common" ) diff --git a/tunnel/channel/util.go b/tunnel/channel/util.go index d3aa646..3ed417a 100644 --- a/tunnel/channel/util.go +++ b/tunnel/channel/util.go @@ -3,7 +3,7 @@ package channel import ( "net" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" ) func closeConn(conn net.Conn) { diff --git a/tunnel/listen_pool.go b/tunnel/listen_pool.go index 283e17a..1ea2790 100644 --- a/tunnel/listen_pool.go +++ b/tunnel/listen_pool.go @@ -6,7 +6,7 @@ import ( "net" "sync" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" ) type listenTarget struct { diff --git a/tunnel/manager.go b/tunnel/manager.go index 0e8c372..8f8b272 100644 --- a/tunnel/manager.go +++ b/tunnel/manager.go @@ -3,7 +3,7 @@ package tunnel import ( "errors" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" "github.com/ooclab/es/session" tcommon "github.com/ooclab/es/tunnel/common" diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 9c8ca76..fc0aaf6 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -7,10 +7,10 @@ import ( "strings" "github.com/ooclab/es" + logrus "github.com/ooclab/es/logger" "github.com/ooclab/es/tunnel/channel" tcommon "github.com/ooclab/es/tunnel/common" "github.com/ooclab/es/util" - "github.com/sirupsen/logrus" ) type TunnelConfig struct { diff --git a/tunnel/tunnel_pool.go b/tunnel/tunnel_pool.go index 8a39805..7608c54 100644 --- a/tunnel/tunnel_pool.go +++ b/tunnel/tunnel_pool.go @@ -4,7 +4,7 @@ import ( "errors" "sync" - "github.com/sirupsen/logrus" + logrus "github.com/ooclab/es/logger" ) type Pool struct {