diff --git a/config.example.yaml b/config.example.yaml index 639cc64..7560db7 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -69,8 +69,8 @@ maxRequestSleepNum: 100 maxRequestNum: 500 # 单ip同时最大搜索请求数 singleIpSearchNum: 10 - - +# 错误日志输出路径 +logOutput: err.log # Gzip gzip: false # 提交评论url diff --git a/internal/actions/comment.go b/internal/actions/comment.go index e91283b..bc7886d 100644 --- a/internal/actions/comment.go +++ b/internal/actions/comment.go @@ -94,17 +94,17 @@ func PostComment(c *gin.Context) { go func() { id := str.ToInteger[uint64](i, 0) if id <= 0 { - logs.ErrPrintln(err, "获取文档id", i) + logs.Error(errors.New("获取文档id错误"), "", i) return } post, err := cache.GetPostById(cc, id) if err != nil { - logs.ErrPrintln(err, "获取文档", id) + logs.Error(err, "获取文档错误", id) return } su := fmt.Sprintf("%s: %s[%s]发表了评论对文档[%v]的评论", wpconfig.GetOption("siteurl"), author, m, post.PostTitle) err = mail.SendMail([]string{conf.Mail.User}, su, comment) - logs.ErrPrintln(err, "发送邮件", conf.Mail.User, su, comment) + logs.IfError(err, "发送邮件", conf.Mail.User, su, comment) }() s, er := io.ReadAll(ress.Body) diff --git a/internal/cmd/main.go b/internal/cmd/main.go index e111fc5..d9d09b8 100644 --- a/internal/cmd/main.go +++ b/internal/cmd/main.go @@ -53,7 +53,10 @@ func initConf(c string) (err error) { if err != nil { return } - + err = logs.InitLogger() + if err != nil { + return err + } database, err := db.InitDb() if err != nil { return @@ -84,7 +87,7 @@ func flushCache() { defer func() { if r := recover(); r != nil { err := mail.SendMail([]string{config.GetConfig().Mail.User}, "清空缓存失败", fmt.Sprintf("err:[%s]", r)) - logs.ErrPrintln(err, "发邮件失败") + logs.IfError(err, "发邮件失败") } }() cachemanager.Flush() @@ -98,13 +101,15 @@ func reloads() { } }() err := config.InitConfig(confPath) - logs.ErrPrintln(err, "获取配置文件失败", confPath) + logs.IfError(err, "获取配置文件失败", confPath) + err = logs.InitLogger() + logs.IfError(err, "日志配置错误") _, err = db.InitDb() - logs.ErrPrintln(err, "重新读取db失败", config.GetConfig().Mysql) + logs.IfError(err, "重新读取db失败", config.GetConfig().Mysql) err = wpconfig.InitOptions() - logs.ErrPrintln(err, "获取网站设置WpOption失败") + logs.IfError(err, "获取网站设置WpOption失败") err = wpconfig.InitTerms() - logs.ErrPrintln(err, "获取WpTerms表失败") + logs.IfError(err, "获取WpTerms表失败") reload.Reload() flushCache() log.Println("reload complete") diff --git a/internal/middleware/sendmail.go b/internal/middleware/sendmail.go index 5f7bf04..f9bfdb5 100644 --- a/internal/middleware/sendmail.go +++ b/internal/middleware/sendmail.go @@ -48,7 +48,7 @@ func RecoverAndSendMail(w io.Writer) func(ctx *gin.Context) { fmt.Sprintf("%s%s %s 发生错误", fmt.Sprintf(wpconfig.GetOption("siteurl")), c.FullPath(), time.Now().Format(time.RFC1123Z)), content) if er != nil { - logs.ErrPrintln(er, "recover send mail fail", fmt.Sprintf("%v", err)) + logs.IfError(er, "recover send mail fail", fmt.Sprintf("%v", err)) } }() } diff --git a/internal/pkg/cache/cache.go b/internal/pkg/cache/cache.go index 1ef3a92..3719c94 100644 --- a/internal/pkg/cache/cache.go +++ b/internal/pkg/cache/cache.go @@ -103,7 +103,7 @@ func Archives(ctx context.Context) []models.PostArchive { if l > 0 && a.month != m || l < 1 { r, err := a.fn(ctx) if err != nil { - logs.ErrPrintln(err, "set cache err[%s]") + logs.Error(err, "set cache fail") return nil } a.month = m @@ -123,7 +123,7 @@ func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy { tt = t[0] } r, err := categoryAndTagsCaches.GetCache(ctx, tt, time.Second, ctx, tt) - logs.ErrPrintln(err, "get category err") + logs.IfError(err, "get category fail") return r } func AllCategoryTagsNames(ctx context.Context, t ...int) map[string]struct{} { @@ -132,7 +132,10 @@ func AllCategoryTagsNames(ctx context.Context, t ...int) map[string]struct{} { tt = t[0] } r, err := categoryAndTagsCaches.GetCache(ctx, tt, time.Second, ctx, tt) - logs.ErrPrintln(err, "get category err") + if err != nil { + logs.Error(err, "get category fail") + return nil + } return slice.ToMap(r, func(t models.TermsMy) (string, struct{}) { return t.Name, struct{}{} }, true) diff --git a/internal/pkg/cache/comments.go b/internal/pkg/cache/comments.go index a1572fe..2883fec 100644 --- a/internal/pkg/cache/comments.go +++ b/internal/pkg/cache/comments.go @@ -15,7 +15,7 @@ func RecentComments(ctx context.Context, n int) (r []models.Comments) { if len(r) > n { r = r[0:n] } - logs.ErrPrintln(err, "get recent comment") + logs.IfError(err, "get recent comment fail") return } diff --git a/internal/pkg/cache/feed.go b/internal/pkg/cache/feed.go index 90d975e..cb41aad 100644 --- a/internal/pkg/cache/feed.go +++ b/internal/pkg/cache/feed.go @@ -96,7 +96,7 @@ func postFeed(arg ...any) (x string, err error) { id := arg[1].(string) ID := str.ToInteger[uint64](id, 0) maxId, err := GetMaxPostId(c) - logs.ErrPrintln(err, "get max post id") + logs.IfError(err, "get max post id") if ID < 1 || ID > maxId || err != nil { return } diff --git a/internal/pkg/cache/posts.go b/internal/pkg/cache/posts.go index 1709070..cc8480d 100644 --- a/internal/pkg/cache/posts.go +++ b/internal/pkg/cache/posts.go @@ -53,7 +53,7 @@ func RecentPosts(ctx context.Context, n int) (r []models.Posts) { if n < len(r) { r = r[:n] } - logs.ErrPrintln(err, "get recent post") + logs.IfError(err, "get recent post") return } diff --git a/internal/pkg/cache/users.go b/internal/pkg/cache/users.go index 48ac880..0678436 100644 --- a/internal/pkg/cache/users.go +++ b/internal/pkg/cache/users.go @@ -25,6 +25,6 @@ func GetAllUsername(ctx context.Context) (map[string]struct{}, error) { func GetUserById(ctx context.Context, uid uint64) models.Users { r, err := usersCache.GetCache(ctx, uid, time.Second, ctx, uid) - logs.ErrPrintln(err, "get user", uid) + logs.IfError(err, "get user", uid) return r } diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 707bb89..cfbc4aa 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -15,65 +15,66 @@ func GetConfig() Config { } type Config struct { - Ssl Ssl `yaml:"ssl"` - Mysql Mysql `yaml:"mysql"` - Mail Mail `yaml:"mail"` - CacheTime CacheTime `yaml:"cacheTime"` - DigestWordCount int `yaml:"digestWordCount"` - MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum"` - MaxRequestNum int64 `yaml:"maxRequestNum"` - SingleIpSearchNum int64 `yaml:"singleIpSearchNum"` - Gzip bool `yaml:"gzip"` - PostCommentUrl string `yaml:"postCommentUrl"` - TrustIps []string `yaml:"trustIps"` - TrustServerNames []string `yaml:"trustServerNames"` - Theme string `yaml:"theme"` - PostOrder string `yaml:"postOrder"` - UploadDir string `yaml:"uploadDir"` - Pprof string `yaml:"pprof"` - ListPagePlugins []string `yaml:"listPagePlugins"` - PaginationStep int `yaml:"paginationStep"` - ShowQuerySql bool `yaml:"showQuerySql"` - Plugins []string `yaml:"plugins"` + Ssl Ssl `yaml:"ssl" json:"ssl"` + Mysql Mysql `yaml:"mysql" json:"mysql"` + Mail Mail `yaml:"mail" json:"mail"` + CacheTime CacheTime `yaml:"cacheTime" json:"cacheTime"` + DigestWordCount int `yaml:"digestWordCount" json:"digestWordCount,omitempty"` + MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum" json:"maxRequestSleepNum,omitempty"` + MaxRequestNum int64 `yaml:"maxRequestNum" json:"maxRequestNum,omitempty"` + SingleIpSearchNum int64 `yaml:"singleIpSearchNum" json:"singleIpSearchNum,omitempty"` + Gzip bool `yaml:"gzip" json:"gzip,omitempty"` + PostCommentUrl string `yaml:"postCommentUrl" json:"postCommentUrl,omitempty"` + TrustIps []string `yaml:"trustIps" json:"trustIps,omitempty"` + TrustServerNames []string `yaml:"trustServerNames" json:"trustServerNames,omitempty"` + Theme string `yaml:"theme" json:"theme,omitempty"` + PostOrder string `yaml:"postOrder" json:"postOrder,omitempty"` + UploadDir string `yaml:"uploadDir" json:"uploadDir,omitempty"` + Pprof string `yaml:"pprof" json:"pprof,omitempty"` + ListPagePlugins []string `yaml:"listPagePlugins" json:"listPagePlugins,omitempty"` + PaginationStep int `yaml:"paginationStep" json:"paginationStep,omitempty"` + ShowQuerySql bool `yaml:"showQuerySql" json:"showQuerySql,omitempty"` + Plugins []string `yaml:"plugins" json:"plugins,omitempty"` + LogOutput string `yaml:"logOutput" json:"logOutput,omitempty"` } type CacheTime struct { - CacheControl time.Duration `yaml:"cacheControl"` - RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"` - CategoryCacheTime time.Duration `yaml:"categoryCacheTime"` - ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"` - ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime"` - RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"` - DigestCacheTime time.Duration `yaml:"digestCacheTime"` - PostListCacheTime time.Duration `yaml:"postListCacheTime"` - SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"` - MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"` - PostDataCacheTime time.Duration `yaml:"postDataCacheTime"` - PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime"` - CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"` - MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"` - UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"` - CommentsCacheTime time.Duration `yaml:"commentsCacheTime"` - SleepTime []time.Duration `yaml:"sleepTime"` + CacheControl time.Duration `yaml:"cacheControl" json:"cacheControl,omitempty"` + RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime" json:"recentPostCacheTime,omitempty"` + CategoryCacheTime time.Duration `yaml:"categoryCacheTime" json:"categoryCacheTime,omitempty"` + ArchiveCacheTime time.Duration `yaml:"archiveCacheTime" json:"archiveCacheTime,omitempty"` + ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime" json:"contextPostCacheTime,omitempty"` + RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime" json:"recentCommentsCacheTime,omitempty"` + DigestCacheTime time.Duration `yaml:"digestCacheTime" json:"digestCacheTime,omitempty"` + PostListCacheTime time.Duration `yaml:"postListCacheTime" json:"postListCacheTime,omitempty"` + SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime" json:"searchPostCacheTime,omitempty"` + MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime" json:"monthPostCacheTime,omitempty"` + PostDataCacheTime time.Duration `yaml:"postDataCacheTime" json:"postDataCacheTime,omitempty"` + PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime" json:"postCommentsCacheTime,omitempty"` + CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime" json:"crontabClearCacheTime,omitempty"` + MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime" json:"maxPostIdCacheTime,omitempty"` + UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime" json:"userInfoCacheTime,omitempty"` + CommentsCacheTime time.Duration `yaml:"commentsCacheTime" json:"commentsCacheTime,omitempty"` + SleepTime []time.Duration `yaml:"sleepTime" json:"sleepTime,omitempty"` } type Ssl struct { - Cert string `yaml:"cert"` - Key string `yaml:"key"` + Cert string `yaml:"cert" json:"cert,omitempty"` + Key string `yaml:"key" json:"key,omitempty"` } type Mail struct { - User string `yaml:"user"` - Alias string `yaml:"alias"` - Pass string `yaml:"pass"` - Host string `yaml:"host"` - Port int `yaml:"port"` - Ssl bool `yaml:"ssl"` + User string `yaml:"user" json:"user,omitempty"` + Alias string `yaml:"alias" json:"alias,omitempty"` + Pass string `yaml:"pass" json:"pass,omitempty"` + Host string `yaml:"host" json:"host,omitempty"` + Port int `yaml:"port" json:"port,omitempty"` + Ssl bool `yaml:"ssl" json:"ssl,omitempty"` } type Mysql struct { - Dsn Dsn `yaml:"dsn"` - Pool Pool `yaml:"pool"` + Dsn Dsn `yaml:"dsn" json:"dsn"` + Pool Pool `yaml:"pool" json:"pool"` } func InitConfig(conf string) error { @@ -94,12 +95,12 @@ func InitConfig(conf string) error { } type Dsn struct { - Host string `yaml:"host"` - Port string `yaml:"port"` - Db string `yaml:"db"` - User string `yaml:"user"` - Password string `yaml:"password"` - Charset string `yaml:"charset"` + Host string `yaml:"host" json:"host,omitempty"` + Port string `yaml:"port" json:"port,omitempty"` + Db string `yaml:"db" json:"db,omitempty"` + User string `yaml:"user" json:"user,omitempty"` + Password string `yaml:"password" json:"password,omitempty"` + Charset string `yaml:"charset" json:"charset,omitempty"` } func (m Dsn) GetDsn() string { @@ -111,8 +112,8 @@ func (m Dsn) GetDsn() string { } type Pool struct { - ConnMaxIdleTime time.Duration `yaml:"connMaxIdleTime"` - MaxOpenConn int `yaml:"maxOpenConn"` - MaxIdleConn int `yaml:"maxIdleConn"` - ConnMaxLifetime time.Duration `yaml:"connMaxLifetime"` + ConnMaxIdleTime time.Duration `yaml:"connMaxIdleTime" json:"connMaxIdleTime,omitempty"` + MaxOpenConn int `yaml:"maxOpenConn" json:"maxOpenConn,omitempty"` + MaxIdleConn int `yaml:"maxIdleConn" json:"maxIdleConn,omitempty"` + ConnMaxLifetime time.Duration `yaml:"connMaxLifetime" json:"connMaxLifetime,omitempty"` } diff --git a/internal/pkg/dao/postmeta.go b/internal/pkg/dao/postmeta.go index a5288a9..3f98b6a 100644 --- a/internal/pkg/dao/postmeta.go +++ b/internal/pkg/dao/postmeta.go @@ -30,7 +30,7 @@ func GetPostMetaByPostIds(args ...any) (r map[uint64]map[string]any, err error) if postmeta.MetaKey == "_wp_attachment_metadata" { metadata, err := phphelper.UnPHPSerializeToStruct[models.WpAttachmentMetadata](postmeta.MetaValue) if err != nil { - logs.ErrPrintln(err, "解析postmeta失败", postmeta.MetaId, postmeta.MetaValue) + logs.Error(err, "解析postmeta失败", postmeta.MetaId, postmeta.MetaValue) continue } r[postmeta.PostId][postmeta.MetaKey] = metadata diff --git a/internal/pkg/logs/log.go b/internal/pkg/logs/log.go index 5dfa925..515da40 100644 --- a/internal/pkg/logs/log.go +++ b/internal/pkg/logs/log.go @@ -2,17 +2,45 @@ package logs import ( "fmt" + "github.com/fthvgb1/wp-go/internal/pkg/config" + "github.com/fthvgb1/wp-go/safety" + "io" "log" + "os" "runtime" "strings" ) -func ErrPrintln(err error, desc string, args ...any) { - if err == nil { - return +var logs = safety.NewVar[*log.Logger](nil) + +func InitLogger() error { + l := &log.Logger{} + c := config.GetConfig() + if c.LogOutput == "" { + c.LogOutput = "stderr" } + var out io.Writer + switch c.LogOutput { + case "stdout": + out = os.Stdout + case "stderr": + out = os.Stderr + default: + file, err := os.OpenFile(c.LogOutput, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777) + if err != nil { + return err + } + out = file + } + logs.Store(l) + l.SetFlags(log.Ldate | log.Ltime) + l.SetOutput(out) + return nil +} + +func Errs(err error, depth int, desc string, args ...any) { var pcs [1]uintptr - runtime.Callers(2, pcs[:]) + runtime.Callers(depth, pcs[:]) f := runtime.CallersFrames([]uintptr{pcs[0]}) ff, _ := f.Next() s := strings.Builder{} @@ -23,7 +51,16 @@ func ErrPrintln(err error, desc string, args ...any) { _, _ = fmt.Fprintf(&s, "%v", arg) } } - if err != nil { - log.Println(s.String()) - } + logs.Load().Println(s.String()) +} + +func Error(err error, desc string, args ...any) { + Errs(err, 3, desc, args) +} + +func IfError(err error, desc string, args ...any) { + if err == nil { + return + } + Errs(err, 3, desc, args...) } diff --git a/internal/theme/twentyfifteen/twentyfifteen.go b/internal/theme/twentyfifteen/twentyfifteen.go index 3d43ea8..d4df9c9 100644 --- a/internal/theme/twentyfifteen/twentyfifteen.go +++ b/internal/theme/twentyfifteen/twentyfifteen.go @@ -19,19 +19,24 @@ const ThemeName = "twentyfifteen" func Init(fs embed.FS) { b, err := fs.ReadFile("twentyfifteen/themesupport.json") if err != nil { + logs.Error(err, "读取themesupport.json失败") return } err = json.Unmarshal(b, &themesupport) - logs.ErrPrintln(err, "解析themesupport失败") + if err != nil { + logs.Error(err, "解析themesupport失败") + return + } bytes, err := fs.ReadFile("twentyfifteen/colorscheme.json") if err != nil { + logs.Error(err, "读取colorscheme.json失败") return } err = json.Unmarshal(bytes, &colorscheme) if err != nil { + logs.Error(err, "解析colorscheme失败") return } - logs.ErrPrintln(err, "解析colorscheme失败") } var pipe = wp.HandlePipe(wp.ExecuteHandleFn, widget.MiddleWare(ready, data)...) @@ -56,7 +61,7 @@ func data(next wp.HandleFn[*wp.Handle], h *wp.Handle) { next(h) } -func configs(h *wp.Handle) *wp.Handle { +func configs(h *wp.Handle) { h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string, args ...any) string { return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`) }) @@ -67,5 +72,4 @@ func configs(h *wp.Handle) *wp.Handle { h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50)) h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50)) - return h } diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index 842e182..1908a09 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -49,7 +49,7 @@ func Hook(h *wp.Handle) { pipe(h) } -func configs(h *wp.Handle) *wp.Handle { +func configs(h *wp.Handle) { wphandle.RegisterPlugins(h, config.GetConfig().Plugins...) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20)) h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader) @@ -67,7 +67,6 @@ func configs(h *wp.Handle) *wp.Handle { wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 10)) h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10)) - return h } func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) { wp.InitThemeArgAndConfig(configs, h) @@ -95,7 +94,7 @@ var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] { func errorsHandle(h *wp.Handle) { switch h.Stats { case constraints.Error404, constraints.InternalErr, constraints.ParamError: - logs.ErrPrintln(h.Err(), "报错:") + logs.IfError(h.Err(), "报错:") h.SetTempl("twentyseventeen/posts/error.gohtml") } } diff --git a/internal/theme/wp/components/block/category.go b/internal/theme/wp/components/block/category.go index 02980e0..6110547 100644 --- a/internal/theme/wp/components/block/category.go +++ b/internal/theme/wp/components/block/category.go @@ -78,7 +78,7 @@ func Category(h *wp.Handle, id string, blockParser ParserBlock) (func() string, var con any err = json.Unmarshal([]byte(blockParser.Attrs), &con) if err != nil { - logs.ErrPrintln(err, "解析category attr错误", blockParser.Attrs) + logs.Error(err, "解析category attr错误", blockParser.Attrs) return nil } var conf map[any]any diff --git a/internal/theme/wp/customheader.go b/internal/theme/wp/customheader.go index c049f33..8128b24 100644 --- a/internal/theme/wp/customheader.go +++ b/internal/theme/wp/customheader.go @@ -27,7 +27,7 @@ func (h *Handle) GetCustomHeader() (r models.PostThumbnail, isRand bool) { return hs }) if err != nil { - logs.ErrPrintln(err, "获取页眉背景图失败") + logs.Error(err, "获取页眉背景图失败") return } hs := slice.Copy(img) diff --git a/internal/theme/wp/detail.go b/internal/theme/wp/detail.go index 2df6ec3..d1f0134 100644 --- a/internal/theme/wp/detail.go +++ b/internal/theme/wp/detail.go @@ -38,7 +38,7 @@ func (d *DetailHandle) BuildDetailData() (err error) { func (d *DetailHandle) CheckAndGetPost() (err error) { id := str.ToInteger[uint64](d.C.Param("id"), 0) maxId, err := cache.GetMaxPostId(d.C) - logs.ErrPrintln(err, "get max post id") + logs.IfError(err, "get max post id") if id > maxId || id <= 0 { d.Stats = constraints.ParamError err = errors.New("无效的文档id") @@ -49,7 +49,7 @@ func (d *DetailHandle) CheckAndGetPost() (err error) { post, err := cache.GetPostById(d.C, id) if post.Id == 0 || err != nil || post.PostStatus != "publish" { d.Stats = constraints.Error404 - logs.ErrPrintln(err, "获取id失败") + logs.IfError(err, "获取id失败") err = errors.New(str.Join("无效的文档id ")) return } @@ -71,7 +71,7 @@ func (d *DetailHandle) PasswordProject() { } func (d *DetailHandle) Comment() { comments, err := cache.PostComments(d.C, d.Post.Id) - logs.ErrPrintln(err, "get d.Post comment", d.Post.Id) + logs.IfError(err, "get d.Post comment", d.Post.Id) d.ginH["comments"] = comments d.Comments = comments @@ -95,7 +95,7 @@ func (d *DetailHandle) RenderComment() { func (d *DetailHandle) ContextPost() { prev, next, err := cache.GetContextPost(d.C, d.Post.Id, d.Post.PostDate) - logs.ErrPrintln(err, "get pre and next post", d.Post.Id, d.Post.PostDate) + logs.IfError(err, "get pre and next post", d.Post.Id, d.Post.PostDate) d.ginH["next"] = next d.ginH["prev"] = prev } diff --git a/internal/theme/wp/listpostplugins.go b/internal/theme/wp/listpostplugins.go index b299578..b593c9b 100644 --- a/internal/theme/wp/listpostplugins.go +++ b/internal/theme/wp/listpostplugins.go @@ -70,7 +70,7 @@ func GetListPostPlugins(name []string, m map[string]Plugin[models.Posts, *Handle if ok { return v, true } - logs.ErrPrintln(errors.New(str.Join("插件", t, "不存在")), "") + logs.IfError(errors.New(str.Join("插件", t, "不存在")), "") return nil, false }) } diff --git a/internal/theme/wp/stickyposts.go b/internal/theme/wp/stickyposts.go index d88c8a8..d701679 100644 --- a/internal/theme/wp/stickyposts.go +++ b/internal/theme/wp/stickyposts.go @@ -21,7 +21,7 @@ func (h *Handle) StickPosts() []models.Posts { } array, err := phpserialize.UnmarshalIndexedArray([]byte(v)) if err != nil { - logs.ErrPrintln(err, "解析option sticky_posts错误") + logs.Error(err, "解析option sticky_posts错误", v) return } r = slice.FilterAndMap(array, func(t any) (models.Posts, bool) { diff --git a/internal/theme/wp/wp.go b/internal/theme/wp/wp.go index f1e7dde..c2c6e90 100644 --- a/internal/theme/wp/wp.go +++ b/internal/theme/wp/wp.go @@ -65,22 +65,23 @@ type HandleCall struct { Order int } -func InitThemeArgAndConfig(fn func(*Handle) *Handle, h *Handle) { +func InitThemeArgAndConfig(fn func(*Handle), h *Handle) { hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) Handle { h.components = make(map[string][]Components[string]) h.handleFns = make(map[int][]HandleCall) h.componentsArgs = make(map[string]any) h.componentFilterFn = make(map[string][]func(*Handle, string, ...any) string) - hh := fn(h) - return *hh + h.ginH = gin.H{} + fn(h) + return *h }) m := make(map[string][]Components[string]) for k, v := range hh.components { vv := make([]Components[string], len(v)) copy(vv, v) - m[k] = vv // slice.Copy(v) + m[k] = vv } - h.components = m // maps.Copy(hh.components) + h.components = m h.handleFns = hh.handleFns h.componentsArgs = hh.componentsArgs h.componentFilterFn = hh.componentFilterFn @@ -201,12 +202,11 @@ func (h *Handle) SetComponentsArgs(key string, value any) { func NewHandle(c *gin.Context, scene int, theme string) *Handle { mods, err := wpconfig.GetThemeMods(theme) - logs.ErrPrintln(err, "获取mods失败") + logs.IfError(err, "获取mods失败") return &Handle{ C: c, theme: theme, Session: sessions.Default(c), - ginH: gin.H{}, scene: scene, Stats: constraints.Ok, themeMods: mods, diff --git a/internal/wpconfig/thememods.go b/internal/wpconfig/thememods.go index a119a15..0afbda9 100644 --- a/internal/wpconfig/thememods.go +++ b/internal/wpconfig/thememods.go @@ -149,7 +149,7 @@ func GetThemeMods(theme string) (r ThemeMods, err error) { //这里在的err可以不用处理,因为php的默认值和有设置过的类型可能不一样,直接按有设置的类型处理就行 r, err = maps.StrAnyMapToStruct[ThemeMods](m) if err != nil { - logs.ErrPrintln(err, "解析thememods错误") + logs.Error(err, "解析thememods错误(可忽略)") err = nil } r.setThemeSupport(theme)