Skip to content

Commit f4676bf

Browse files
committed
implement a logging interface
1 parent dcc3e90 commit f4676bf

10 files changed

Lines changed: 313 additions & 227 deletions

File tree

go/base/context.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package base
77

88
import (
99
"fmt"
10+
"github.com/outbrain/golib/log"
1011
"os"
1112
"regexp"
1213
"strings"
@@ -214,6 +215,25 @@ type MigrationContext struct {
214215
ForceTmpTableName string
215216

216217
recentBinlogCoordinates mysql.BinlogCoordinates
218+
219+
Log Logger
220+
}
221+
222+
type Logger interface {
223+
Debug(args ...interface{})
224+
Debugf(format string, args ...interface{})
225+
Info(args ...interface{})
226+
Infof(format string, args ...interface{})
227+
Warning(args ...interface{}) error
228+
Warningf(format string, args ...interface{}) error
229+
Error(args ...interface{}) error
230+
Errorf(format string, args ...interface{}) error
231+
Errore(err error) error
232+
Fatal(args ...interface{}) error
233+
Fatalf(format string, args ...interface{}) error
234+
Fatale(err error) error
235+
SetLevel(level log.LogLevel)
236+
SetPrintStackTrace(printStackTraceFlag bool)
217237
}
218238

219239
type ContextConfig struct {
@@ -248,6 +268,7 @@ func NewMigrationContext() *MigrationContext {
248268
pointOfInterestTimeMutex: &sync.Mutex{},
249269
ColumnRenameMap: make(map[string]string),
250270
PanicAbort: make(chan error),
271+
Log: NewDefaultLogger(),
251272
}
252273
}
253274

go/base/default_logger.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package base
2+
3+
import (
4+
"github.com/outbrain/golib/log"
5+
)
6+
7+
type simpleLogger struct{}
8+
9+
func NewDefaultLogger() *simpleLogger {
10+
return &simpleLogger{}
11+
}
12+
13+
func (*simpleLogger) Debug(args ...interface{}) {
14+
log.Debug(args[0].(string), args[1:])
15+
return
16+
}
17+
18+
func (*simpleLogger) Debugf(format string, args ...interface{}) {
19+
log.Debugf(format, args...)
20+
return
21+
}
22+
23+
func (*simpleLogger) Info(args ...interface{}) {
24+
log.Info(args[0].(string), args[1:])
25+
return
26+
}
27+
28+
func (*simpleLogger) Infof(format string, args ...interface{}) {
29+
log.Infof(format, args...)
30+
return
31+
}
32+
33+
func (*simpleLogger) Warning(args ...interface{}) error {
34+
return log.Warning(args[0].(string), args[1:])
35+
}
36+
37+
func (*simpleLogger) Warningf(format string, args ...interface{}) error {
38+
return log.Warningf(format, args...)
39+
}
40+
41+
func (*simpleLogger) Error(args ...interface{}) error {
42+
return log.Error(args[0].(string), args[1:])
43+
}
44+
45+
func (*simpleLogger) Errorf(format string, args ...interface{}) error {
46+
return log.Errorf(format, args...)
47+
}
48+
49+
func (*simpleLogger) Errore(err error) error {
50+
return log.Errore(err)
51+
}
52+
53+
func (*simpleLogger) Fatal(args ...interface{}) error {
54+
return log.Fatal(args[0].(string), args[1:])
55+
}
56+
57+
func (*simpleLogger) Fatalf(format string, args ...interface{}) error {
58+
return log.Fatalf(format, args...)
59+
}
60+
61+
func (*simpleLogger) Fatale(err error) error {
62+
return log.Fatale(err)
63+
}
64+
65+
func (*simpleLogger) SetLevel(level log.LogLevel) {
66+
log.SetLevel(level)
67+
return
68+
}
69+
70+
func (*simpleLogger) SetPrintStackTrace(printStackTraceFlag bool) {
71+
log.SetPrintStackTrace(printStackTraceFlag)
72+
return
73+
}

go/base/utils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
gosql "database/sql"
1616
"github.com/github/gh-ost/go/mysql"
17-
"github.com/outbrain/golib/log"
1817
)
1918

