test-errcode
wangchuxiao 2 years ago
parent a348af74fa
commit d73ab781fe

@ -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())

@ -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

@ -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
}

@ -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(),
"",

Loading…
Cancel
Save