diff --git a/cmd/api/main.go b/cmd/api/main.go index 96e714da2..b6644c05e 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -43,6 +43,7 @@ func run(port int) error { } fmt.Println("start api server, address: ", address, ", OpenIM version: ", config.Version) log2.Info(context.Background(), "start server success", "address", address, "version", config.Version) + log.Info("s", "start server") err = router.Run(address) if err != nil { log.Error("", "api run failed ", address, err.Error()) diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index 3f2057c40..72a490ee2 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -3,7 +3,6 @@ package cmd import ( "OpenIM/pkg/common/config" "OpenIM/pkg/common/constant" - log "OpenIM/pkg/common/logger" "github.com/spf13/cobra" ) @@ -20,11 +19,7 @@ func NewRootCmd() (rootCmd *RootCmd) { Short: "Start the server", Long: `Start the server`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - err := rootCmd.getConfFromCmdAndInit(cmd) - if err != nil { - return err - } - return log.InitFromConfig("newlog") + return rootCmd.getConfFromCmdAndInit(cmd) }, } rootCmd.Command = c diff --git a/pkg/common/log/logger.go b/pkg/common/log/logger.go new file mode 100644 index 000000000..b00ddbb5e --- /dev/null +++ b/pkg/common/log/logger.go @@ -0,0 +1,17 @@ +package log + +import "context" + +type Logger interface { + Debug(ctx context.Context, msg string, keysAndValues ...interface{}) + Info(ctx context.Context, msg string, keysAndValues ...interface{}) + Warn(ctx context.Context, msg string, err error, keysAndValues ...interface{}) + Error(ctx context.Context, msg string, err error, keysAndValues ...interface{}) + WithValues(keysAndValues ...interface{}) LogrusLogger + WithName(name string) LogrusLogger + WithCallDepth(depth int) LogrusLogger + + WithItemSampler() LogrusLogger + // WithoutSampler returns the original logger without sampling + WithoutSampler() LogrusLogger +} diff --git a/pkg/common/log/logrus.go b/pkg/common/log/logrus.go index 694394a1f..1d416fe74 100644 --- a/pkg/common/log/logrus.go +++ b/pkg/common/log/logrus.go @@ -16,10 +16,10 @@ import ( "github.com/sirupsen/logrus" ) -var logger *Logger -var ctxLogger *Logger +var logger *LogrusLogger +var ctxLogger *LogrusLogger -type Logger struct { +type LogrusLogger struct { *logrus.Logger Pid int Type string @@ -35,7 +35,7 @@ func NewPrivateLog(moduleName string) { ctxLogger = ctxLoggerInit(moduleName) } -func ctxLoggerInit(moduleName string) *Logger { +func ctxLoggerInit(moduleName string) *LogrusLogger { var ctxLogger = logrus.New() ctxLogger.SetLevel(logrus.Level(config.Config.Log.RemainLogLevel)) src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend) @@ -55,14 +55,14 @@ func ctxLoggerInit(moduleName string) *Logger { //Log file segmentation hook hook := NewLfsHook(time.Duration(config.Config.Log.RotationTime)*time.Hour, config.Config.Log.RemainRotationCount, moduleName) ctxLogger.AddHook(hook) - return &Logger{ + return &LogrusLogger{ ctxLogger, os.Getpid(), "ctxLogger", } } -func loggerInit(moduleName string) *Logger { +func loggerInit(moduleName string) *LogrusLogger { var logger = logrus.New() //All logs will be printed logger.SetLevel(logrus.Level(config.Config.Log.RemainLogLevel)) @@ -93,7 +93,7 @@ func loggerInit(moduleName string) *Logger { //Log file segmentation hook hook := NewLfsHook(time.Duration(config.Config.Log.RotationTime)*time.Hour, config.Config.Log.RemainRotationCount, moduleName) logger.AddHook(hook) - return &Logger{ + return &LogrusLogger{ logger, os.Getpid(), "",