2019
var (
@@ -86,7 +85,7 @@ func ValidateConnection(db *gosql.DB, connectionConfig *mysql.ConnectionConfig,
8685
}
8786

8887
if connectionConfig.Key.Port == port || (extraPort > 0 && connectionConfig.Key.Port == extraPort) {
89-
log.Infof("connection validated on %+v", connectionConfig.Key)
88+
migrationContext.Log.Infof("connection validated on %+v", connectionConfig.Key)
9089
return version, nil
9190
} else if extraPort == 0 {
9291
return "", fmt.Errorf("Unexpected database port reported: %+v", port)

go/cmd/gh-ost/main.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func acceptSignals(migrationContext *base.MigrationContext) {
3131
for sig := range c {
3232
switch sig {
3333
case syscall.SIGHUP:
34-
log.Infof("Received SIGHUP. Reloading configuration")
34+
migrationContext.Log.Infof("Received SIGHUP. Reloading configuration")
3535
if err := migrationContext.ReadConfigFile(); err != nil {
3636
log.Errore(err)
3737
} else {
@@ -156,69 +156,69 @@ func main() {
156156
return
157157
}
158158

159-
log.SetLevel(log.ERROR)
159+
migrationContext.Log.SetLevel(log.ERROR)
160160
if *verbose {
161-
log.SetLevel(log.INFO)
161+
migrationContext.Log.SetLevel(log.INFO)
162162
}
163163
if *debug {
164-
log.SetLevel(log.DEBUG)
164+
migrationContext.Log.SetLevel(log.DEBUG)
165165
}
166166
if *stack {
167-
log.SetPrintStackTrace(*stack)
167+
migrationContext.Log.SetPrintStackTrace(*stack)
168168
}
169169
if *quiet {
170170
// Override!!
171-
log.SetLevel(log.ERROR)
171+
migrationContext.Log.SetLevel(log.ERROR)
172172
}
173173

174174
if migrationContext.DatabaseName == "" {
175-
log.Fatalf("--database must be provided and database name must not be empty")
175+
migrationContext.Log.Fatalf("--database must be provided and database name must not be empty")
176176
}
177177
if migrationContext.OriginalTableName == "" {
178-
log.Fatalf("--table must be provided and table name must not be empty")
178+
migrationContext.Log.Fatalf("--table must be provided and table name must not be empty")
179179
}
180180
if migrationContext.AlterStatement == "" {
181-
log.Fatalf("--alter must be provided and statement must not be empty")
181+
migrationContext.Log.Fatalf("--alter must be provided and statement must not be empty")
182182
}
183183
migrationContext.Noop = !(*executeFlag)
184184
if migrationContext.AllowedRunningOnMaster && migrationContext.TestOnReplica {
185-
log.Fatalf("--allow-on-master and --test-on-replica are mutually exclusive")
185+
migrationContext.Log.Fatalf("--allow-on-master and --test-on-replica are mutually exclusive")
186186
}
187187
if migrationContext.AllowedRunningOnMaster && migrationContext.MigrateOnReplica {
188-
log.Fatalf("--allow-on-master and --migrate-on-replica are mutually exclusive")
188+
migrationContext.Log.Fatalf("--allow-on-master and --migrate-on-replica are mutually exclusive")
189189
}
190190
if migrationContext.MigrateOnReplica && migrationContext.TestOnReplica {
191-
log.Fatalf("--migrate-on-replica and --test-on-replica are mutually exclusive")
191+
migrationContext.Log.Fatalf("--migrate-on-replica and --test-on-replica are mutually exclusive")
192192
}
193193
if migrationContext.SwitchToRowBinlogFormat && migrationContext.AssumeRBR {
194-
log.Fatalf("--switch-to-rbr and --assume-rbr are mutually exclusive")
194+
migrationContext.Log.Fatalf("--switch-to-rbr and --assume-rbr are mutually exclusive")
195195
}
196196
if migrationContext.TestOnReplicaSkipReplicaStop {
197197
if !migrationContext.TestOnReplica {
198-
log.Fatalf("--test-on-replica-skip-replica-stop requires --test-on-replica to be enabled")
198+
migrationContext.Log.Fatalf("--test-on-replica-skip-replica-stop requires --test-on-replica to be enabled")
199199
}
200-
log.Warning("--test-on-replica-skip-replica-stop enabled. We will not stop replication before cut-over. Ensure you have a plugin that does this.")
200+
migrationContext.Log.Warning("--test-on-replica-skip-replica-stop enabled. We will not stop replication before cut-over. Ensure you have a plugin that does this.")
201201
}
202202
if migrationContext.CliMasterUser != "" && migrationContext.AssumeMasterHostname == "" {
203-
log.Fatalf("--master-user requires --assume-master-host")
203+
migrationContext.Log.Fatalf("--master-user requires --assume-master-host")
204204
}
205205
if migrationContext.CliMasterPassword != "" && migrationContext.AssumeMasterHostname == "" {
206-
log.Fatalf("--master-password requires --assume-master-host")
206+
migrationContext.Log.Fatalf("--master-password requires --assume-master-host")
207207
}
208208
if migrationContext.TLSCACertificate != "" && !migrationContext.UseTLS {
209-
log.Fatalf("--ssl-ca requires --ssl")
209+
migrationContext.Log.Fatalf("--ssl-ca requires --ssl")
210210
}
211211
if migrationContext.TLSCertificate != "" && !migrationContext.UseTLS {
212-
log.Fatalf("--ssl-cert requires --ssl")
212+
migrationContext.Log.Fatalf("--ssl-cert requires --ssl")
213213
}
214214
if migrationContext.TLSKey != "" && !migrationContext.UseTLS {
215-
log.Fatalf("--ssl-key requires --ssl")
215+
migrationContext.Log.Fatalf("--ssl-key requires --ssl")
216216
}
217217
if migrationContext.TLSAllowInsecure && !migrationContext.UseTLS {
218-
log.Fatalf("--ssl-allow-insecure requires --ssl")
218+
migrationContext.Log.Fatalf("--ssl-allow-insecure requires --ssl")
219219
}
220220
if *replicationLagQuery != "" {
221-
log.Warningf("--replication-lag-query is deprecated")
221+
migrationContext.Log.Warningf("--replication-lag-query is deprecated")
222222
}
223223

224224
switch *cutOver {
@@ -227,19 +227,19 @@ func main() {
227227
case "two-step":
228228
migrationContext.CutOverType = base.CutOverTwoStep
229229
default:
230-
log.Fatalf("Unknown cut-over: %s", *cutOver)
230+
migrationContext.Log.Fatalf("Unknown cut-over: %s", *cutOver)
231231
}
232232
if err := migrationContext.ReadConfigFile(); err != nil {
233-
log.Fatale(err)
233+
migrationContext.Log.Fatale(err)
234234
}
235235
if err := migrationContext.ReadThrottleControlReplicaKeys(*throttleControlReplicas); err != nil {
236-
log.Fatale(err)
236+
migrationContext.Log.Fatale(err)
237237
}
238238
if err := migrationContext.ReadMaxLoad(*maxLoad); err != nil {
239-
log.Fatale(err)
239+
migrationContext.Log.Fatale(err)
240240
}
241241
if err := migrationContext.ReadCriticalLoad(*criticalLoad); err != nil {
242-
log.Fatale(err)
242+
migrationContext.Log.Fatale(err)
243243
}
244244
if migrationContext.ServeSocketFile == "" {
245245
migrationContext.ServeSocketFile = fmt.Sprintf("/tmp/gh-ost.%s.%s.sock", migrationContext.DatabaseName, migrationContext.OriginalTableName)
@@ -248,7 +248,7 @@ func main() {
248248
fmt.Println("Password:")
249249
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
250250
if err != nil {
251-
log.Fatale(err)
251+
migrationContext.Log.Fatale(err)
252252
}
253253
migrationContext.CliPassword = string(bytePassword)
254254
}
@@ -262,13 +262,13 @@ func main() {
262262
migrationContext.SetDefaultNumRetries(*defaultRetries)
263263
migrationContext.ApplyCredentials()
264264
if err := migrationContext.SetupTLS(); err != nil {
265-
log.Fatale(err)
265+
migrationContext.Log.Fatale(err)
266266
}
267267
if err := migrationContext.SetCutOverLockTimeoutSeconds(*cutOverLockTimeoutSeconds); err != nil {
268-
log.Errore(err)
268+
migrationContext.Log.Errore(err)
269269
}
270270
if err := migrationContext.SetExponentialBackoffMaxInterval(*exponentialBackoffMaxInterval); err != nil {
271-
log.Errore(err)
271+
migrationContext.Log.Errore(err)
272272
}
273273

274274
log.Infof("starting gh-ost %+v", AppVersion)
@@ -278,7 +278,7 @@ func main() {
278278
err := migrator.Migrate()
279279
if err != nil {
280280
migrator.ExecOnFailureHook()
281-
log.Fatale(err)
281+
migrationContext.Log.Fatale(err)
282282
}
283283
fmt.Fprintf(os.Stdout, "# Done\n")
284284
}

0 commit comments

Comments
 (0)