optimize log

This commit is contained in:
n0vad3v 2020-03-06 22:02:01 +08:00 committed by Benny~
parent 92d1715762
commit ff0ec84d82
No known key found for this signature in database
GPG Key ID: 6CD0DBDA5235D481
6 changed files with 58 additions and 38 deletions

View File

@ -19,6 +19,8 @@ import (
func WebpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err error) { func WebpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err error) {
// if convert fails, return error; success nil // if convert fails, return error; success nil
log.Debugf("target: %s with quality of %f", path.Base(p1), quality)
var buf bytes.Buffer var buf bytes.Buffer
var img image.Image var img image.Image
@ -37,30 +39,31 @@ func WebpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err erro
img, _ = bmp.Decode(bytes.NewReader(data)) img, _ = bmp.Decode(bytes.NewReader(data))
} else if strings.Contains(contentType, "gif") { } else if strings.Contains(contentType, "gif") {
// TODO: need to support animated webp // TODO: need to support animated webp
log.Warn("Gif support is not perfect!")
img, _ = gif.Decode(bytes.NewReader(data)) img, _ = gif.Decode(bytes.NewReader(data))
} }
if img == nil { if img == nil {
msg := "image file " + path.Base(p1) + " is corrupted or not supported" msg := "image file " + path.Base(p1) + " is corrupted or not supported"
log.Info(msg) log.Debug(msg)
err = errors.New(msg) err = errors.New(msg)
ChanErr(c) ChanErr(c)
return return
} }
if err = webp.Encode(&buf, img, &webp.Options{Lossless: false, Quality: quality}); err != nil { if err = webp.Encode(&buf, img, &webp.Options{Lossless: false, Quality: quality}); err != nil {
log.Info(err) log.Error(err)
ChanErr(c) ChanErr(c)
return return
} }
if err = ioutil.WriteFile(p2, buf.Bytes(), 0755); err != nil { if err = ioutil.WriteFile(p2, buf.Bytes(), 0755); err != nil {
log.Warn(err) log.Error(err)
ChanErr(c) ChanErr(c)
return return
} }
if Log { if Log {
log.Warn("Save to %s ok\n", p2) log.Info("Save to " + p2 + " ok!\n")
} }
ChanErr(c) ChanErr(c)

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
log "github.com/sirupsen/logrus"
"net/http" "net/http"
"os" "os"
"path" "path"
@ -45,7 +46,7 @@ func GenWebpAbs(RawImagePath string, ExhaustPath string, ImgFilename string, req
// get file mod time // get file mod time
STAT, err := os.Stat(RawImagePath) STAT, err := os.Stat(RawImagePath)
if err != nil { if err != nil {
fmt.Println(err.Error()) log.Error(err.Error())
} }
ModifiedTime := STAT.ModTime().Unix() ModifiedTime := STAT.ModTime().Unix()
// webpFilename: abc.jpg.png -> abc.jpg.png1582558990.webp // webpFilename: abc.jpg.png -> abc.jpg.png1582558990.webp

View File

@ -3,20 +3,23 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"log"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
log "github.com/sirupsen/logrus"
) )
func PrefetchImages(confImgPath string, ExhaustPath string, QUALITY string) { func PrefetchImages(confImgPath string, ExhaustPath string, QUALITY string) {
fmt.Println(`Prefetch will convert all your images to webp, it may take some time and consume a lot of CPU resource. Do you want to proceed(Y/n)`) fmt.Println(`Prefetch will convert all your images to webp,
it may take some time and consume a lot of CPU resource. Do you want to proceed(Y/n)`)
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
char, _, _ := reader.ReadRune() //y Y enter char, _, _ := reader.ReadRune() //y Y enter
// maximum ongoing prefetch is depending on your core of CPU // maximum ongoing prefetch is depending on your core of CPU
log.Printf("Prefetching using %d cores", jobs) log.Infof("Prefetching using %d cores", jobs)
var finishChan = make(chan int, jobs) var finishChan = make(chan int, jobs)
for i := 0; i < jobs; i++ { for i := 0; i < jobs; i++ {
finishChan <- 0 finishChan <- 0
@ -42,7 +45,7 @@ func PrefetchImages(confImgPath string, ExhaustPath string, QUALITY string) {
return nil return nil
}) })
if err != nil { if err != nil {
log.Println(err) log.Debug(err)
} }
} }

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"fmt" log "github.com/sirupsen/logrus"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -22,9 +22,11 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
UA := c.Get("User-Agent") UA := c.Get("User-Agent")
if strings.Contains(UA, "Safari") && !strings.Contains(UA, "Chrome") && if strings.Contains(UA, "Safari") && !strings.Contains(UA, "Chrome") &&
!strings.Contains(UA, "Firefox") { !strings.Contains(UA, "Firefox") {
log.Info("A Safari use has arrived...")
c.SendFile(RawImageAbs) c.SendFile(RawImageAbs)
return return
} }
log.Debugf("Incoming connection from %s@%s with %s", UA, c.IP(), ImgFilename)
// check ext // check ext
// TODO: may remove this function. Check in Nginx. // TODO: may remove this function. Check in Nginx.
@ -40,14 +42,18 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
} }
} }
if !allowed { if !allowed {
c.Send("File extension not allowed!") msg := "File extension not allowed!"
log.Warnf("%s %s", msg, ImgFilename)
c.Send(msg)
c.SendStatus(403) c.SendStatus(403)
return return
} }
// Check the original image for existence, // Check the original image for existence,
if !ImageExists(RawImageAbs) { if !ImageExists(RawImageAbs) {
c.Send("Image not found!") msg := "Image not found!"
c.Send(msg)
log.Warn(msg)
c.SendStatus(404) c.SendStatus(404)
return return
} }
@ -63,7 +69,7 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
destHalfFile := path.Clean(path.Join(WebpAbsPath, path.Dir(reqURI), ImgFilename)) destHalfFile := path.Clean(path.Join(WebpAbsPath, path.Dir(reqURI), ImgFilename))
matches, err := filepath.Glob(destHalfFile + "*") matches, err := filepath.Glob(destHalfFile + "*")
if err != nil { if err != nil {
fmt.Println(err.Error()) log.Error(err.Error())
} else { } else {
// /home/webp_server/exhaust/path/to/tsuki.jpg.1582558100.webp <- older ones will be removed // /home/webp_server/exhaust/path/to/tsuki.jpg.1582558100.webp <- older ones will be removed
// /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp <- keep the latest one // /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp <- keep the latest one
@ -80,7 +86,7 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
err = WebpEncoder(RawImageAbs, WebpAbsPath, float32(q), verboseMode, nil) err = WebpEncoder(RawImageAbs, WebpAbsPath, float32(q), verboseMode, nil)
if err != nil { if err != nil {
fmt.Println(err) log.Error(err)
c.SendStatus(400) c.SendStatus(400)
c.Send("Bad file!") c.Send("Bad file!")
return return

View File

@ -4,17 +4,18 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"os" "os"
"path" "path"
"runtime" "runtime"
log "github.com/sirupsen/logrus"
) )
func autoUpdate() { func autoUpdate() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
log.Println("Download error.", err) log.Error("Download error.", err)
} }
}() }()
@ -23,16 +24,16 @@ func autoUpdate() {
TagName string `json:"tag_name"` TagName string `json:"tag_name"`
} }
var res Result var res Result
log.Debugf("Requesting to %s", api)
resp1, _ := http.Get(api) resp1, _ := http.Get(api)
data1, _ := ioutil.ReadAll(resp1.Body) data1, _ := ioutil.ReadAll(resp1.Body)
_ = json.Unmarshal(data1, &res) _ = json.Unmarshal(data1, &res)
var gitVersion = res.TagName var gitVersion = res.TagName
if gitVersion > version { if gitVersion > version {
log.Printf("Time to update! New version %s found!", gitVersion) log.Infof("Time to update! New version %s found", gitVersion)
} else { } else {
log.Println("No new version found.") log.Debug("No new version found.")
return return
} }
@ -41,11 +42,10 @@ func autoUpdate() {
filename += ".exe" filename += ".exe"
} }
var releaseUrl = "https://github.com/webp-sh/webp_server_go/releases/latest/download/" + filename var releaseUrl = "https://github.com/webp-sh/webp_server_go/releases/latest/download/" + filename
log.Println("Downloading binary...") log.Info("Downloading binary to update...")
resp, _ := http.Get(releaseUrl) resp, _ := http.Get(releaseUrl)
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
log.Printf("%s-%s not found on release. "+ log.Debug("%s-%s not found on release.", runtime.GOOS, runtime.GOARCH)
"Contact developers to supply your version", runtime.GOOS, runtime.GOARCH)
return return
} }
data, _ := ioutil.ReadAll(resp.Body) data, _ := ioutil.ReadAll(resp.Body)
@ -54,7 +54,7 @@ func autoUpdate() {
err := ioutil.WriteFile(path.Join("update", filename), data, 0755) err := ioutil.WriteFile(path.Join("update", filename), data, 0755)
if err == nil { if err == nil {
log.Println("Update complete. Please find your binary from update directory.") log.Info("Update complete. Please find your binary from update directory.")
} }
_ = resp.Body.Close() _ = resp.Body.Close()
} }

