A:调整日志输出格式

This commit is contained in:
jimmy committed 2025-07-23 21:13:34 +08:00
1 parent e0dd6a4ce8
commit 1c97bfb181
6 files changed
+59 -100

No files matched your search

+4 -1
View File
@@ -18,6 +18,8 @@ type ImageCompressionOptions struct {
ThumbnailSize []int
// 缩略图质量
ThumbnailQuality int
// 用户屏幕尺寸大小
ScreenSize int
}
// PathConfig 路径相关配置
@@ -64,8 +66,9 @@ var CONFIG Config
func InitConfig() *Config {
i := ImageCompressionOptions{
ThumbnailFormat: consts.FormatJPG,
ThumbnailSize: []int{256, 512, 720},
ThumbnailSize: []int{512, 720},
ThumbnailQuality: 80,
ScreenSize: 1080,
}
pathConfig := PathConfig{
CachePath: "cache",
+14
View File
@@ -0,0 +1,14 @@
package utils
/*
统一图像格式
输入图像路径,判断图像是否是 PNG、JPG、Webp
如果是 Raw 格式,首先转换为 PNG 格式作为中转格式(首个版本可不支持)
如果是复杂格式则使用 ImageMagick 转换为 PNG 中转格式,再转换为 JPG 或 Webp 格式
如果是 PNG、JPG、Webp 则将其通过 LibVips 转换为 JPG 或 Webp 格式
*/
/*
压缩图片(支持输入格式为 jpg.webp) 输出为 jpg.webp
输入内容为指定图片的原图路径
*/
+8 -85
View File
@@ -7,13 +7,10 @@ import (
"encoding/hex"
"fmt"
"github.com/google/uuid"
"github.com/h2non/filetype"
"go.uber.org/zap"
"os"
"os/exec"
"rear/internal/consts"
"rear/internal/model"
"rear/internal/utils/tools"
"rear/internal/api"
"rear/pkg/logger"
"rear/pkg/utils"
"runtime"
@@ -91,94 +88,20 @@ func (pt *PictureTask) Resume() {
func (pt *PictureTask) Run() {
pt.setStatus(StatusRunning)
// 文件是否存在
if !fileExists(pt.Path) {
logger.Error(
"指定文件不存在!",
zap.String("path", pt.Path),
)
return
}
// 初始化文件处理
imageAPI, _ := api.NewImageAPI(pt.Path)
pt.waitIfPaused()
// 读取文件
buf, err := os.ReadFile(pt.Path)
// 获取基础信息
err := imageAPI.GetExif()
if err != nil {
logger.Error(
"文件读取失败!",
zap.String("path", pt.Path),
)
return
}
pt.waitIfPaused()
// 检测照片格式
kind, err := filetype.Match(buf)
if err != nil {
logger.Error(
"文件类型匹配失败!",
zap.String("path", pt.Path),
)
return
}
// 获取原图路径
pt.waitIfPaused()
// 不同图像类型不同的处理方式
fileType := kind.Extension
logger.Info("探测到的文件类型.", zap.String("fileType", fileType))
// 读取 hash
hash, err := utils.HashUtils.HashFile(pt.Path, utils.SHA256)
if err != nil {
logger.Error(
"Hash获取失败!",
zap.String("path", pt.Path),
)
return
}
logger.Info("获取到 Hash", zap.String("hash", hash))
// 获取基本信息,如果图像的很小则不进行压缩
ctx := context.Background()
exifData, err := tools.GetExifData(ctx, pt.Path)
if err != nil {
logger.Error(
"EXIF数据获取失败!",
zap.String("path", pt.Path),
zap.Error(err),
)
return
}
// 分割 EXIF 数据
splitExifData := model.SplitExifData(exifData)
// 如果是非常规格式或 raw 则转换为 png ;如果是 PNG 则无损转换为 webp 或 jpg;如果是 webp 或 jpg 则进行压缩和其他处理
if fileType == string(consts.FormatJPG) {
//options := tools.DefaultOptions()
//options.MaxSize = 800
//options.Quality = 90
//options.Format = "jpeg"
//tools.LibVipsUtil.ProcessImage(options)
} else if fileType == string(consts.FormatWEBP) {
} else if fileType == string(consts.FormatPNG) {
} else {
}
// 图像转换后,将转换后照片信息检索判断是否有必要进行压缩
// 压缩图像 【判断图像的大小是否需要压缩】
imgWidth := splitExifData.BaseInfo.ImageWidth
imgHeight := splitExifData.BaseInfo.ImageHeight
logger.Info("图像宽度",
zap.Int("width", imgWidth),
zap.Int("height", imgHeight),
)
// 如果图像的宽度或高度小于 800,则不进行压缩
// 获取压缩图片路径
imageAPI.GetImage()
return
// 分割 Hash 路径
+19 -1
View File
@@ -35,7 +35,14 @@ func main() {
config.InitConfig()
// 日志初始化
err := logger.InitDefaultLogger()
err, logPath := getLogPath() // 获取日志路径
if err != nil {
log.Fatal("获取日志路径失败:", err)
return
}
logConfig := logger.DefaultConfig()
logConfig.LogPath = logPath
err = logger.InitDefaultLogger(logConfig)
if err != nil {
// log.Fatal 会输出错误信息并调用 os.Exit(1)
log.Fatalf("Failed to initialize logger: %v", err)
@@ -88,6 +95,17 @@ func main() {
startHttp(newContainer, newTaskContainer)
}
// 获取日志路径
func getLogPath() (error, string) {
join := filepath.Join(config.CONFIG.AppDir, config.CONFIG.PathConfig.LogPath)
err := utils.FileUtils.CreateDir(join)
if err != nil {
logger.Error("日志文件夹创建失败!", zap.String("path", join), zap.Error(err))
return err, ""
}
return nil, join
}
// 创建软件所需的缓存目录等内容
func createCachePath(dir string) {
// 缩略图目录
+12 -11
View File
@@ -22,8 +22,8 @@ const (
FatalLevel
)
// Config 日志配置
type Config struct {
// LogConfig 日志配置
type LogConfig struct {
Level LogLevel `json:"level"` // 日志级别
LogPath string `json:"log_path"` // 日志文件路径
MaxSize int `json:"max_size"` // 单个日志文件最大大小(MB)
@@ -40,14 +40,14 @@ type Config struct {
type Logger struct {
zap *zap.Logger
sugar *zap.SugaredLogger
config *Config
config *LogConfig
}
// defaultConfig 默认配置
func defaultConfig() *Config {
return &Config{
// DefaultConfig 默认配置
func DefaultConfig() *LogConfig {
return &LogConfig{
Level: InfoLevel,
LogPath: "./logs/app.log",
LogPath: "./logs",
MaxSize: 1, // 1MB
MaxBackups: 30, // 保留30个备份
MaxAge: 7, // 保留7天
@@ -60,13 +60,14 @@ func defaultConfig() *Config {
}
// NewLogger 创建新的日志器
func NewLogger(skipCaller int, config ...*Config) (*Logger, error) {
var cfg *Config
func NewLogger(skipCaller int, config ...*LogConfig) (*Logger, error) {
var cfg *LogConfig
if len(config) > 0 && config[0] != nil {
cfg = config[0]
} else {
cfg = defaultConfig()
cfg = DefaultConfig()
}
cfg.LogPath = filepath.Join(cfg.LogPath, "app.log")
// 确保日志目录存在
logDir := filepath.Dir(cfg.LogPath)
@@ -279,7 +280,7 @@ func (l *Logger) GetSugarLogger() *zap.SugaredLogger {
var defaultLogger *Logger
// InitDefaultLogger 初始化默认日志器
func InitDefaultLogger(config ...*Config) error {
func InitDefaultLogger(config ...*LogConfig) error {
var err error
defaultLogger, err = NewLogger(2, config...)
return err
+2 -2
View File
@@ -27,7 +27,7 @@ func main() {
defaultLogger.Info("Application started with default config")
// 示例2: 使用自定义配置
customConfig := &Config{
customConfig := &LogConfig{
Level: InfoLevel,
LogPath: "./logs/custom.log",
MaxSize: 2, // 2MB
@@ -91,7 +91,7 @@ func main() {
requestLogger.Infof("Request completed in %dms", 250)
// 示例8: 初始化全局默认日志器
err = InitDefaultLogger(&Config{
err = InitDefaultLogger(&LogConfig{
Level: DebugLevel,
LogPath: "./logs/global.log",
MaxSize: 1,