diff --git a/Gopkg.lock b/Gopkg.lock index cac84aa..1f25efd 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -17,12 +17,40 @@ revision = "a0583e0143b1624142adab07e0e97fe106d99561" version = "v1.3.0" +[[projects]] + digest = "1:d867dfa6751c8d7a435821ad3b736310c2ed68945d05b50fb9d23aee0540c8cc" + name = "github.com/sirupsen/logrus" + packages = ["."] + pruneopts = "UT" + revision = "3e01752db0189b9157070a0e1668a620f9a85da2" + version = "v1.0.6" + +[[projects]] + branch = "master" + digest = "1:3f3a05ae0b95893d90b9b3b5afdb79a9b3d96e4e36e099d841ae602e4aca0da8" + name = "golang.org/x/crypto" + packages = ["ssh/terminal"] + pruneopts = "UT" + revision = "182538f80094b6a8efaade63a8fd8e0d9d5843dd" + +[[projects]] + branch = "master" + digest = "1:629034aef6b53eb8ea737e6d82cb5402907e2757fbab5ddf5e74c08b4c6473af" + name = "golang.org/x/sys" + packages = [ + "unix", + "windows", + ] + pruneopts = "UT" + revision = "fa5fdf94c78965f1aa8423f0cc50b8b8d728b05a" + [solve-meta] analyzer-name = "dep" analyzer-version = 1 input-imports = [ "github.com/BurntSushi/toml", "github.com/go-sql-driver/mysql", + "github.com/sirupsen/logrus", ] solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 98621d5..85df0c5 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -36,3 +36,7 @@ [prune] go-tests = true unused-packages = true + +[[constraint]] + name = "github.com/sirupsen/logrus" + version = "1.0.6" diff --git a/main.go b/main.go index 6ffebd3..f297567 100644 --- a/main.go +++ b/main.go @@ -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, @@ -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 } @@ -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) @@ -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!") } } @@ -97,6 +104,4 @@ func main() { }, } MySQLWarmUp(config) - - fmt.Println("[+] Finish") } diff --git a/mysql/innodb.go b/mysql/innodb.go index e5485dc..c9fc9f4 100644 --- a/mysql/innodb.go +++ b/mysql/innodb.go @@ -7,6 +7,7 @@ import ( "strings" _ "github.com/go-sql-driver/mysql" + log "github.com/sirupsen/logrus" ) // MySQLのカラム型種別(大きく数値型、日時型、文字列型に分ける) @@ -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) }