Skip to content

Commit

Permalink
change logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tukeJonny committed Sep 3, 2018
1 parent e605a9a commit 7016920
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
28 changes: 28 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.0.6"
19 changes: 12 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ package main
import (
"flag"
"fmt"
"log"
"strings"

log "github.com/sirupsen/logrus"
"github.com/tukejonny/mysql-warmer/mysql"
)

func init() {
log.SetFormatter(&log.JSONFormatter{})
log.SetLevel(log.DebugLevel)
}

func MySQLWarmUp(config mysql.Config) {
log.Info("[+] Start warmup ...")
client, err := mysql.NewMySQLClient(mysql.MySQLDSNParams{
Username: config.MySQL.Username,
Password: config.MySQL.Password,
Expand All @@ -21,15 +27,17 @@ func MySQLWarmUp(config mysql.Config) {
log.Fatalf("Failed to connct Mysql: %s\n", err.Error())
}

log.Info("[*] Get table list ...")
tables, err := client.GetTables()
if err != nil {
log.Fatalf("Failed to get mysql tables: %s\n", err.Error())
}

for _, table := range tables {
fmt.Printf("[+] Warming up %s table...\n", table.Name)
log.Infof("[*] Warming up %s ...\n", table.Name)
// インデックスが無ければ次のテーブルへ
if len(table.Indexes) == 0 {
log.Warnf("[!] Skip %s", table.Name)
continue
}

Expand All @@ -42,7 +50,6 @@ func MySQLWarmUp(config mysql.Config) {

for col := range colMap {
var sumStmt string
log.Println(colMap[col])
switch colMap[col] {
case "INT":
sumStmt = fmt.Sprintf("SUM(`%s`)", col)
Expand All @@ -66,12 +73,12 @@ func MySQLWarmUp(config mysql.Config) {
strings.Join(cols, ","),
)

fmt.Printf("[*] Invoke %s ...\n", stmt)
log.Info("[*] Querying ...")
_, err := client.Client.Query(stmt)
if err != nil {
log.Fatalf("Failed to exeute warmup for %s: %s", table.Name, err.Error())
}
fmt.Println(" done!")
log.Info("[+] done!")
}
}

Expand All @@ -97,6 +104,4 @@ func main() {
},
}
MySQLWarmUp(config)

fmt.Println("[+] Finish")
}
3 changes: 3 additions & 0 deletions mysql/innodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

_ "github.com/go-sql-driver/mysql"
log "github.com/sirupsen/logrus"
)

// MySQLのカラム型種別(大きく数値型、日時型、文字列型に分ける)
Expand Down Expand Up @@ -45,8 +46,10 @@ func NewMySQLClient(params MySQLDSNParams) (*MySQLClient, error) {
var dsn string
if len(params.UnixSock) > 0 {
// Unix Domain Socketが利用可能であれば、なるべく使う
log.Warn("[!] Use UNIX domain socket")
dsn = fmt.Sprintf("%s:%s@unix(%s)/%s", params.Username, params.Password, params.UnixSock, params.DbName)
} else {
log.Warn("[!] Use TCP connection")
// Unix Domain Socketが使えないならば、TCP接続
dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", params.Username, params.Password, params.Hostname, params.Port, params.DbName)
}
Expand Down

0 comments on commit 7016920

Please sign in to comment.