Skip to content

Commit

Permalink
优化错误日志输出
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiwy committed Dec 12, 2019
1 parent 5021863 commit 81a1cd6
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
*.exe
*.log
build/
script/release.go
conf.yaml
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.12
require (
github.com/gin-gonic/gin v1.4.0
github.com/jinzhu/gorm v1.9.10
github.com/pkg/sftp v1.10.1 // indirect
github.com/tdewolff/minify v2.3.6+incompatible
github.com/tdewolff/minify/v2 v2.5.0
github.com/tdewolff/parse v2.3.4+incompatible // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
Expand All @@ -75,6 +77,10 @@ github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1 h1:VasscCm72135zRysgrJDKsntdmPN+OuU3+nnHYA9wyc=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
Expand All @@ -90,6 +96,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tdewolff/minify v2.3.6+incompatible h1:2hw5/9ZvxhWLvBUnHE06gElGYz+Jv9R4Eys0XUzItYo=
github.com/tdewolff/minify v2.3.6+incompatible/go.mod h1:9Ov578KJUmAWpS6NeZwRZyT56Uf6o3Mcz9CEsg8USYs=
github.com/tdewolff/minify/v2 v2.5.0 h1:OWPdsMnomzKoL5tzgW3HK3t1zVxsHF6SlGxJjUnoMdw=
Expand All @@ -104,9 +111,12 @@ github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGr
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c h1:Vj5n4GlwjmQteupaxJ9+0FNOmBrHfq7vN4btdGoDZgI=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f h1:R423Cnkcp5JABoeemiGEPlt9tHXFfw5kvc0yqlxRPWo=
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 h1:7KByu05hhLed2MO29w7p1XfZvZ13m8mub3shuVftRs0=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff h1:+2zgJKVDVAz/BWSsuniCmU1kLCjL88Z8/kv39xCI9NQ=
golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
Expand Down
19 changes: 10 additions & 9 deletions script/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ import (
"github.com/tdewolff/minify/html"
"github.com/tdewolff/minify/js"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)

const buildDir = "./build"
const buildDir = "build"
const tempDir = buildDir + "/oktools"

const srcTplDir = "./tpl"
const srcTplDir = "tpl"
const dstTplDir = tempDir + "/tpl/"

const srcCSSDir = "./static/css"
const srcCSSDir = "static/css"
const dstCSSDir = tempDir + "/static/css/"

const srcJSDir = "./static/js"
const srcJSDir = "static/js"
const dstJSDir = tempDir + "/static/js/"

func main() {
Expand All @@ -48,7 +49,7 @@ func buildGo() {
err = os.Setenv("GOARCH", "amd64")
checkError(err)

cmd := exec.Command("go", "build", "-o", tempDir+"/oktools", "-i", "./src")
cmd := exec.Command("go", "build", "-o", tempDir+"/oktools", "-i", "src")
cmd.Stderr = os.Stderr
out, err := cmd.Output()
checkError(err)
Expand All @@ -67,9 +68,9 @@ func minifyStatic() {
}

func copyFiles() {
copyDir("./sql", tempDir+"/sql")
copyFile("./static/favicon.ico", tempDir+"/static/favicon.ico")
copyFile("./conf.yaml", tempDir+"/conf.yaml")
copyDir("sql", tempDir+"/sql")
copyFile("static/favicon.ico", tempDir+"/static/favicon.ico")
copyFile("conf.yaml", tempDir+"/conf.yaml")
}

func minifyFiles(m *minify.M, mimeType, src, dst string) {
Expand Down Expand Up @@ -192,6 +193,6 @@ func compress(file *os.File, prefix string, tw *tar.Writer) {

func checkError(err error) {
if err != nil {
panic(err)
log.Fatalln(err)
}
}
5 changes: 3 additions & 2 deletions src/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package conf
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
)

Expand Down Expand Up @@ -49,11 +50,11 @@ func init() {

data, err := ioutil.ReadFile(conf)
if err != nil {
panic(err)
log.Fatalln(err)
}

err = yaml.UnmarshalStrict(data, &Conf)
if err != nil {
panic(err)
log.Fatalln(err)
}
}
5 changes: 3 additions & 2 deletions src/global/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package global
import (
"database/sql"
"fmt"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/lib/pq"
"log"
"oktools/src/conf"
)

Expand All @@ -17,6 +18,6 @@ func init() {
"host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
dbConf.Host, dbConf.Port, dbConf.Username, dbConf.Password, dbConf.DbName))
if err != nil {
panic(err)
log.Fatalln(err)
}
}
14 changes: 6 additions & 8 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ func main() {

logfile := conf.Conf.App.LogFile
if logfile == "" {
panic("Please set the log file path!")
log.Fatalln("Please set the log file path!")
}

file, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
if err != nil {
file, err = os.Create(logfile)
if file == nil {
panic(err)
log.Fatalln(err)
}
}

defer func() {
err := file.Close()
if err != nil {
panic(err)
log.Fatalln(err)
}
}()

Expand All @@ -49,7 +49,7 @@ func main() {
go func() {
err := http.ListenAndServe(":6060", nil)
if err != nil {
panic(err)
log.Fatalln(err)
}
}()
}
Expand All @@ -70,8 +70,7 @@ func main() {
if gin.Mode() == gin.ReleaseMode {
serverChan("OkTools server stopped")
}
log.Println("Something terrible happened:", err)
panic(err)
log.Fatalln("Something terrible happened:", err)
}
}

Expand Down Expand Up @@ -105,8 +104,7 @@ func runNoTLS() {

err := e.Run(":80")
if err != nil {
log.Println("Something terrible happened:", err)
panic(err)
log.Fatalln("Something terrible happened:", err)
}
}()
}
2 changes: 1 addition & 1 deletion src/model/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,6 @@ func UpdateUsageCount() {
func init() {
_, err := loadTools()
if err != nil {
panic(err)
log.Fatalln(err)
}
}

0 comments on commit 81a1cd6

Please sign in to comment.