update logger

This commit is contained in:
Zhang Minghan 2023-10-27 20:40:01 +08:00
parent 0b282dff85
commit 4aca4cd669
12 changed files with 82 additions and 8 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@ config.yaml
addition/generation/data/*
!addition/generation/data/.gitkeep
sdk
logs
chat
chat.exe

View File

@ -58,7 +58,7 @@ func (c *ChatInstance) ProcessLine(data string) string {
var form *ChatStreamResponse
if form = utils.UnmarshalForm[ChatStreamResponse](item); form == nil {
if form = utils.UnmarshalForm[ChatStreamResponse](item[:len(item)-1]); form == nil {
fmt.Println(fmt.Sprintf("chatgpt error: cannot parse response: %s", item))
globals.Warn(fmt.Sprintf("chatgpt error: cannot parse response: %s", item))
return ""
}
}

View File

@ -96,7 +96,7 @@ func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, hook globals.Ho
return nil
}
fmt.Println(fmt.Sprintf("anthropic error: cannot parse response: %s", data))
globals.Warn(fmt.Sprintf("anthropic error: cannot parse response: %s", data))
return nil
})
}

View File

@ -1,6 +1,7 @@
package generation
import (
"chat/globals"
"chat/utils"
"fmt"
)
@ -9,7 +10,7 @@ func CreateGenerationWithCache(model string, prompt string, enableReverse bool,
hash, path := GetFolderByHash(model, prompt)
if !utils.Exists(path) {
if err := CreateGeneration(model, prompt, path, enableReverse, hook); err != nil {
fmt.Println(fmt.Sprintf("[Project] error during generation %s (model %s): %s", prompt, model, err.Error()))
globals.Info(fmt.Sprintf("[Project] error during generation %s (model %s): %s", prompt, model, err.Error()))
return "", fmt.Errorf("error during generate project: %s", err.Error())
}
}

69
globals/logger.go Normal file
View File

@ -0,0 +1,69 @@
package globals
import (
"fmt"
"github.com/natefinch/lumberjack"
"github.com/sirupsen/logrus"
"strings"
)
var Logger *logrus.Logger
type AppLogger struct {
*logrus.Logger
}
func (l *AppLogger) Format(entry *logrus.Entry) ([]byte, error) {
data := fmt.Sprintf(
"[%s] - [%s] - %s\n",
strings.ToUpper(entry.Level.String()),
entry.Time.Format("2006-01-02 15:04:05"),
entry.Message,
)
return []byte(data), nil
}
func init() {
Logger = logrus.New()
Logger.SetFormatter(&AppLogger{
Logger: Logger,
})
Logger.SetOutput(&lumberjack.Logger{
Filename: "logs/chat.log",
MaxSize: 20,
MaxBackups: 20,
MaxAge: 1,
})
Logger.SetLevel(logrus.DebugLevel)
}
func Output(args ...interface{}) {
Logger.Println(args...)
}
func Debug(args ...interface{}) {
Logger.Debugln(args...)
}
func Info(args ...interface{}) {
Logger.Infoln(args...)
}
func Warn(args ...interface{}) {
Logger.Warnln(args...)
}
func Error(args ...interface{}) {
Logger.Errorln(args...)
}
func Fatal(args ...interface{}) {
Logger.Fatalln(args...)
}
func Panic(args ...interface{}) {
Logger.Panicln(args...)
}

1
go.mod
View File

@ -45,6 +45,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/refraction-networking/utls v1.3.2 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect

2
go.sum
View File

@ -209,6 +209,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=

View File

@ -78,7 +78,7 @@ func MockStreamSender(conn *Connection, message string) {
func ChatHandler(conn *Connection, user *auth.User, instance *conversation.Conversation) string {
defer func() {
if err := recover(); err != nil {
fmt.Println(fmt.Sprintf("caught panic from chat handler: %s (instance: %s, client: %s)",
globals.Warn(fmt.Sprintf("caught panic from chat handler: %s (instance: %s, client: %s)",
err, instance.GetModel(), conn.GetCtx().ClientIP(),
))
}
@ -128,7 +128,7 @@ func ChatHandler(conn *Connection, user *auth.User, instance *conversation.Conve
})
if err != nil && err.Error() != "signal" {
fmt.Println(fmt.Sprintf("caught error from chat handler: %s (instance: %s, client: %s)", err, model, conn.GetCtx().ClientIP()))
globals.Warn(fmt.Sprintf("caught error from chat handler: %s (instance: %s, client: %s)", err, model, conn.GetCtx().ClientIP()))
CollectQuota(conn.GetCtx(), user, buffer.GetQuota(), reversible)
conn.Send(globals.ChatSegmentResponse{

View File

@ -13,7 +13,7 @@ import (
func NativeChatHandler(c *gin.Context, user *auth.User, model string, message []globals.Message, enableWeb bool) (string, string, float32) {
defer func() {
if err := recover(); err != nil {
fmt.Println(fmt.Sprintf("caught panic from chat handler: %s (instance: %s, client: %s)",
globals.Warn(fmt.Sprintf("caught panic from chat handler: %s (instance: %s, client: %s)",
err, model, c.ClientIP(),
))
}

View File

@ -34,7 +34,7 @@ func (c *Conversation) SaveConversation(db *sql.DB) bool {
_, err = stmt.Exec(c.UserID, c.Id, c.Name, data, c.Model)
if err != nil {
fmt.Println(fmt.Sprintf("execute error during save conversation: %s", err.Error()))
globals.Info(fmt.Sprintf("execute error during save conversation: %s", err.Error()))
return false
}
return true

View File

@ -123,7 +123,7 @@ func sendTranshipmentResponse(c *gin.Context, form TranshipmentForm, id string,
return nil
})
if err != nil {
fmt.Println(fmt.Sprintf("error from chat request api: %s", err.Error()))
globals.Warn(fmt.Sprintf("error from chat request api: %s", err.Error()))
}
CollectQuota(c, user, buffer.GetQuota(), reversible)