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() {
|
func main() {
|
||||||
msgUtilsCmd := cmd.NewMsgUtilsCmd()
|
msgUtilsCmd := cmd.NewMsgUtilsCmd("openIMCmdUtils", "openIM cmd utils", nil)
|
||||||
msgUtilsCmd.AddSuperGroupIDFlag()
|
getCmd := cmd.NewGetCmd()
|
||||||
msgUtilsCmd.AddUserIDFlag()
|
fixCmd := cmd.NewFixCmd()
|
||||||
|
clearCmd := cmd.NewClearCmd()
|
||||||
seqCmd := cmd.NewSeqCmd()
|
seqCmd := cmd.NewSeqCmd()
|
||||||
msgCmd := cmd.NewMsgCmd()
|
msgCmd := cmd.NewMsgCmd()
|
||||||
cmd.GetCmd.AddCommand(seqCmd.Command, msgCmd.Command)
|
getCmd.AddCommand(seqCmd.GetSeqCmd(), msgCmd.GetMsgCmd())
|
||||||
cmd.FixCmd.AddCommand(seqCmd.Command)
|
getCmd.AddSuperGroupIDFlag()
|
||||||
cmd.GetCmd.AddCommand(msgCmd.Command)
|
getCmd.AddUserIDFlag()
|
||||||
msgUtilsCmd.AddCommand(cmd.GetCmd, cmd.FixCmd, cmd.ClearCmd)
|
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 {
|
if err := msgUtilsCmd.Execute(); err != nil {
|
||||||
panic(err.Error())
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ mysql:
|
|||||||
dbMaxOpenConns: 100
|
dbMaxOpenConns: 100
|
||||||
dbMaxIdleConns: 10
|
dbMaxIdleConns: 10
|
||||||
dbMaxLifeTime: 5
|
dbMaxLifeTime: 5
|
||||||
logLevel: 4 #1=slient 2=error 3=warn 4=info
|
logLevel: 6 #1=slient 2=error 3=warn 4=info
|
||||||
slowThreshold: 500
|
slowThreshold: 500
|
||||||
|
|
||||||
mongo:
|
mongo:
|
||||||
@ -160,8 +160,9 @@ log:
|
|||||||
rotationTime: 24
|
rotationTime: 24
|
||||||
remainRotationCount: 2 #日志数量
|
remainRotationCount: 2 #日志数量
|
||||||
#日志级别 6表示全都打印,测试阶段建议设置为6
|
#日志级别 6表示全都打印,测试阶段建议设置为6
|
||||||
remainLogLevel: -1
|
remainLogLevel: 6
|
||||||
stderr: true
|
isStdout: true
|
||||||
|
isJson: true
|
||||||
withStack: false
|
withStack: false
|
||||||
elasticSearchSwitch: false
|
elasticSearchSwitch: false
|
||||||
elasticSearchAddr: [ 127.0.0.1:9201 ]
|
elasticSearchAddr: [ 127.0.0.1:9201 ]
|
||||||
|
@ -1,157 +1,170 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/internal/tools"
|
"github.com/OpenIMSDK/Open-IM-Server/internal/tools"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MsgUtilsCmd struct {
|
type MsgUtilsCmd struct {
|
||||||
*RootCmd
|
cobra.Command
|
||||||
userID string
|
msgTool *tools.MsgTool
|
||||||
|
|
||||||
superGroupID string
|
|
||||||
|
|
||||||
clearAll bool
|
|
||||||
|
|
||||||
fixAll bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMsgUtilsCmd() MsgUtilsCmd {
|
|
||||||
return MsgUtilsCmd{RootCmd: NewRootCmd("msgUtils")}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgUtilsCmd) AddUserIDFlag() {
|
func (m *MsgUtilsCmd) AddUserIDFlag() {
|
||||||
m.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID")
|
m.Command.PersistentFlags().StringP("userID", "u", "", "openIM userID")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgUtilsCmd) GetUserIDFlag() string {
|
func (m *MsgUtilsCmd) getUserIDFlag(cmdLines *cobra.Command) string {
|
||||||
return m.userID
|
userID, _ := cmdLines.Flags().GetString("userID")
|
||||||
|
return userID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgUtilsCmd) AddFixAllFlag() {
|
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 {
|
func (m *MsgUtilsCmd) getFixAllFlag(cmdLines *cobra.Command) bool {
|
||||||
return m.fixAll
|
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() {
|
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 {
|
func (m *MsgUtilsCmd) getSuperGroupIDFlag(cmdLines *cobra.Command) string {
|
||||||
return m.superGroupID
|
superGroupID, _ := cmdLines.Flags().GetString("superGroupID")
|
||||||
|
return superGroupID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgUtilsCmd) AddClearAllFlag() bool {
|
func (m *MsgUtilsCmd) AddBeginSeqFlag() {
|
||||||
return m.clearAll
|
m.Command.PersistentFlags().Int64P("beginSeq", "b", 0, "openIM beginSeq")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgUtilsCmd) GetClearAllFlag() bool {
|
func (m *MsgUtilsCmd) getBeginSeqFlag(cmdLines *cobra.Command) int64 {
|
||||||
return m.clearAll
|
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 {
|
type SeqCmd struct {
|
||||||
Command *cobra.Command
|
*MsgUtilsCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (SeqCmd) RunCommand(cmdLines *cobra.Command, args []string) error {
|
func NewSeqCmd() *SeqCmd {
|
||||||
msgTool, err := tools.InitMsgTool()
|
seqCmd := &SeqCmd{
|
||||||
if err != nil {
|
NewMsgUtilsCmd("seq", "seq", nil),
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
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
|
return seqCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
type MsgCmd struct {
|
func (s *SeqCmd) GetSeqCmd() *cobra.Command {
|
||||||
Command *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 {
|
func (s *SeqCmd) FixSeqCmd() *cobra.Command {
|
||||||
msgCmd := MsgCmd{&cobra.Command{
|
return &s.Command
|
||||||
Use: "msg",
|
}
|
||||||
Short: "msg operation",
|
|
||||||
}}
|
type MsgCmd struct {
|
||||||
msgCmd.Command.RunE = msgCmd.RunCommand
|
*MsgUtilsCmd
|
||||||
msgCmd.Command.Flags().BoolP("clearAll", "c", false, "openIM clear all timeout msgs")
|
}
|
||||||
|
|
||||||
|
func NewMsgCmd() *MsgCmd {
|
||||||
|
msgCmd := &MsgCmd{
|
||||||
|
NewMsgUtilsCmd("msg", "msg", nil),
|
||||||
|
}
|
||||||
return msgCmd
|
return msgCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*MsgCmd) RunCommand(cmdLines *cobra.Command, args []string) error {
|
func (m *MsgCmd) GetMsgCmd() *cobra.Command {
|
||||||
msgTool, err := tools.InitMsgTool()
|
return &m.Command
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var GetCmd = &cobra.Command{
|
func (m *MsgCmd) ClearMsgCmd() *cobra.Command {
|
||||||
Use: "get",
|
return &m.Command
|
||||||
Short: "get operation",
|
|
||||||
}
|
|
||||||
|
|
||||||
var FixCmd = &cobra.Command{
|
|
||||||
Use: "fix",
|
|
||||||
Short: "fix seq operation",
|
|
||||||
}
|
|
||||||
|
|
||||||
var ClearCmd = &cobra.Command{
|
|
||||||
Use: "clear",
|
|
||||||
Short: "clear operation",
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
"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/constant"
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
"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 {
|
if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil {
|
||||||
return err
|
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
|
rootCmd.Command = c
|
||||||
|
@ -187,13 +187,13 @@ type config struct {
|
|||||||
RotationTime int `yaml:"rotationTime"`
|
RotationTime int `yaml:"rotationTime"`
|
||||||
RemainRotationCount uint `yaml:"remainRotationCount"`
|
RemainRotationCount uint `yaml:"remainRotationCount"`
|
||||||
RemainLogLevel int `yaml:"remainLogLevel"`
|
RemainLogLevel int `yaml:"remainLogLevel"`
|
||||||
Stderr bool `yaml:"stderr"`
|
IsStdout bool `yaml:"isStdout"`
|
||||||
WithStack bool `yaml:"withStack"`
|
WithStack bool `yaml:"withStack"`
|
||||||
ElasticSearchSwitch bool `yaml:"elasticSearchSwitch"`
|
ElasticSearchSwitch bool `yaml:"elasticSearchSwitch"`
|
||||||
ElasticSearchAddr []string `yaml:"elasticSearchAddr"`
|
ElasticSearchAddr []string `yaml:"elasticSearchAddr"`
|
||||||
ElasticSearchUser string `yaml:"elasticSearchUser"`
|
ElasticSearchUser string `yaml:"elasticSearchUser"`
|
||||||
ElasticSearchPassword string `yaml:"elasticSearchPassword"`
|
ElasticSearchPassword string `yaml:"elasticSearchPassword"`
|
||||||
isJson bool `yaml:"isJson"`
|
IsJson bool `yaml:"isJson"`
|
||||||
}
|
}
|
||||||
ModuleName struct {
|
ModuleName struct {
|
||||||
LongConnSvrName string `yaml:"longConnSvrName"`
|
LongConnSvrName string `yaml:"longConnSvrName"`
|
||||||
|
@ -13,11 +13,6 @@ type UserModel struct {
|
|||||||
UserID string `gorm:"column:user_id;primary_key;size:64"`
|
UserID string `gorm:"column:user_id;primary_key;size:64"`
|
||||||
Nickname string `gorm:"column:name;size:255"`
|
Nickname string `gorm:"column:name;size:255"`
|
||||||
FaceURL string `gorm:"column:face_url;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"`
|
Ex string `gorm:"column:ex;size:1024"`
|
||||||
CreateTime time.Time `gorm:"column:create_time;index:create_time; autoCreateTime"`
|
CreateTime time.Time `gorm:"column:create_time;index:create_time; autoCreateTime"`
|
||||||
AppMangerLevel int32 `gorm:"column:app_manger_level;default:18"`
|
AppMangerLevel int32 `gorm:"column:app_manger_level;default:18"`
|
||||||
|
@ -2,13 +2,14 @@ package log
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
"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/constant"
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
|
||||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
@ -17,11 +18,20 @@ import (
|
|||||||
var (
|
var (
|
||||||
pkgLogger Logger = &ZapLogger{}
|
pkgLogger Logger = &ZapLogger{}
|
||||||
sp = string(filepath.Separator)
|
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
|
// InitFromConfig initializes a Zap-based logger
|
||||||
func InitFromConfig(name string) error {
|
func InitFromConfig(name string, logLevel int, isStdout bool, isJson bool) error {
|
||||||
l, err := NewZapLogger()
|
l, err := NewZapLogger(logLevel, isStdout, isJson)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -49,19 +59,21 @@ type ZapLogger struct {
|
|||||||
zap *zap.SugaredLogger
|
zap *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewZapLogger() (*ZapLogger, error) {
|
func NewZapLogger(logLevel int, isStdout bool, isJson bool) (*ZapLogger, error) {
|
||||||
zapConfig := zap.Config{
|
zapConfig := zap.Config{
|
||||||
Level: zap.NewAtomicLevelAt(zapcore.Level(config.Config.Log.RemainLogLevel)),
|
Level: zap.NewAtomicLevelAt(logLevelMap[logLevel]),
|
||||||
Encoding: "json",
|
|
||||||
EncoderConfig: zap.NewProductionEncoderConfig(),
|
EncoderConfig: zap.NewProductionEncoderConfig(),
|
||||||
InitialFields: map[string]interface{}{"PID": os.Getegid()},
|
InitialFields: map[string]interface{}{"PID": os.Getegid()},
|
||||||
DisableStacktrace: true,
|
DisableStacktrace: true,
|
||||||
}
|
}
|
||||||
if config.Config.Log.Stderr {
|
if isJson {
|
||||||
zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stderr")
|
zapConfig.Encoding = "json"
|
||||||
|
}
|
||||||
|
if isStdout {
|
||||||
|
zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stdout", "stderr")
|
||||||
}
|
}
|
||||||
zl := &ZapLogger{}
|
zl := &ZapLogger{}
|
||||||
opts, err := zl.cores()
|
opts, err := zl.cores(logLevel, isStdout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -73,7 +85,7 @@ func NewZapLogger() (*ZapLogger, error) {
|
|||||||
return zl, nil
|
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 := zap.NewProductionEncoderConfig()
|
||||||
c.EncodeTime = zapcore.ISO8601TimeEncoder
|
c.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||||
c.EncodeDuration = zapcore.SecondsDurationEncoder
|
c.EncodeDuration = zapcore.SecondsDurationEncoder
|
||||||
@ -91,11 +103,11 @@ func (l *ZapLogger) cores() (zap.Option, error) {
|
|||||||
var cores []zapcore.Core
|
var cores []zapcore.Core
|
||||||
if config.Config.Log.StorageLocation != "" {
|
if config.Config.Log.StorageLocation != "" {
|
||||||
cores = []zapcore.Core{
|
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 {
|
if isStdout {
|
||||||
cores = append(cores, zapcore.NewCore(fileEncoder, zapcore.Lock(os.Stdout), zap.NewAtomicLevelAt(zapcore.Level(config.Config.Log.RemainLogLevel))))
|
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 zap.WrapCore(func(c zapcore.Core) zapcore.Core {
|
||||||
return zapcore.NewTee(cores...)
|
return zapcore.NewTee(cores...)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user