Skip to content

Commit 954d091

Browse files
committed
feat: add new formatter log output for github
Signed-off-by: Felipe Zipitria <felipe.zipitria@owasp.org>
1 parent 9c4e24e commit 954d091

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

cmd/flag_types.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"path/filepath"
1212

1313
"github.com/rs/zerolog"
14+
15+
loggerConfig "github.com/coreruleset/crs-toolchain/logger"
1416
)
1517

1618
// The types in this file satisfy the interface of pflag.Value.
@@ -38,7 +40,10 @@ func (o *outputType) String() string {
3840

3941
func (o *outputType) Set(value string) error {
4042
switch value {
41-
case string(text), string(gitHub):
43+
case string(gitHub):
44+
logger = loggerConfig.SetGithubOutput()
45+
fallthrough
46+
case string(text):
4247
rootValues.output = outputType(value)
4348
return nil
4449
default:

logger/logger.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package logger
55

66
import (
7+
"fmt"
78
"os"
89

910
"github.com/rs/zerolog"
@@ -12,7 +13,27 @@ import (
1213

1314
const DefaultLogLevel zerolog.Level = zerolog.InfoLevel
1415

16+
var consoleOutput = zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "03:04:05"}
17+
1518
func init() {
16-
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "03:04:05"}).With().Caller().Logger()
19+
log.Logger = log.Output(consoleOutput).With().Caller().Logger()
1720
zerolog.SetGlobalLevel(DefaultLogLevel)
1821
}
22+
23+
// SetGithubOutput changes the standard logging format to be compatible with GitHub's.
24+
// See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-creating-an-annotation-for-an-error
25+
// Levels on github are:
26+
// - debug, notice, error, warning
27+
// Another possibility is to add the following strings between the level and the message:
28+
// file={name},line={line},endLine={endLine},title={title}
29+
func SetGithubOutput() zerolog.Logger {
30+
// the following formatlevel func might need to rename levels to match with github naming
31+
consoleOutput.FormatLevel = func(i interface{}) string {
32+
return fmt.Sprintf("::%s", i)
33+
}
34+
consoleOutput.FormatMessage = func(i interface{}) string {
35+
return fmt.Sprintf("::%s", i)
36+
}
37+
consoleOutput.PartsExclude = []string{zerolog.TimestampFieldName}
38+
return zerolog.New(consoleOutput).With().Logger()
39+
}

0 commit comments

Comments
 (0)