-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Structured logging #818
Merged
Merged
Structured logging #818
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
db673db
Update dependency
tothszabi 44d21fa
Use new logger
tothszabi 507e52e
Add temp version number
tothszabi 66276e7
Update go-utils/advancedlog
godrei c698ef6
Update go-utils/advancedlog
godrei 69a9f6a
Code cleanup
godrei 6c2a050
Migrate remaining go-utils/log usages to advancedlog
godrei e560229
Migrate remaining fmt usages to advancedlog
godrei 6e03594
Code cleanup
godrei fe95ed8
Update version.go
godrei 2955edd
Test json and console log compatibility
godrei 7a390a8
Update log_format_test_bitrise.yml
godrei ffecfe1
Update dependency
tothszabi 3d7ac47
Use new logger
tothszabi e4f9a25
Update go-utils/advancedlog
godrei f24d250
Update go-utils/advancedlog
godrei 9a5b2ef
Code cleanup
godrei 9ccb081
Migrate remaining fmt usages to advancedlog
godrei f332916
Code cleanup
godrei 5e48dfd
Rebase fixes
godrei 3b44192
Pull latest go-utils/v2
godrei cd28017
Fix rebase
godrei 1951210
Fix rebase
godrei 4af5871
Fix rebase
godrei acdc71a
Move advancedlog package to bitrise
godrei c3b015b
Rename advancedlog package to log
godrei 74a395f
Update paths.go
godrei 97f486f
Hangdetector log with timestamp
godrei 47552d6
Add producer id to step logs
godrei fe9076b
Code cleanup
godrei 5949e56
Console logger tests
godrei c6e37a7
PR fixes
godrei e064449
Constructor for utilsLogAdapter
godrei 9e2dda2
Introduce new LoggerOpts
godrei 1d591c3
Update run_test.go
godrei 25c3d94
Remove configs.LoggerType
godrei 2ef64c3
PR fixes
godrei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package integration | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"os/exec" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConsoleLogCanBeRestoredFromJSONLog(t *testing.T) { | ||
consoleLog := createConsoleLog(t) | ||
jsonLog := createJSONleLog(t) | ||
convertedConsoleLog := restoreConsoleLog(t, jsonLog) | ||
require.Equal(t, replaceVariableParts(consoleLog), replaceVariableParts(convertedConsoleLog)) | ||
} | ||
|
||
func createConsoleLog(t *testing.T) string { | ||
execCmd := exec.Command(binPath(), "setup") | ||
outBytes, err := execCmd.CombinedOutput() | ||
require.NoError(t, err, string(outBytes)) | ||
|
||
cmd := exec.Command(binPath(), "run", "fail_test", "--config", "log_format_test_bitrise.yml") | ||
out, err := cmd.CombinedOutput() | ||
require.EqualError(t, err, "exit status 1") | ||
return string(out) | ||
} | ||
|
||
func createJSONleLog(t *testing.T) []byte { | ||
execCmd := exec.Command(binPath(), "setup") | ||
outBytes, err := execCmd.CombinedOutput() | ||
require.NoError(t, err, string(outBytes)) | ||
|
||
cmd := exec.Command(binPath(), "run", "fail_test", "--config", "log_format_test_bitrise.yml", "--output-format", "json") | ||
out, err := cmd.CombinedOutput() | ||
require.EqualError(t, err, "exit status 1") | ||
return out | ||
} | ||
|
||
func restoreConsoleLog(t *testing.T, log []byte) string { | ||
type Log struct { | ||
Message string `json:"message"` | ||
Level string `json:"level"` | ||
} | ||
|
||
var consoleLog string | ||
lines := bytes.Split(log, []byte("\n")) | ||
for _, line := range lines { | ||
if string(line) == "" { | ||
continue | ||
} | ||
var log Log | ||
err := json.Unmarshal(line, &log) | ||
require.NoError(t, err, string(line)) | ||
consoleLog += createLogMsg(log.Level, log.Message) | ||
} | ||
return consoleLog | ||
} | ||
|
||
var levelToANSIColorCode = map[level]ansiColorCode{ | ||
errorLevel: redCode, | ||
warnLevel: yellowCode, | ||
infoLevel: blueCode, | ||
doneLevel: greenCode, | ||
debugLevel: magentaCode, | ||
} | ||
|
||
func createLogMsg(lvl string, message string) string { | ||
color := levelToANSIColorCode[level(lvl)] | ||
if color != "" { | ||
return addColor(color, message) | ||
} | ||
return message | ||
} | ||
|
||
func addColor(color ansiColorCode, msg string) string { | ||
return string(color) + msg + string(resetCode) | ||
} | ||
|
||
type level string | ||
|
||
const ( | ||
errorLevel level = "error" | ||
warnLevel level = "warn" | ||
infoLevel level = "info" | ||
doneLevel level = "done" | ||
normalLevel level = "normal" | ||
debugLevel level = "debug" | ||
) | ||
|
||
type ansiColorCode string | ||
|
||
const ( | ||
redCode ansiColorCode = "\x1b[31;1m" | ||
yellowCode ansiColorCode = "\x1b[33;1m" | ||
blueCode ansiColorCode = "\x1b[34;1m" | ||
greenCode ansiColorCode = "\x1b[32;1m" | ||
magentaCode ansiColorCode = "\x1b[35;1m" | ||
resetCode ansiColorCode = "\x1b[0m" | ||
) | ||
|
||
func replaceVariableParts(line string) string { | ||
timeRegexp := regexp.MustCompile(`(\| time: .+\|)`) | ||
line = timeRegexp.ReplaceAllString(line, "[REPLACED]") | ||
|
||
runTimeRegexp := regexp.MustCompile(`(\| .+ sec \|)`) | ||
line = runTimeRegexp.ReplaceAllString(line, "[REPLACED]") | ||
|
||
totalRunTimeRegexp := regexp.MustCompile(`(\| Total runtime: .+ \|)`) | ||
line = totalRunTimeRegexp.ReplaceAllString(line, "[REPLACED]") | ||
|
||
return line | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
format_version: "11" | ||
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git | ||
project_type: other | ||
|
||
workflows: | ||
fail_test: | ||
title: Fails | ||
description: Workflow will fail | ||
steps: | ||
- script: | ||
title: Success | ||
inputs: | ||
- content: |- | ||
set -ex | ||
exit 0 | ||
- script@1.1: | ||
title: Fail wit exit code 2 | ||
inputs: | ||
- content: |- | ||
set -ex | ||
exit 2 | ||
- git::https://github.com/bitrise-steplib/steps-script.git@master: | ||
title: Skippable fail with exit code 2 | ||
is_always_run: true | ||
is_skippable: true | ||
inputs: | ||
- content: |- | ||
set -ex | ||
exit 2 | ||
- script: | ||
title: Skipping success | ||
is_always_run: false | ||
inputs: | ||
- content: |- | ||
set -ex | ||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package analytics | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/bitrise-io/bitrise/log" | ||
) | ||
|
||
var ( | ||
analyticsServerURL = "https://bitrise-step-analytics.herokuapp.com" | ||
httpClient = http.Client{ | ||
Timeout: time.Second * 5, | ||
} | ||
) | ||
|
||
// LogMessage sends the log message to the configured analytics server | ||
func LogMessage(logLevel string, stepID string, tag string, data map[string]interface{}, format string, v ...interface{}) { | ||
// Entry represents a line in a log | ||
e := struct { | ||
LogLevel string `json:"log_level"` | ||
Message string `json:"message"` | ||
Data map[string]interface{} `json:"data"` | ||
}{ | ||
Message: fmt.Sprintf(format, v...), | ||
LogLevel: logLevel, | ||
} | ||
|
||
e.Data = make(map[string]interface{}) | ||
for k, v := range data { | ||
e.Data[k] = v | ||
} | ||
|
||
if v, ok := e.Data["step_id"]; ok { | ||
log.Printf("internal logger: data.step_id (%s) will be overriden with (%s) ", v, stepID) | ||
} | ||
if v, ok := e.Data["tag"]; ok { | ||
log.Printf("internal logger: data.tag (%s) will be overriden with (%s) ", v, tag) | ||
} | ||
|
||
e.Data["step_id"] = stepID | ||
e.Data["tag"] = tag | ||
|
||
var b bytes.Buffer | ||
if err := json.NewEncoder(&b).Encode(e); err != nil { | ||
return | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
defer cancel() | ||
|
||
req, err := http.NewRequest(http.MethodPost, analyticsServerURL+"/logs", &b) | ||
if err != nil { | ||
// deliberately not writing into users log | ||
return | ||
} | ||
req = req.WithContext(ctx) | ||
req.Header.Add("Content-Type", "application/json") | ||
|
||
if _, err := httpClient.Do(req); err != nil { | ||
// deliberately not writing into users log | ||
return | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package analytics | ||
|
||
import ( | ||
"github.com/bitrise-io/bitrise/log" | ||
) | ||
|
||
// utilsLogAdapter extends the bitrise/log.Logger to meet the go-utils/v2/log.Logger interface. | ||
type utilsLogAdapter struct { | ||
debug bool | ||
log.Logger | ||
} | ||
|
||
func newUtilsLogAdapter() utilsLogAdapter { | ||
opts := log.GetGlobalLoggerOpts() | ||
return utilsLogAdapter{ | ||
Logger: log.NewLogger(opts), | ||
debug: opts.DebugLogEnabled, | ||
} | ||
} | ||
|
||
func (l *utilsLogAdapter) TInfof(format string, v ...interface{}) { | ||
log.Infof(format, v...) | ||
} | ||
func (l *utilsLogAdapter) TWarnf(format string, v ...interface{}) { | ||
log.Warnf(format, v...) | ||
} | ||
func (l *utilsLogAdapter) TPrintf(format string, v ...interface{}) { | ||
log.Printf(format, v...) | ||
} | ||
func (l *utilsLogAdapter) TDonef(format string, v ...interface{}) { | ||
log.Donef(format, v...) | ||
} | ||
func (l *utilsLogAdapter) TDebugf(format string, v ...interface{}) { | ||
if !l.debug { | ||
return | ||
} | ||
log.Debugf(format, v...) | ||
} | ||
func (l *utilsLogAdapter) TErrorf(format string, v ...interface{}) { | ||
log.Errorf(format, v...) | ||
} | ||
func (l *utilsLogAdapter) Println() { | ||
log.Print() | ||
} | ||
func (l *utilsLogAdapter) EnableDebugLog(enable bool) { | ||
l.debug = enable | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The legacyLogger uses the global log functions from the log package. Can you tell me why this
log.Logger
variable is necessary here because as I understand it it will not be used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log.Logger implements the remaining methods defined by the
go-utils/v2/log.Logger
interface.This way, we extend the new (
bitrise-io/log.Logger
) with the missing methods, so thatlegacyLogger
can be passed to the tracker constructor.