feat: add json logger output (#1026)
* feat: add json logger output This will allow to format log output as json. This is helpful in cases where act is not executed on a 'local' machine. * refactor: use runner config Using the runner config to configure logging is cleaner. Co-authored-by: Casey Lee <cplee@nektos.com>
This commit is contained in:
@@ -157,9 +157,14 @@ func TestAddmaskUsemask(t *testing.T) {
|
||||
|
||||
a := assert.New(t)
|
||||
|
||||
config := &Config{
|
||||
Secrets: map[string]string{},
|
||||
InsecureSecrets: false,
|
||||
}
|
||||
|
||||
re := captureOutput(t, func() {
|
||||
ctx := context.Background()
|
||||
ctx = WithJobLogger(ctx, "testjob", map[string]string{}, false, &rc.Masks)
|
||||
ctx = WithJobLogger(ctx, "testjob", config, &rc.Masks)
|
||||
|
||||
handler := rc.commandHandler(ctx)
|
||||
handler("::add-mask::secret\n")
|
||||
|
@@ -38,14 +38,22 @@ func init() {
|
||||
}
|
||||
|
||||
// WithJobLogger attaches a new logger to context that is aware of steps
|
||||
func WithJobLogger(ctx context.Context, jobName string, secrets map[string]string, insecureSecrets bool, masks *[]string) context.Context {
|
||||
func WithJobLogger(ctx context.Context, jobName string, config *Config, masks *[]string) context.Context {
|
||||
mux.Lock()
|
||||
defer mux.Unlock()
|
||||
formatter := new(stepLogFormatter)
|
||||
formatter.color = colors[nextColor%len(colors)]
|
||||
formatter.secrets = secrets
|
||||
formatter.insecureSecrets = insecureSecrets
|
||||
formatter.masks = masks
|
||||
|
||||
var formatter logrus.Formatter
|
||||
if config.JSONLogger {
|
||||
formatter = &jobLogJSONFormatter{
|
||||
formatter: &logrus.JSONFormatter{},
|
||||
masker: valueMasker(config.InsecureSecrets, config.Secrets, masks),
|
||||
}
|
||||
} else {
|
||||
formatter = &jobLogFormatter{
|
||||
color: colors[nextColor%len(colors)],
|
||||
masker: valueMasker(config.InsecureSecrets, config.Secrets, masks),
|
||||
}
|
||||
}
|
||||
|
||||
nextColor++
|
||||
|
||||
@@ -64,30 +72,39 @@ func WithJobLogger(ctx context.Context, jobName string, secrets map[string]strin
|
||||
return common.WithLogger(ctx, rtn)
|
||||
}
|
||||
|
||||
type stepLogFormatter struct {
|
||||
color int
|
||||
secrets map[string]string
|
||||
insecureSecrets bool
|
||||
masks *[]string
|
||||
type entryProcessor func(entry *logrus.Entry) *logrus.Entry
|
||||
|
||||
func valueMasker(insecureSecrets bool, secrets map[string]string, masks *[]string) entryProcessor {
|
||||
return func(entry *logrus.Entry) *logrus.Entry {
|
||||
if insecureSecrets {
|
||||
return entry
|
||||
}
|
||||
|
||||
for _, v := range secrets {
|
||||
if v != "" {
|
||||
entry.Message = strings.ReplaceAll(entry.Message, v, "***")
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range *masks {
|
||||
if v != "" {
|
||||
entry.Message = strings.ReplaceAll(entry.Message, v, "***")
|
||||
}
|
||||
}
|
||||
|
||||
return entry
|
||||
}
|
||||
}
|
||||
|
||||
func (f *stepLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
type jobLogFormatter struct {
|
||||
color int
|
||||
masker entryProcessor
|
||||
}
|
||||
|
||||
func (f *jobLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
b := &bytes.Buffer{}
|
||||
|
||||
// Replace any secrets in the entry if insecure-secrets flag is not used
|
||||
if !f.insecureSecrets {
|
||||
for _, v := range f.secrets {
|
||||
if v != "" {
|
||||
entry.Message = strings.ReplaceAll(entry.Message, v, "***")
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range *f.masks {
|
||||
if v != "" {
|
||||
entry.Message = strings.ReplaceAll(entry.Message, v, "***")
|
||||
}
|
||||
}
|
||||
}
|
||||
entry = f.masker(entry)
|
||||
|
||||
if f.isColored(entry) {
|
||||
f.printColored(b, entry)
|
||||
@@ -99,7 +116,7 @@ func (f *stepLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
return b.Bytes(), nil
|
||||
}
|
||||
|
||||
func (f *stepLogFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry) {
|
||||
func (f *jobLogFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry) {
|
||||
entry.Message = strings.TrimSuffix(entry.Message, "\n")
|
||||
jobName := entry.Data["job"]
|
||||
|
||||
@@ -112,7 +129,7 @@ func (f *stepLogFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry) {
|
||||
}
|
||||
}
|
||||
|
||||
func (f *stepLogFormatter) print(b *bytes.Buffer, entry *logrus.Entry) {
|
||||
func (f *jobLogFormatter) print(b *bytes.Buffer, entry *logrus.Entry) {
|
||||
entry.Message = strings.TrimSuffix(entry.Message, "\n")
|
||||
jobName := entry.Data["job"]
|
||||
|
||||
@@ -125,7 +142,7 @@ func (f *stepLogFormatter) print(b *bytes.Buffer, entry *logrus.Entry) {
|
||||
}
|
||||
}
|
||||
|
||||
func (f *stepLogFormatter) isColored(entry *logrus.Entry) bool {
|
||||
func (f *jobLogFormatter) isColored(entry *logrus.Entry) bool {
|
||||
isColored := checkIfTerminal(entry.Logger.Out)
|
||||
|
||||
if force, ok := os.LookupEnv("CLICOLOR_FORCE"); ok && force != "0" {
|
||||
@@ -147,3 +164,12 @@ func checkIfTerminal(w io.Writer) bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
type jobLogJSONFormatter struct {
|
||||
masker entryProcessor
|
||||
formatter *logrus.JSONFormatter
|
||||
}
|
||||
|
||||
func (f *jobLogJSONFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
return f.formatter.Format(f.masker(entry))
|
||||
}
|
||||
|
@@ -31,6 +31,7 @@ type Config struct {
|
||||
ForcePull bool // force pulling of the image, even if already present
|
||||
ForceRebuild bool // force rebuilding local docker image action
|
||||
LogOutput bool // log the output from docker run
|
||||
JSONLogger bool // use json or text logger
|
||||
Env map[string]string // env for containers
|
||||
Secrets map[string]string // list of secrets
|
||||
InsecureSecrets bool // switch hiding output when printing to terminal
|
||||
@@ -164,7 +165,7 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
|
||||
}
|
||||
|
||||
return nil
|
||||
})(common.WithJobErrorContainer(WithJobLogger(ctx, jobName, rc.Config.Secrets, rc.Config.InsecureSecrets, &rc.Masks)))
|
||||
})(common.WithJobErrorContainer(WithJobLogger(ctx, jobName, rc.Config, &rc.Masks)))
|
||||
})
|
||||
}
|
||||
pipeline = append(pipeline, common.NewParallelExecutor(maxParallel, stageExecutor...))
|
||||
|
Reference in New Issue
Block a user