Optionally mirror web logs to stdout

This commit is contained in:
Arthur Barr
2021-01-06 13:52:46 +00:00
committed by Arthur J Barr
parent d2ea17ec30
commit 76070234d4
5 changed files with 68 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
/* /*
© Copyright IBM Corporation 2017, 2020 © Copyright IBM Corporation 2017, 2021
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -79,6 +79,8 @@ func formatBasic(obj map[string]interface{}) string {
if len(inserts) > 0 { if len(inserts) > 0 {
return fmt.Sprintf("%s %s [%v]\n", obj["ibm_datetime"], obj["message"], strings.Join(inserts, ", ")) return fmt.Sprintf("%s %s [%v]\n", obj["ibm_datetime"], obj["message"], strings.Join(inserts, ", "))
} }
// Convert time zone information from some logs (e.g. Liberty) for consistency
obj["ibm_datetime"] = strings.Replace(obj["ibm_datetime"].(string), "+0000", "Z", 1)
return fmt.Sprintf("%s %s\n", obj["ibm_datetime"], obj["message"]) return fmt.Sprintf("%s %s\n", obj["ibm_datetime"], obj["message"])
} }
@@ -105,6 +107,11 @@ func mirrorHTPasswdLogs(ctx context.Context, wg *sync.WaitGroup, name string, fr
return mirrorLog(ctx, wg, "/var/mqm/errors/mqhtpass.json", false, mf, true) return mirrorLog(ctx, wg, "/var/mqm/errors/mqhtpass.json", false, mf, true)
} }
// mirrorWebServerLogs starts a goroutine to mirror the contents of the Liberty web server messages.log
func mirrorWebServerLogs(ctx context.Context, wg *sync.WaitGroup, name string, fromStart bool, mf mirrorFunc) (chan error, error) {
return mirrorLog(ctx, wg, "/var/mqm/web/installations/Installation1/servers/mqweb/logs/messages.log", false, mf, true)
}
func getDebug() bool { func getDebug() bool {
debug := os.Getenv("DEBUG") debug := os.Getenv("DEBUG")
if debug == "true" || debug == "1" { if debug == "true" || debug == "1" {
@@ -124,6 +131,8 @@ func configureLogger(name string) (mirrorFunc, error) {
return nil, err return nil, err
} }
return func(msg string, isQMLog bool) bool { return func(msg string, isQMLog bool) bool {
// Check if the message is JSON
if len(msg) > 0 && msg[0] == '{' {
obj, err := processLogMessage(msg) obj, err := processLogMessage(msg)
if err == nil && isQMLog && filterQMLogMessage(obj) { if err == nil && isQMLog && filterQMLogMessage(obj) {
return false return false
@@ -133,6 +142,11 @@ func configureLogger(name string) (mirrorFunc, error) {
} else { } else {
fmt.Println(msg) fmt.Println(msg)
} }
} else {
// The log being mirrored isn't JSON, so wrap it in a simple JSON message
// MQ error logs are usually JSON, but this is useful for Liberty logs - usually expect WLP_LOGGING_MESSAGE_FORMAT=JSON to be set when mirroring Liberty logs.
fmt.Printf("{\"message\":\"%s\"}\n", msg)
}
return true return true
}, nil }, nil
case "basic": case "basic":
@@ -141,6 +155,8 @@ func configureLogger(name string) (mirrorFunc, error) {
return nil, err return nil, err
} }
return func(msg string, isQMLog bool) bool { return func(msg string, isQMLog bool) bool {
// Check if the message is JSON
if len(msg) > 0 && msg[0] == '{' {
// Parse the JSON message, and print a simplified version // Parse the JSON message, and print a simplified version
obj, err := processLogMessage(msg) obj, err := processLogMessage(msg)
if err == nil && isQMLog && filterQMLogMessage(obj) { if err == nil && isQMLog && filterQMLogMessage(obj) {
@@ -150,7 +166,11 @@ func configureLogger(name string) (mirrorFunc, error) {
log.Printf("Failed to unmarshall JSON in log message - %v", err) log.Printf("Failed to unmarshall JSON in log message - %v", err)
} else { } else {
fmt.Printf(formatBasic(obj)) fmt.Printf(formatBasic(obj))
// fmt.Printf(formatSimple(obj["ibm_datetime"].(string), obj["message"].(string))) }
} else {
// The log being mirrored isn't JSON, so just print it.
// MQ error logs are usually JSON, but this is useful for Liberty logs - usually expect WLP_LOGGING_MESSAGE_FORMAT=JSON to be set when mirroring Liberty logs.
fmt.Println(msg)
} }
return true return true
}, nil }, nil

View File

@@ -1,5 +1,5 @@
/* /*
© Copyright IBM Corporation 2017, 2020 © Copyright IBM Corporation 2017, 2021
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -195,6 +195,16 @@ func doMain() error {
return err return err
} }
} }
// Recommended to use this option in conjunction with setting WLP_LOGGING_MESSAGE_FORMAT=JSON
mirrorWebLog := os.Getenv("MQ_ENABLE_EMBEDDED_WEB_SERVER_LOG")
if mirrorWebLog == "true" || mirrorWebLog == "1" {
_, err = mirrorWebServerLogs(ctx, &wg, name, newQM, mf)
if err != nil {
logTermination(err)
return err
}
}
err = updateCommandLevel() err = updateCommandLevel()
if err != nil { if err != nil {
logTermination(err) logTermination(err)

View File

@@ -1,5 +1,5 @@
/* /*
© Copyright IBM Corporation 2018, 2019 © Copyright IBM Corporation 2018, 2021
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -117,14 +117,22 @@ func (l *Logger) log(level string, msg string) {
// Debug logs a line as debug // Debug logs a line as debug
func (l *Logger) Debug(args ...interface{}) { func (l *Logger) Debug(args ...interface{}) {
if l.debug { if l.debug {
if l.json {
l.log(debugLevel, fmt.Sprint(args...)) l.log(debugLevel, fmt.Sprint(args...))
} else {
l.log(debugLevel, "DEBUG: "+fmt.Sprint(args...))
}
} }
} }
// Debugf logs a line as debug using format specifiers // Debugf logs a line as debug using format specifiers
func (l *Logger) Debugf(format string, args ...interface{}) { func (l *Logger) Debugf(format string, args ...interface{}) {
if l.debug { if l.debug {
if l.json {
l.log(debugLevel, fmt.Sprintf(format, args...)) l.log(debugLevel, fmt.Sprintf(format, args...))
} else {
l.log(debugLevel, fmt.Sprintf("DEBUG: "+format, args...))
}
} }
} }

View File

@@ -1,7 +1,7 @@
// +build mqdev // +build mqdev
/* /*
© Copyright IBM Corporation 2018, 2019 © Copyright IBM Corporation 2018, 2021
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -82,6 +82,8 @@ func TestDevSecure(t *testing.T) {
"MQ_QMGR_NAME=" + qm, "MQ_QMGR_NAME=" + qm,
"MQ_APP_PASSWORD=" + appPassword, "MQ_APP_PASSWORD=" + appPassword,
"DEBUG=1", "DEBUG=1",
"WLP_LOGGING_MESSAGE_FORMAT=JSON",
"MQ_ENABLE_EMBEDDED_WEB_SERVER_LOG=true",
}, },
Image: imageName(), Image: imageName(),
} }

View File

@@ -1,7 +1,7 @@
// +build mqdev // +build mqdev
/* /*
© Copyright IBM Corporation 2018, 2019 © Copyright IBM Corporation 2018, 2021
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -48,8 +48,9 @@ var insecureTLSConfig *tls.Config = &tls.Config{
} }
func waitForWebReady(t *testing.T, cli *client.Client, ID string, tlsConfig *tls.Config) { func waitForWebReady(t *testing.T, cli *client.Client, ID string, tlsConfig *tls.Config) {
t.Logf("%s Waiting for web server to be ready", time.Now().Format(time.RFC3339))
httpClient := http.Client{ httpClient := http.Client{
Timeout: time.Duration(3 * time.Second), Timeout: time.Duration(10 * time.Second),
Transport: &http.Transport{ Transport: &http.Transport{
TLSClientConfig: tlsConfig, TLSClientConfig: tlsConfig,
}, },
@@ -63,13 +64,13 @@ func waitForWebReady(t *testing.T, cli *client.Client, ID string, tlsConfig *tls
case <-time.After(1 * time.Second): case <-time.After(1 * time.Second):
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
req.SetBasicAuth("admin", defaultAdminPassword) req.SetBasicAuth("admin", defaultAdminPassword)
resp, err := httpClient.Do(req.WithContext(ctx)) resp, err := httpClient.Do(req)
if err == nil && resp.StatusCode == http.StatusOK { if err == nil && resp.StatusCode == http.StatusOK {
t.Log("MQ web server is ready") t.Logf("%s MQ web server is ready", time.Now().Format(time.RFC3339))
return return
} }
case <-ctx.Done(): case <-ctx.Done():
t.Fatal("Timed out waiting for web server to become ready") t.Fatalf("%s Timed out waiting for web server to become ready", time.Now().Format(time.RFC3339))
} }
} }
} }