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) {
// if convert fails, return error; success nil
log.Debugf("target: %s with quality of %f", path.Base(p1), quality)
var buf bytes.Buffer
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))
} else if strings.Contains(contentType, "gif") {
// TODO: need to support animated webp
log.Warn("Gif support is not perfect!")
img, _ = gif.Decode(bytes.NewReader(data))
}
if img == nil {
msg := "image file " + path.Base(p1) + " is corrupted or not supported"
log.Info(msg)
log.Debug(msg)
err = errors.New(msg)
ChanErr(c)
return
}
if err = webp.Encode(&buf, img, &webp.Options{Lossless: false, Quality: quality}); err != nil {
log.Info(err)
log.Error(err)
ChanErr(c)
return
}
if err = ioutil.WriteFile(p2, buf.Bytes(), 0755); err != nil {
log.Warn(err)
log.Error(err)
ChanErr(c)
return
}
if Log {
log.Warn("Save to %s ok\n", p2)
log.Info("Save to " + p2 + " ok!\n")
}
ChanErr(c)

View File

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

View File

@ -3,20 +3,23 @@ package main
import (
"bufio"
"fmt"
"log"
"os"
"path"
"path/filepath"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
)
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)
char, _, _ := reader.ReadRune() //y Y enter
// 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)
for i := 0; i < jobs; i++ {
finishChan <- 0
@ -42,7 +45,7 @@ func PrefetchImages(confImgPath string, ExhaustPath string, QUALITY string) {
return nil
})
if err != nil {
log.Println(err)
log.Debug(err)
}
}

View File

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

View File

@ -4,17 +4,18 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"runtime"
log "github.com/sirupsen/logrus"
)
func autoUpdate() {
defer func() {
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"`
}
var res Result
log.Debugf("Requesting to %s", api)
resp1, _ := http.Get(api)
data1, _ := ioutil.ReadAll(resp1.Body)
_ = json.Unmarshal(data1, &res)
var gitVersion = res.TagName
if gitVersion > version {
log.Printf("Time to update! New version %s found!", gitVersion)
log.Infof("Time to update! New version %s found", gitVersion)
} else {
log.Println("No new version found.")
log.Debug("No new version found.")
return
}
@ -41,11 +42,10 @@ func autoUpdate() {
filename += ".exe"
}
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)
if resp.StatusCode != 200 {
log.Printf("%s-%s not found on release. "+
"Contact developers to supply your version", runtime.GOOS, runtime.GOARCH)
log.Debug("%s-%s not found on release.", runtime.GOOS, runtime.GOARCH)
return
}
data, _ := ioutil.ReadAll(resp.Body)
@ -54,7 +54,7 @@ func autoUpdate() {
err := ioutil.WriteFile(path.Join("update", filename), data, 0755)
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()
}

View File

@ -8,9 +8,8 @@ import (
"path"
"runtime"
log "github.com/sirupsen/logrus"
"github.com/gofiber/fiber"
log "github.com/sirupsen/logrus"
)
type Config struct {
@ -38,7 +37,7 @@ const sampleConfig = `
"QUALITY": "80",
"IMG_PATH": "/path/to/pics",
"EXHAUST_PATH": "",
"ALLOWED_TYPES": ["jpg","png","jpeg","bmp","gif"]
"ALLOWED_TYPES": ["jpg","png","jpeg","bmp"]
}`
const sampleSystemd = `
[Unit]
@ -56,7 +55,6 @@ ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=3s
[Install]
WantedBy=multi-user.target`
@ -80,18 +78,28 @@ func init() {
flag.BoolVar(&dumpSystemd, "dump-systemd", false, "Print sample systemd service file.")
flag.BoolVar(&verboseMode, "v", false, "Verbose, print out debug info.")
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() {
// Logrus
log.SetOutput(os.Stdout)
if verboseMode {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.WarnLevel)
}
// process cli params
if dumpConfig {
fmt.Println(sampleConfig)
@ -128,8 +136,7 @@ func main() {
ListenAddress := HOST + ":" + PORT
// Server Info
ServerInfo := "WebP Server " + version + " is running at " + ListenAddress
fmt.Println(ServerInfo)
log.Infof("WebP Server %s %s", version, ListenAddress)
app.Get("/*", Convert(confImgPath, ExhaustPath, AllowedTypes, QUALITY))
app.Listen(ListenAddress)