From 513c9d40e412add760655f98e261cac2a3019bb4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=82=85=E4=B8=80=E6=B4=AA?= <fuyihong.1@bytedance.com>
Date: Thu, 16 Nov 2023 11:42:44 +0800
Subject: [PATCH] feat: add log level const

---
 engine.go                  |  7 +++----
 eventbus/mysql/eventbus.go |  9 ++++-----
 logger/logger.go           | 26 ++++++++++++++++++++++++++
 logger/stdr/stdr.go        | 28 ++++++++++++++++++++++++++++
 4 files changed, 61 insertions(+), 9 deletions(-)
 create mode 100644 logger/logger.go
 create mode 100644 logger/stdr/stdr.go

diff --git a/engine.go b/engine.go
index b221dc1..66cb708 100644
--- a/engine.go
+++ b/engine.go
@@ -20,22 +20,21 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
-	stdlog "log"
-	"os"
 	"reflect"
 	"sort"
 	"strings"
 
 	"github.com/go-logr/logr"
-	"github.com/go-logr/stdr"
 	"github.com/rs/xid"
+
+	"github.com/bytedance/dddfirework/logger/stdr"
 )
 
 var ErrBreak = fmt.Errorf("break process") // 中断流程,不返回错误
 var ErrEntityNotFound = fmt.Errorf("entity not found")
 var ErrEntityRepeated = fmt.Errorf("entity already added")
 
-var defaultLogger = stdr.New(stdlog.New(os.Stderr, "", stdlog.LstdFlags|stdlog.Lshortfile)).WithName("ddd_engine")
+var defaultLogger = stdr.NewStdr("ddd_engine")
 
 type ILock interface {
 	Lock(ctx context.Context, key string) (keyLock interface{}, err error)
diff --git a/eventbus/mysql/eventbus.go b/eventbus/mysql/eventbus.go
index 89050d8..3920ba2 100644
--- a/eventbus/mysql/eventbus.go
+++ b/eventbus/mysql/eventbus.go
@@ -19,19 +19,18 @@ import (
 	"context"
 	"errors"
 	"fmt"
-	stdlog "log"
-	"os"
 	"sync"
 	"time"
 	"unicode/utf8"
 
 	"github.com/go-logr/logr"
-	"github.com/go-logr/stdr"
 	"github.com/robfig/cron"
 	"gorm.io/gorm"
 	"gorm.io/gorm/clause"
 
 	"github.com/bytedance/dddfirework"
+	"github.com/bytedance/dddfirework/logger"
+	"github.com/bytedance/dddfirework/logger/stdr"
 )
 
 const retryInterval = time.Second * 3
@@ -52,7 +51,7 @@ var ErrInvalidDB = fmt.Errorf("invalid db")
 var ErrNoTransaction = fmt.Errorf("no transaction")
 var ErrServiceNotCreate = fmt.Errorf("service not create")
 
-var defaultLogger = stdr.New(stdlog.New(os.Stderr, "", stdlog.LstdFlags|stdlog.Lshortfile)).WithName("mysql_eventbus")
+var defaultLogger = stdr.NewStdr("mysql_eventbus")
 
 type IRetryStrategy interface {
 	// Next 获取下一次重试的策略,返回 nil 表示不再重试
@@ -440,7 +439,7 @@ func (e *EventBus) dispatchEvents(ctx context.Context, eventPOs []*EventPO) (suc
 }
 
 func (e *EventBus) handleEvents() error {
-	e.logger.V(4).Info("handle events")
+	e.logger.V(logger.LevelDebug).Info("handle events")
 
 	return e.db.Transaction(func(tx *gorm.DB) error {
 		ctx := context.Background()
diff --git a/logger/logger.go b/logger/logger.go
new file mode 100644
index 0000000..4ca7e27
--- /dev/null
+++ b/logger/logger.go
@@ -0,0 +1,26 @@
+//
+// Copyright 2023 Bytedance Ltd. and/or its affiliates
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package logger
+
+// LogLevel 日志级别,值越大级别越低
+const (
+	LevelWarn         = iota // warn日志级别
+	LevelInfo                // info日志级别
+	LevelInfoVerbose         // info详细日志级别
+	LevelInfoExtended        // info扩展信息日志级别
+	LevelDebug               // debug日志级别
+	LevelTrace               // trace日志级别
+)
diff --git a/logger/stdr/stdr.go b/logger/stdr/stdr.go
new file mode 100644
index 0000000..9a0e948
--- /dev/null
+++ b/logger/stdr/stdr.go
@@ -0,0 +1,28 @@
+//
+// Copyright 2023 Bytedance Ltd. and/or its affiliates
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package stdr
+
+import (
+	stdlog "log"
+	"os"
+
+	"github.com/go-logr/logr"
+	"github.com/go-logr/stdr"
+)
+
+func NewStdr(name string) logr.Logger {
+	return stdr.New(stdlog.New(os.Stderr, "", stdlog.LstdFlags|stdlog.Lshortfile)).WithName(name)
+}
-- 
GitLab