View File

@ -8,9 +8,8 @@ import (
"path" "path"
"runtime" "runtime"
log "github.com/sirupsen/logrus"
"github.com/gofiber/fiber" "github.com/gofiber/fiber"
log "github.com/sirupsen/logrus"
) )
type Config struct { type Config struct {
@ -38,7 +37,7 @@ const sampleConfig = `
"QUALITY": "80", "QUALITY": "80",
"IMG_PATH": "/path/to/pics", "IMG_PATH": "/path/to/pics",
"EXHAUST_PATH": "", "EXHAUST_PATH": "",
"ALLOWED_TYPES": ["jpg","png","jpeg","bmp","gif"] "ALLOWED_TYPES": ["jpg","png","jpeg","bmp"]
}` }`
const sampleSystemd = ` const sampleSystemd = `
[Unit] [Unit]
@ -56,7 +55,6 @@ ExecReload=/bin/kill -HUP $MAINPID
Restart=always Restart=always
RestartSec=3s RestartSec=3s
[Install] [Install]
WantedBy=multi-user.target` WantedBy=multi-user.target`
@ -80,18 +78,28 @@ func init() {
flag.BoolVar(&dumpSystemd, "dump-systemd", false, "Print sample systemd service file.") flag.BoolVar(&dumpSystemd, "dump-systemd", false, "Print sample systemd service file.")
flag.BoolVar(&verboseMode, "v", false, "Verbose, print out debug info.") flag.BoolVar(&verboseMode, "v", false, "Verbose, print out debug info.")
flag.Parse() flag.Parse()
// Logrus
log.SetOutput(os.Stdout)
log.SetReportCaller(true)
Formatter := &log.TextFormatter{
EnvironmentOverrideColors: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
return fmt.Sprintf("[%s()]", f.Function), ""
},
}
log.SetFormatter(Formatter)
if verboseMode {
log.SetLevel(log.DebugLevel)
log.Debug("Debug mode is enable!")
} else {
log.SetLevel(log.InfoLevel)
}
} }
func main() { func main() {
// Logrus
log.SetOutput(os.Stdout)
if verboseMode {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.WarnLevel)
}
// process cli params // process cli params
if dumpConfig { if dumpConfig {
fmt.Println(sampleConfig) fmt.Println(sampleConfig)
@ -128,8 +136,7 @@ func main() {
ListenAddress := HOST + ":" + PORT ListenAddress := HOST + ":" + PORT
// Server Info // Server Info
ServerInfo := "WebP Server " + version + " is running at " + ListenAddress log.Infof("WebP Server %s %s", version, ListenAddress)
fmt.Println(ServerInfo)
app.Get("/*", Convert(confImgPath, ExhaustPath, AllowedTypes, QUALITY)) app.Get("/*", Convert(confImgPath, ExhaustPath, AllowedTypes, QUALITY))
app.Listen(ListenAddress) app.Listen(ListenAddress)