-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update dependency * Use new logger * Add temp version number * Update go-utils/advancedlog * Update go-utils/advancedlog * Code cleanup * Migrate remaining go-utils/log usages to advancedlog * Migrate remaining fmt usages to advancedlog * Code cleanup * Update version.go * Test json and console log compatibility * Update log_format_test_bitrise.yml * Update dependency * Use new logger * Update go-utils/advancedlog * Update go-utils/advancedlog * Code cleanup * Migrate remaining fmt usages to advancedlog * Code cleanup * Rebase fixes * Pull latest go-utils/v2 * Fix rebase * Fix rebase * Fix rebase * Move advancedlog package to bitrise * Rename advancedlog package to log * Update paths.go * Hangdetector log with timestamp * Add producer id to step logs * Code cleanup * Console logger tests * PR fixes * Constructor for utilsLogAdapter * Introduce new LoggerOpts * Update run_test.go * Remove configs.LoggerType * PR fixes Co-authored-by: Szabolcs Toth <[email protected]>
- Loading branch information
Showing
79 changed files
with
2,029 additions
and
428 deletions.
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 | ||
- [email protected]: | ||
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.