mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-26 19:46:57 +08:00
Merge remote-tracking branch 'origin/errcode' into errcode
This commit is contained in:
commit
efa8d9297a
@ -5,16 +5,41 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
msgUtilsCmd := cmd.NewMsgUtilsCmd()
|
||||
msgUtilsCmd.AddSuperGroupIDFlag()
|
||||
msgUtilsCmd.AddUserIDFlag()
|
||||
msgUtilsCmd := cmd.NewMsgUtilsCmd("openIMCmdUtils", "openIM cmd utils", nil)
|
||||
getCmd := cmd.NewGetCmd()
|
||||
fixCmd := cmd.NewFixCmd()
|
||||
clearCmd := cmd.NewClearCmd()
|
||||
seqCmd := cmd.NewSeqCmd()
|
||||
msgCmd := cmd.NewMsgCmd()
|
||||
cmd.GetCmd.AddCommand(seqCmd.Command, msgCmd.Command)
|
||||
cmd.FixCmd.AddCommand(seqCmd.Command)
|
||||
cmd.GetCmd.AddCommand(msgCmd.Command)
|
||||
msgUtilsCmd.AddCommand(cmd.GetCmd, cmd.FixCmd, cmd.ClearCmd)
|
||||
getCmd.AddCommand(seqCmd.GetSeqCmd(), msgCmd.GetMsgCmd())
|
||||
getCmd.AddSuperGroupIDFlag()
|
||||
getCmd.AddUserIDFlag()
|
||||
getCmd.AddBeginSeqFlag()
|
||||
getCmd.AddLimitFlag()
|
||||
// openIM get seq --userID=xxx
|
||||
// openIM get seq --superGroupID=xxx
|
||||
// openIM get msg --userID=xxx --beginSeq=100 --limit=10
|
||||
// openIM get msg --superGroupID=xxx --beginSeq=100 --limit=10
|
||||
|
||||
fixCmd.AddCommand(seqCmd.FixSeqCmd())
|
||||
fixCmd.AddSuperGroupIDFlag()
|
||||
fixCmd.AddUserIDFlag()
|
||||
fixCmd.AddFixAllFlag()
|
||||
// openIM fix seq --userID=xxx
|
||||
// openIM fix seq --superGroupID=xxx
|
||||
// openIM fix seq --fixAll
|
||||
|
||||
clearCmd.AddCommand(msgCmd.ClearMsgCmd())
|
||||
clearCmd.AddSuperGroupIDFlag()
|
||||
clearCmd.AddUserIDFlag()
|
||||
clearCmd.AddClearAllFlag()
|
||||
clearCmd.AddBeginSeqFlag()
|
||||
clearCmd.AddLimitFlag()
|
||||
// openIM clear msg --userID=xxx --beginSeq=100 --limit=10
|
||||
// openIM clear msg --superGroupID=xxx --beginSeq=100 --limit=10
|
||||
// openIM clear msg --clearAll
|
||||
msgUtilsCmd.AddCommand(&getCmd.Command, &fixCmd.Command, &clearCmd.Command)
|
||||
if err := msgUtilsCmd.Execute(); err != nil {
|
||||
panic(err.Error())
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ mysql:
|
||||
dbMaxOpenConns: 100
|
||||
dbMaxIdleConns: 10
|
||||
dbMaxLifeTime: 5
|
||||
logLevel: 4 #1=slient 2=error 3=warn 4=info
|
||||
logLevel: 6 #1=slient 2=error 3=warn 4=info
|
||||
slowThreshold: 500
|
||||
|
||||
mongo:
|
||||
@ -160,8 +160,9 @@ log:
|
||||
rotationTime: 24
|
||||
remainRotationCount: 2 #日志数量
|
||||
#日志级别 6表示全都打印,测试阶段建议设置为6
|
||||
remainLogLevel: -1
|
||||
stderr: true
|
||||
remainLogLevel: 6
|
||||
isStdout: true
|
||||
isJson: true
|
||||
withStack: false
|
||||
elasticSearchSwitch: false
|
||||
elasticSearchAddr: [ 127.0.0.1:9201 ]
|
||||
|
@ -1,157 +1,170 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/internal/tools"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type MsgUtilsCmd struct {
|
||||
*RootCmd
|
||||
userID string
|
||||
|
||||
superGroupID string
|
||||
|
||||
clearAll bool
|
||||
|
||||
fixAll bool
|
||||
}
|
||||
|
||||
func NewMsgUtilsCmd() MsgUtilsCmd {
|
||||
return MsgUtilsCmd{RootCmd: NewRootCmd("msgUtils")}
|
||||
cobra.Command
|
||||
msgTool *tools.MsgTool
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) AddUserIDFlag() {
|
||||
m.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID")
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) GetUserIDFlag() string {
|
||||
return m.userID
|
||||
func (m *MsgUtilsCmd) getUserIDFlag(cmdLines *cobra.Command) string {
|
||||
userID, _ := cmdLines.Flags().GetString("userID")
|
||||
return userID
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) AddFixAllFlag() {
|
||||
m.Command.PersistentFlags().BoolP("fixAll", "c", false, "openIM fix all seqs")
|
||||
m.Command.PersistentFlags().BoolP("fixAll", "f", false, "openIM fix all seqs")
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) GetFixAllFlag() bool {
|
||||
return m.fixAll
|
||||
func (m *MsgUtilsCmd) getFixAllFlag(cmdLines *cobra.Command) bool {
|
||||
fixAll, _ := cmdLines.Flags().GetBool("fixAll")
|
||||
return fixAll
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) AddClearAllFlag() {
|
||||
m.Command.PersistentFlags().BoolP("clearAll", "c", false, "openIM clear all seqs")
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) getClearAllFlag(cmdLines *cobra.Command) bool {
|
||||
clearAll, _ := cmdLines.Flags().GetBool("clearAll")
|
||||
return clearAll
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) AddSuperGroupIDFlag() {
|
||||
m.Command.PersistentFlags().StringP("super-groupID", "u", "", "openIM superGroupID")
|
||||
m.Command.PersistentFlags().StringP("superGroupID", "g", "", "openIM superGroupID")
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) GetSuperGroupIDFlag() string {
|
||||
return m.superGroupID
|
||||
func (m *MsgUtilsCmd) getSuperGroupIDFlag(cmdLines *cobra.Command) string {
|
||||
superGroupID, _ := cmdLines.Flags().GetString("superGroupID")
|
||||
return superGroupID
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) AddClearAllFlag() bool {
|
||||
return m.clearAll
|
||||
func (m *MsgUtilsCmd) AddBeginSeqFlag() {
|
||||
m.Command.PersistentFlags().Int64P("beginSeq", "b", 0, "openIM beginSeq")
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) GetClearAllFlag() bool {
|
||||
return m.clearAll
|
||||
func (m *MsgUtilsCmd) getBeginSeqFlag(cmdLines *cobra.Command) int64 {
|
||||
beginSeq, _ := cmdLines.Flags().GetInt64("beginSeq")
|
||||
return beginSeq
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) AddLimitFlag() {
|
||||
m.Command.PersistentFlags().Int64P("limit", "l", 0, "openIM limit")
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) getLimitFlag(cmdLines *cobra.Command) int64 {
|
||||
limit, _ := cmdLines.Flags().GetInt64("limit")
|
||||
return limit
|
||||
}
|
||||
|
||||
func (m *MsgUtilsCmd) Execute() error {
|
||||
return m.Command.Execute()
|
||||
}
|
||||
|
||||
func NewMsgUtilsCmd(use, short string, args cobra.PositionalArgs) *MsgUtilsCmd {
|
||||
return &MsgUtilsCmd{
|
||||
Command: cobra.Command{
|
||||
Use: use,
|
||||
Short: short,
|
||||
Args: args,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type GetCmd struct {
|
||||
*MsgUtilsCmd
|
||||
}
|
||||
|
||||
func NewGetCmd() *GetCmd {
|
||||
return &GetCmd{
|
||||
NewMsgUtilsCmd("get [resource]", "get action", cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)),
|
||||
}
|
||||
}
|
||||
|
||||
type FixCmd struct {
|
||||
*MsgUtilsCmd
|
||||
}
|
||||
|
||||
func NewFixCmd() *FixCmd {
|
||||
return &FixCmd{
|
||||
NewMsgUtilsCmd("fix [resource]", "fix action", cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)),
|
||||
}
|
||||
}
|
||||
|
||||
type ClearCmd struct {
|
||||
*MsgUtilsCmd
|
||||
}
|
||||
|
||||
func NewClearCmd() *ClearCmd {
|
||||
return &ClearCmd{
|
||||
NewMsgUtilsCmd("clear [resource]", "clear action", cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)),
|
||||
}
|
||||
}
|
||||
|
||||
type SeqCmd struct {
|
||||
Command *cobra.Command
|
||||
*MsgUtilsCmd
|
||||
}
|
||||
|
||||
func (SeqCmd) RunCommand(cmdLines *cobra.Command, args []string) error {
|
||||
msgTool, err := tools.InitMsgTool()
|
||||
if err != nil {
|
||||
return err
|
||||
func NewSeqCmd() *SeqCmd {
|
||||
seqCmd := &SeqCmd{
|
||||
NewMsgUtilsCmd("seq", "seq", nil),
|
||||
}
|
||||
userID, _ := cmdLines.Flags().GetString("userID")
|
||||
superGroupID, _ := cmdLines.Flags().GetString("superGroupID")
|
||||
fixAll, _ := cmdLines.Flags().GetBool("fixAll")
|
||||
ctx := context.Background()
|
||||
switch {
|
||||
case cmdLines.Parent() == GetCmd:
|
||||
switch {
|
||||
case userID != "":
|
||||
msgTool.ShowUserSeqs(ctx, userID)
|
||||
case superGroupID != "":
|
||||
msgTool.ShowSuperGroupSeqs(ctx, superGroupID)
|
||||
}
|
||||
case cmdLines.Parent() == FixCmd:
|
||||
switch {
|
||||
case userID != "":
|
||||
_, _, err = msgTool.GetAndFixUserSeqs(ctx, userID)
|
||||
case superGroupID != "":
|
||||
err = msgTool.FixGroupSeq(ctx, userID)
|
||||
case fixAll:
|
||||
err = msgTool.FixAllSeq(ctx)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func NewSeqCmd() SeqCmd {
|
||||
seqCmd := SeqCmd{&cobra.Command{
|
||||
Use: "seq",
|
||||
Short: "seq operation",
|
||||
}}
|
||||
seqCmd.Command.Flags().BoolP("fixAll", "c", false, "openIM fix all seqs")
|
||||
seqCmd.Command.RunE = seqCmd.RunCommand
|
||||
return seqCmd
|
||||
}
|
||||
|
||||
type MsgCmd struct {
|
||||
Command *cobra.Command
|
||||
func (s *SeqCmd) GetSeqCmd() *cobra.Command {
|
||||
s.Command.Run = func(cmdLines *cobra.Command, args []string) {
|
||||
_, err := tools.InitMsgTool()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
userID := s.getUserIDFlag(cmdLines)
|
||||
superGroupID := s.getSuperGroupIDFlag(cmdLines)
|
||||
// beginSeq := s.getBeginSeqFlag(cmdLines)
|
||||
// limit := s.getLimitFlag(cmdLines)
|
||||
if userID != "" {
|
||||
// seq, err := msgTool.s(context.Background(), userID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// println(seq)
|
||||
} else if superGroupID != "" {
|
||||
// seq, err := msgTool.GetSuperGroupSeq(context.Background(), superGroupID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// println(seq)
|
||||
}
|
||||
}
|
||||
return &s.Command
|
||||
}
|
||||
|
||||
func NewMsgCmd() MsgCmd {
|
||||
msgCmd := MsgCmd{&cobra.Command{
|
||||
Use: "msg",
|
||||
Short: "msg operation",
|
||||
}}
|
||||
msgCmd.Command.RunE = msgCmd.RunCommand
|
||||
msgCmd.Command.Flags().BoolP("clearAll", "c", false, "openIM clear all timeout msgs")
|
||||
func (s *SeqCmd) FixSeqCmd() *cobra.Command {
|
||||
return &s.Command
|
||||
}
|
||||
|
||||
type MsgCmd struct {
|
||||
*MsgUtilsCmd
|
||||
}
|
||||
|
||||
func NewMsgCmd() *MsgCmd {
|
||||
msgCmd := &MsgCmd{
|
||||
NewMsgUtilsCmd("msg", "msg", nil),
|
||||
}
|
||||
return msgCmd
|
||||
}
|
||||
|
||||
func (*MsgCmd) RunCommand(cmdLines *cobra.Command, args []string) error {
|
||||
msgTool, err := tools.InitMsgTool()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userID, _ := cmdLines.Flags().GetString("userID")
|
||||
superGroupID, _ := cmdLines.Flags().GetString("superGroupID")
|
||||
clearAll, _ := cmdLines.Flags().GetBool("clearAll")
|
||||
ctx := context.Background()
|
||||
switch {
|
||||
case cmdLines.Parent() == GetCmd:
|
||||
switch {
|
||||
case userID != "":
|
||||
msgTool.ShowUserSeqs(ctx, userID)
|
||||
case superGroupID != "":
|
||||
msgTool.ShowSuperGroupSeqs(ctx, superGroupID)
|
||||
}
|
||||
case cmdLines.Parent() == ClearCmd:
|
||||
switch {
|
||||
case userID != "":
|
||||
msgTool.ClearUsersMsg(ctx, []string{userID})
|
||||
case superGroupID != "":
|
||||
msgTool.ClearSuperGroupMsg(ctx, []string{superGroupID})
|
||||
case clearAll:
|
||||
msgTool.AllUserClearMsgAndFixSeq()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
func (m *MsgCmd) GetMsgCmd() *cobra.Command {
|
||||
return &m.Command
|
||||
}
|
||||
|
||||
var GetCmd = &cobra.Command{
|
||||
Use: "get",
|
||||
Short: "get operation",
|
||||
}
|
||||
|
||||
var FixCmd = &cobra.Command{
|
||||
Use: "fix",
|
||||
Short: "fix seq operation",
|
||||
}
|
||||
|
||||
var ClearCmd = &cobra.Command{
|
||||
Use: "clear",
|
||||
Short: "clear operation",
|
||||
func (m *MsgCmd) ClearMsgCmd() *cobra.Command {
|
||||
return &m.Command
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
||||
@ -25,7 +26,7 @@ func NewRootCmd(name string) (rootCmd *RootCmd) {
|
||||
if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
return log.InitFromConfig(name)
|
||||
return log.InitFromConfig(name, config.Config.Log.RemainLogLevel, config.Config.Log.IsStdout, config.Config.Log.IsJson)
|
||||
},
|
||||
}
|
||||
rootCmd.Command = c
|
||||
|
@ -187,13 +187,13 @@ type config struct {
|
||||
RotationTime int `yaml:"rotationTime"`
|
||||
RemainRotationCount uint `yaml:"remainRotationCount"`
|
||||
RemainLogLevel int `yaml:"remainLogLevel"`
|
||||
Stderr bool `yaml:"stderr"`
|
||||
IsStdout bool `yaml:"isStdout"`
|
||||
WithStack bool `yaml:"withStack"`
|
||||
ElasticSearchSwitch bool `yaml:"elasticSearchSwitch"`
|
||||
ElasticSearchAddr []string `yaml:"elasticSearchAddr"`
|
||||
ElasticSearchUser string `yaml:"elasticSearchUser"`
|
||||
ElasticSearchPassword string `yaml:"elasticSearchPassword"`
|
||||
isJson bool `yaml:"isJson"`
|
||||
IsJson bool `yaml:"isJson"`
|
||||
}
|
||||
ModuleName struct {
|
||||
LongConnSvrName string `yaml:"longConnSvrName"`
|
||||
|
@ -13,11 +13,6 @@ type UserModel struct {
|
||||
UserID string `gorm:"column:user_id;primary_key;size:64"`
|
||||
Nickname string `gorm:"column:name;size:255"`
|
||||
FaceURL string `gorm:"column:face_url;size:255"`
|
||||
//Gender int32 `gorm:"column:gender"`
|
||||
//AreaCode string `gorm:"column:area_code;size:8" json:"areaCode"`
|
||||
//PhoneNumber string `gorm:"column:phone_number;size:32"`
|
||||
//Birth time.Time `gorm:"column:birth"`
|
||||
//Email string `gorm:"column:email;size:64"`
|
||||
Ex string `gorm:"column:ex;size:1024"`
|
||||
CreateTime time.Time `gorm:"column:create_time;index:create_time; autoCreateTime"`
|
||||
AppMangerLevel int32 `gorm:"column:app_manger_level;default:18"`
|
||||
|
@ -2,13 +2,14 @@ package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
|
||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
@ -17,11 +18,20 @@ import (
|
||||
var (
|
||||
pkgLogger Logger = &ZapLogger{}
|
||||
sp = string(filepath.Separator)
|
||||
logLevelMap = map[int]zapcore.Level{
|
||||
6: zapcore.DebugLevel,
|
||||
5: zapcore.DebugLevel,
|
||||
4: zapcore.InfoLevel,
|
||||
3: zapcore.WarnLevel,
|
||||
2: zapcore.ErrorLevel,
|
||||
1: zapcore.FatalLevel,
|
||||
0: zapcore.PanicLevel,
|
||||
}
|
||||
)
|
||||
|
||||
// InitFromConfig initializes a Zap-based logger
|
||||
func InitFromConfig(name string) error {
|
||||
l, err := NewZapLogger()
|
||||
func InitFromConfig(name string, logLevel int, isStdout bool, isJson bool) error {
|
||||
l, err := NewZapLogger(logLevel, isStdout, isJson)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -49,19 +59,21 @@ type ZapLogger struct {
|
||||
zap *zap.SugaredLogger
|
||||
}
|
||||
|
||||
func NewZapLogger() (*ZapLogger, error) {
|
||||
func NewZapLogger(logLevel int, isStdout bool, isJson bool) (*ZapLogger, error) {
|
||||
zapConfig := zap.Config{
|
||||
Level: zap.NewAtomicLevelAt(zapcore.Level(config.Config.Log.RemainLogLevel)),
|
||||
Encoding: "json",
|
||||
Level: zap.NewAtomicLevelAt(logLevelMap[logLevel]),
|
||||
EncoderConfig: zap.NewProductionEncoderConfig(),
|
||||
InitialFields: map[string]interface{}{"PID": os.Getegid()},
|
||||
DisableStacktrace: true,
|
||||
}
|
||||
if config.Config.Log.Stderr {
|
||||
zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stderr")
|
||||
if isJson {
|
||||
zapConfig.Encoding = "json"
|
||||
}
|
||||
if isStdout {
|
||||
zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stdout", "stderr")
|
||||
}
|
||||
zl := &ZapLogger{}
|
||||
opts, err := zl.cores()
|
||||
opts, err := zl.cores(logLevel, isStdout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -73,7 +85,7 @@ func NewZapLogger() (*ZapLogger, error) {
|
||||
return zl, nil
|
||||
}
|
||||
|
||||
func (l *ZapLogger) cores() (zap.Option, error) {
|
||||
func (l *ZapLogger) cores(logLevel int, isStdout bool) (zap.Option, error) {
|
||||
c := zap.NewProductionEncoderConfig()
|
||||
c.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
c.EncodeDuration = zapcore.SecondsDurationEncoder
|
||||
@ -91,11 +103,11 @@ func (l *ZapLogger) cores() (zap.Option, error) {
|
||||
var cores []zapcore.Core
|
||||
if config.Config.Log.StorageLocation != "" {
|
||||
cores = []zapcore.Core{
|
||||
zapcore.NewCore(fileEncoder, writer, zap.NewAtomicLevelAt(zapcore.Level(config.Config.Log.RemainLogLevel))),
|
||||
zapcore.NewCore(fileEncoder, writer, zap.NewAtomicLevelAt(logLevelMap[logLevel])),
|
||||
}
|
||||
}
|
||||
if config.Config.Log.Stderr {
|
||||
cores = append(cores, zapcore.NewCore(fileEncoder, zapcore.Lock(os.Stdout), zap.NewAtomicLevelAt(zapcore.Level(config.Config.Log.RemainLogLevel))))
|
||||
if isStdout {
|
||||
cores = append(cores, zapcore.NewCore(fileEncoder, zapcore.Lock(os.Stdout), zap.NewAtomicLevelAt(zapcore.Level(logLevel))))
|
||||
}
|
||||
return zap.WrapCore(func(c zapcore.Core) zapcore.Core {
|
||||
return zapcore.NewTee(cores...)
|
||||
|
Loading…
x
Reference in New Issue
Block a user