Skip to content

Commit

Permalink
update logger
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Oct 27, 2023
1 parent 0b282df commit 4aca4cd
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ config.yaml
addition/generation/data/*
!addition/generation/data/.gitkeep
sdk
logs

chat
chat.exe
Expand Down
2 changes: 1 addition & 1 deletion adapter/chatgpt/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
}
Expand Down
2 changes: 1 addition & 1 deletion adapter/claude/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
3 changes: 2 additions & 1 deletion addition/generation/generate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generation

import (
"chat/globals"
"chat/utils"
"fmt"
)
Expand All @@ -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())
}
}
Expand Down
69 changes: 69 additions & 0 deletions globals/logger.go
Original file line number Diff line number Diff line change
@@ -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...)
}
File renamed without changes.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions manager/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
))
}
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion manager/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
))
}
Expand Down
2 changes: 1 addition & 1 deletion manager/conversation/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion manager/transhipment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4aca4cd

Please sign in to comment.