mirror of
https://github.com/coaidev/coai.git
synced 2025-06-07 22:30:17 +09:00
update logger
This commit is contained in:
parent
0b282dff85
commit
4aca4cd669
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,6 +5,7 @@ config.yaml
|
|||||||
addition/generation/data/*
|
addition/generation/data/*
|
||||||
!addition/generation/data/.gitkeep
|
!addition/generation/data/.gitkeep
|
||||||
sdk
|
sdk
|
||||||
|
logs
|
||||||
|
|
||||||
chat
|
chat
|
||||||
chat.exe
|
chat.exe
|
||||||
|
@ -58,7 +58,7 @@ func (c *ChatInstance) ProcessLine(data string) string {
|
|||||||
var form *ChatStreamResponse
|
var form *ChatStreamResponse
|
||||||
if form = utils.UnmarshalForm[ChatStreamResponse](item); form == nil {
|
if form = utils.UnmarshalForm[ChatStreamResponse](item); form == nil {
|
||||||
if form = utils.UnmarshalForm[ChatStreamResponse](item[:len(item)-1]); 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 ""
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,7 @@ func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, hook globals.Ho
|
|||||||
return nil
|
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
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package generation
|
package generation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"chat/globals"
|
||||||
"chat/utils"
|
"chat/utils"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
@ -9,7 +10,7 @@ func CreateGenerationWithCache(model string, prompt string, enableReverse bool,
|
|||||||
hash, path := GetFolderByHash(model, prompt)
|
hash, path := GetFolderByHash(model, prompt)
|
||||||
if !utils.Exists(path) {
|
if !utils.Exists(path) {
|
||||||
if err := CreateGeneration(model, prompt, path, enableReverse, hook); err != nil {
|
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())
|
return "", fmt.Errorf("error during generate project: %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
69
globals/logger.go
Normal file
69
globals/logger.go
Normal 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
1
go.mod
@ -45,6 +45,7 @@ require (
|
|||||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // 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/pelletier/go-toml/v2 v2.1.0 // indirect
|
||||||
github.com/refraction-networking/utls v1.3.2 // indirect
|
github.com/refraction-networking/utls v1.3.2 // indirect
|
||||||
github.com/sirupsen/logrus v1.9.3 // indirect
|
github.com/sirupsen/logrus v1.9.3 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -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/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 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
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/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
||||||
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
|
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
|
||||||
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
|
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
|
||||||
|
@ -78,7 +78,7 @@ func MockStreamSender(conn *Connection, message string) {
|
|||||||
func ChatHandler(conn *Connection, user *auth.User, instance *conversation.Conversation) string {
|
func ChatHandler(conn *Connection, user *auth.User, instance *conversation.Conversation) string {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
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(),
|
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" {
|
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)
|
CollectQuota(conn.GetCtx(), user, buffer.GetQuota(), reversible)
|
||||||
conn.Send(globals.ChatSegmentResponse{
|
conn.Send(globals.ChatSegmentResponse{
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
func NativeChatHandler(c *gin.Context, user *auth.User, model string, message []globals.Message, enableWeb bool) (string, string, float32) {
|
func NativeChatHandler(c *gin.Context, user *auth.User, model string, message []globals.Message, enableWeb bool) (string, string, float32) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
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(),
|
err, model, c.ClientIP(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ func (c *Conversation) SaveConversation(db *sql.DB) bool {
|
|||||||
|
|
||||||
_, err = stmt.Exec(c.UserID, c.Id, c.Name, data, c.Model)
|
_, err = stmt.Exec(c.UserID, c.Id, c.Name, data, c.Model)
|
||||||
if err != nil {
|
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 false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -123,7 +123,7 @@ func sendTranshipmentResponse(c *gin.Context, form TranshipmentForm, id string,
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != 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)
|
CollectQuota(c, user, buffer.GetQuota(), reversible)
|
||||||
|
Loading…
Reference in New Issue
Block a user