-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
501 additions
and
31 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package logger | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
// LogLevelDecoder deserializes the log level from a config string. | ||
type LevelDecoder zerolog.Level | ||
|
||
// Names of log levels for use in encoding/decoding from strings. | ||
const ( | ||
llPanic = "panic" | ||
llFatal = "fatal" | ||
llError = "error" | ||
llWarn = "warn" | ||
llInfo = "info" | ||
llDebug = "debug" | ||
llTrace = "trace" | ||
) | ||
|
||
// Decode implements confire Decoder interface. | ||
func (ll *LevelDecoder) Decode(value string) error { | ||
value = strings.TrimSpace(strings.ToLower(value)) | ||
switch value { | ||
case llPanic: | ||
*ll = LevelDecoder(zerolog.PanicLevel) | ||
case llFatal: | ||
*ll = LevelDecoder(zerolog.FatalLevel) | ||
case llError: | ||
*ll = LevelDecoder(zerolog.ErrorLevel) | ||
case llWarn: | ||
*ll = LevelDecoder(zerolog.WarnLevel) | ||
case llInfo: | ||
*ll = LevelDecoder(zerolog.InfoLevel) | ||
case llDebug: | ||
*ll = LevelDecoder(zerolog.DebugLevel) | ||
case llTrace: | ||
*ll = LevelDecoder(zerolog.TraceLevel) | ||
default: | ||
return fmt.Errorf("unknown log level %q", value) | ||
} | ||
return nil | ||
} | ||
|
||
// Encode converts the loglevel into a string for use in YAML and JSON | ||
func (ll *LevelDecoder) Encode() (string, error) { | ||
switch zerolog.Level(*ll) { | ||
case zerolog.PanicLevel: | ||
return llPanic, nil | ||
case zerolog.FatalLevel: | ||
return llFatal, nil | ||
case zerolog.ErrorLevel: | ||
return llError, nil | ||
case zerolog.WarnLevel: | ||
return llWarn, nil | ||
case zerolog.InfoLevel: | ||
return llInfo, nil | ||
case zerolog.DebugLevel: | ||
return llDebug, nil | ||
case zerolog.TraceLevel: | ||
return llTrace, nil | ||
default: | ||
return "", fmt.Errorf("unknown log level %d", ll) | ||
} | ||
} | ||
|
||
func (ll LevelDecoder) String() string { | ||
ls, _ := ll.Encode() | ||
return ls | ||
} | ||
|
||
// UnmarshalJSON implements json.Unmarshaler | ||
func (ll *LevelDecoder) UnmarshalJSON(data []byte) error { | ||
var ls string | ||
if err := json.Unmarshal(data, &ls); err != nil { | ||
return err | ||
} | ||
return ll.Decode(ls) | ||
} | ||
|
||
// MarshalJSON implements json.Marshaler | ||
func (ll LevelDecoder) MarshalJSON() ([]byte, error) { | ||
ls, err := ll.Encode() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return json.Marshal(ls) | ||
} |
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,85 @@ | ||
package logger_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/require" | ||
"github.com/trisacrypto/courier/pkg/logger" | ||
) | ||
|
||
func TestLevelDecoder(t *testing.T) { | ||
testTable := []struct { | ||
value string | ||
expected zerolog.Level | ||
}{ | ||
{ | ||
"panic", zerolog.PanicLevel, | ||
}, | ||
{ | ||
"FATAL", zerolog.FatalLevel, | ||
}, | ||
{ | ||
"Error", zerolog.ErrorLevel, | ||
}, | ||
{ | ||
" warn ", zerolog.WarnLevel, | ||
}, | ||
{ | ||
"iNFo", zerolog.InfoLevel, | ||
}, | ||
{ | ||
"debug", zerolog.DebugLevel, | ||
}, | ||
{ | ||
"trace", zerolog.TraceLevel, | ||
}, | ||
} | ||
|
||
// Test valid cases | ||
for _, testCase := range testTable { | ||
var level logger.LevelDecoder | ||
err := level.Decode(testCase.value) | ||
require.NoError(t, err) | ||
require.Equal(t, testCase.expected, zerolog.Level(level)) | ||
} | ||
|
||
// Test error case | ||
var level logger.LevelDecoder | ||
err := level.Decode("notalevel") | ||
require.EqualError(t, err, `unknown log level "notalevel"`) | ||
} | ||
|
||
func TestUnmarshaler(t *testing.T) { | ||
type Config struct { | ||
Level logger.LevelDecoder | ||
} | ||
|
||
var jsonConf Config | ||
err := json.Unmarshal([]byte(`{"level": "panic"}`), &jsonConf) | ||
require.NoError(t, err, "could not unmarshal level decoder in json file") | ||
require.Equal(t, zerolog.PanicLevel, zerolog.Level(jsonConf.Level)) | ||
} | ||
|
||
func TestMarshaler(t *testing.T) { | ||
confs := []struct { | ||
Level logger.LevelDecoder `yaml:"level" json:"level"` | ||
}{ | ||
{logger.LevelDecoder(zerolog.PanicLevel)}, | ||
{logger.LevelDecoder(zerolog.FatalLevel)}, | ||
{logger.LevelDecoder(zerolog.ErrorLevel)}, | ||
{logger.LevelDecoder(zerolog.WarnLevel)}, | ||
{logger.LevelDecoder(zerolog.InfoLevel)}, | ||
{logger.LevelDecoder(zerolog.DebugLevel)}, | ||
{logger.LevelDecoder(zerolog.TraceLevel)}, | ||
} | ||
|
||
for _, conf := range confs { | ||
data, err := json.Marshal(conf) | ||
require.NoError(t, err, "could not marshal data into json") | ||
require.Equal(t, []byte(fmt.Sprintf(`{"level":%q}`, &conf.Level)), data) | ||
} | ||
|
||
} |
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,40 @@ | ||
package logger | ||
|
||
import "github.com/rs/zerolog" | ||
|
||
type severityGCP string | ||
|
||
const ( | ||
GCPAlertLevel severityGCP = "ALERT" | ||
GCPCriticalLevel severityGCP = "CRITICAL" | ||
GCPErrorLevel severityGCP = "ERROR" | ||
GCPWarningLevel severityGCP = "WARNING" | ||
GCPInfoLevel severityGCP = "INFO" | ||
GCPDebugLevel severityGCP = "DEBUG" | ||
|
||
GCPFieldKeySeverity = "severity" | ||
GCPFieldKeyMsg = "message" | ||
GCPFieldKeyTime = "time" | ||
) | ||
|
||
var ( | ||
zerologToGCPLevel = map[zerolog.Level]severityGCP{ | ||
zerolog.PanicLevel: GCPAlertLevel, | ||
zerolog.FatalLevel: GCPCriticalLevel, | ||
zerolog.ErrorLevel: GCPErrorLevel, | ||
zerolog.WarnLevel: GCPWarningLevel, | ||
zerolog.InfoLevel: GCPInfoLevel, | ||
zerolog.DebugLevel: GCPDebugLevel, | ||
zerolog.TraceLevel: GCPDebugLevel, | ||
} | ||
) | ||
|
||
// SeverityHook adds GCP severity levels to zerolog output log messages. | ||
type SeverityHook struct{} | ||
|
||
// Run implements the zerolog.Hook interface. | ||
func (h SeverityHook) Run(e *zerolog.Event, level zerolog.Level, msg string) { | ||
if level != zerolog.NoLevel { | ||
e.Str(GCPFieldKeySeverity, string(zerologToGCPLevel[level])) | ||
} | ||
} |
Oops, something went wrong.