Skip to content

Commit

Permalink
Merge pull request #7 from ajainc/feature/lazy_encode
Browse files Browse the repository at this point in the history
lazy encode
  • Loading branch information
sambaiz authored Nov 5, 2019
2 parents 07f5326 + e0faf75 commit 7e3404a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 56 deletions.
4 changes: 2 additions & 2 deletions logevent_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type FormatLogEvent struct {
}

// Encode implements LogEvent.Encode
func (event FormatLogEvent) Encode(metadata *LogEventMetadata) []byte {
func (event *FormatLogEvent) Encode(metadata *LogEventMetadata) []byte {
// delegate to Text log Event
return TextLogEvent{Event: fmt.Sprintf(event.format, event.args...),}.Encode(metadata)
return (&TextLogEvent{Event: fmt.Sprintf(event.format, event.args...),}).Encode(metadata)
}
2 changes: 1 addition & 1 deletion logevent_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type JsonLogEvent struct {
}

// Encode is implementation of LogEvent.Encode
func (jsonLogEvent JsonLogEvent) Encode(data *LogEventMetadata) []byte {
func (jsonLogEvent *JsonLogEvent) Encode(data *LogEventMetadata) []byte {

if data == nil {
// encode json
Expand Down
2 changes: 1 addition & 1 deletion logevent_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type TextLogEvent struct {
}

// Encode implements LogEvent.Encode
func (logEvent TextLogEvent) Encode(metadata *LogEventMetadata) []byte {
func (logEvent *TextLogEvent) Encode(metadata *LogEventMetadata) []byte {
if metadata != nil {

data := metadata.GetLogLevel() + " " +
Expand Down
2 changes: 1 addition & 1 deletion logevent_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestTextLogEvent_Encode(t *testing.T) {
return "[timestamp]"
}
expected := `[TRACE] [timestamp] defaultLogger logevent_text_test.go(11) test`
buf := TextLogEvent{Event: "test"}.Encode(metadata)
buf := (&TextLogEvent{Event: "test"}).Encode(metadata)
assert.Equal(t, expected, string(buf))
}()
}
Expand Down
99 changes: 50 additions & 49 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Logger struct {
}

// doAppendIfLevelEnabled
func (logger *Logger) doAppendIfLevelEnabled(event []byte, level LogLevel) {
func (logger *Logger) doAppendIfLevelEnabled(logEvent LogEvent, metadata *LogEventMetadata, level LogLevel) {

// recover
defer func(writer io.Writer) {
Expand All @@ -62,6 +62,7 @@ func (logger *Logger) doAppendIfLevelEnabled(event []byte, level LogLevel) {
}(os.Stderr)

if appenders, ok := logger.levelAppender[level]; ok {
event := logEvent.Encode(metadata)
for _, appender := range appenders {
appender.Write(event)
}
Expand All @@ -83,59 +84,59 @@ func (logger *Logger) newMetadata(level LogLevel) LogEventMetadata {
func (logger *Logger) Trace(string string) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_TRACE)
logger.doAppendIfLevelEnabled(TextLogEvent{Event: string}.Encode(&metadata), LogLevel_TRACE)
logger.doAppendIfLevelEnabled(&TextLogEvent{Event: string}, &metadata, LogLevel_TRACE)
} else {
logger.doAppendIfLevelEnabled(TextLogEvent{Event: string}.Encode(nil), LogLevel_TRACE)
logger.doAppendIfLevelEnabled(&TextLogEvent{Event: string}, nil, LogLevel_TRACE)
}
}

// Debug calls specified appender to print string.
func (logger *Logger) Debug(string string) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_DEBUG)
logger.doAppendIfLevelEnabled(TextLogEvent{Event: string}.Encode(&metadata), LogLevel_DEBUG)
logger.doAppendIfLevelEnabled(&TextLogEvent{Event: string}, &metadata, LogLevel_DEBUG)
} else {
logger.doAppendIfLevelEnabled(TextLogEvent{Event: string}.Encode(nil), LogLevel_DEBUG)
logger.doAppendIfLevelEnabled(&TextLogEvent{Event: string}, nil, LogLevel_DEBUG)
}
}

// Info calls specified appender to print string.
func (logger *Logger) Info(string string) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_INFO)
logger.doAppendIfLevelEnabled(TextLogEvent{Event: string}.Encode(&metadata), LogLevel_INFO)
logger.doAppendIfLevelEnabled(&TextLogEvent{Event: string}, &metadata, LogLevel_INFO)
} else {
logger.doAppendIfLevelEnabled(TextLogEvent{Event: string}.Encode(nil), LogLevel_INFO)
logger.doAppendIfLevelEnabled(&TextLogEvent{Event: string}, nil, LogLevel_INFO)
}
}

// Warn calls specified appender to print string.
func (logger *Logger) Warn(string string) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_WARN)
logger.doAppendIfLevelEnabled(TextLogEvent{Event: string}.Encode(&metadata), LogLevel_WARN)
logger.doAppendIfLevelEnabled(&TextLogEvent{Event: string}, &metadata, LogLevel_WARN)
} else {
logger.doAppendIfLevelEnabled(TextLogEvent{Event: string}.Encode(nil), LogLevel_WARN)
logger.doAppendIfLevelEnabled(&TextLogEvent{Event: string},nil, LogLevel_WARN)
}
}

// Error calls specified appender to print string.
func (logger *Logger) Error(string string) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_ERROR)
logger.doAppendIfLevelEnabled(TextLogEvent{Event: string}.Encode(&metadata), LogLevel_ERROR)
logger.doAppendIfLevelEnabled(&TextLogEvent{Event: string}, &metadata, LogLevel_ERROR)
} else {
logger.doAppendIfLevelEnabled(TextLogEvent{Event: string}.Encode(nil), LogLevel_ERROR)
logger.doAppendIfLevelEnabled(&TextLogEvent{Event: string}, nil, LogLevel_ERROR)
}
}

// Fatal calls specified appender to print string.
func (logger *Logger) Fatal(string string) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_FATAL)
logger.doAppendIfLevelEnabled(TextLogEvent{Event: string}.Encode(&metadata), LogLevel_FATAL)
logger.doAppendIfLevelEnabled(&TextLogEvent{Event: string}, &metadata, LogLevel_FATAL)
} else {
logger.doAppendIfLevelEnabled(TextLogEvent{Event: string}.Encode(nil), LogLevel_FATAL)
logger.doAppendIfLevelEnabled(&TextLogEvent{Event: string}, nil, LogLevel_FATAL)
}

logger.Close()
Expand All @@ -146,59 +147,59 @@ func (logger *Logger) Fatal(string string) {
func (logger *Logger) Tracef(format string, args ...interface{}) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_TRACE)
logger.doAppendIfLevelEnabled(FormatLogEvent{format: format, args: args,}.Encode(&metadata), LogLevel_TRACE)
logger.doAppendIfLevelEnabled(&FormatLogEvent{format: format, args: args,}, &metadata, LogLevel_TRACE)
} else {
logger.doAppendIfLevelEnabled(FormatLogEvent{format: format, args: args,}.Encode(nil), LogLevel_TRACE)
logger.doAppendIfLevelEnabled(&FormatLogEvent{format: format, args: args,}, nil, LogLevel_TRACE)
}
}

// Debugf encodes according to format specifier and calls specified appender to print.
func (logger *Logger) Debugf(format string, args ...interface{}) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_DEBUG)
logger.doAppendIfLevelEnabled(FormatLogEvent{format: format, args: args,}.Encode(&metadata), LogLevel_DEBUG)
logger.doAppendIfLevelEnabled(&FormatLogEvent{format: format, args: args,}, &metadata, LogLevel_DEBUG)
} else {
logger.doAppendIfLevelEnabled(FormatLogEvent{format: format, args: args,}.Encode(nil), LogLevel_DEBUG)
logger.doAppendIfLevelEnabled(&FormatLogEvent{format: format, args: args,}, nil, LogLevel_DEBUG)
}
}

// Infof encodes according to format specifier and calls specified appender to print.
func (logger *Logger) Infof(format string, args ...interface{}) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_INFO)
logger.doAppendIfLevelEnabled(FormatLogEvent{format: format, args: args,}.Encode(&metadata), LogLevel_INFO)
logger.doAppendIfLevelEnabled(&FormatLogEvent{format: format, args: args,}, &metadata, LogLevel_INFO)
} else {
logger.doAppendIfLevelEnabled(FormatLogEvent{format: format, args: args,}.Encode(nil), LogLevel_INFO)
logger.doAppendIfLevelEnabled(&FormatLogEvent{format: format, args: args,}, nil, LogLevel_INFO)
}
}

// Warnf encodes according to format specifier and calls specified appender to print.
func (logger *Logger) Warnf(format string, args ...interface{}) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_WARN)
logger.doAppendIfLevelEnabled(FormatLogEvent{format: format, args: args,}.Encode(&metadata), LogLevel_WARN)
logger.doAppendIfLevelEnabled(&FormatLogEvent{format: format, args: args,}, &metadata, LogLevel_WARN)
} else {
logger.doAppendIfLevelEnabled(FormatLogEvent{format: format, args: args,}.Encode(nil), LogLevel_WARN)
logger.doAppendIfLevelEnabled(&FormatLogEvent{format: format, args: args,}, nil, LogLevel_WARN)
}
}

// Errorf encodes according to format specifier and calls specified appender to print.
func (logger *Logger) Errorf(format string, args ...interface{}) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_ERROR)
logger.doAppendIfLevelEnabled(FormatLogEvent{format: format, args: args,}.Encode(&metadata), LogLevel_ERROR)
logger.doAppendIfLevelEnabled(&FormatLogEvent{format: format, args: args,}, &metadata, LogLevel_ERROR)
} else {
logger.doAppendIfLevelEnabled(FormatLogEvent{format: format, args: args,}.Encode(nil), LogLevel_ERROR)
logger.doAppendIfLevelEnabled(&FormatLogEvent{format: format, args: args,}, nil, LogLevel_ERROR)
}
}

// Fatalf encodes according to format specifier and calls specified appender to print.
func (logger *Logger) Fatalf(format string, args ...interface{}) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_FATAL)
logger.doAppendIfLevelEnabled(FormatLogEvent{format: format, args: args,}.Encode(&metadata), LogLevel_FATAL)
logger.doAppendIfLevelEnabled(&FormatLogEvent{format: format, args: args,}, &metadata, LogLevel_FATAL)
} else {
logger.doAppendIfLevelEnabled(FormatLogEvent{format: format, args: args,}.Encode(nil), LogLevel_FATAL)
logger.doAppendIfLevelEnabled(&FormatLogEvent{format: format, args: args,}, nil, LogLevel_FATAL)
}

logger.Close()
Expand All @@ -209,59 +210,59 @@ func (logger *Logger) Fatalf(format string, args ...interface{}) {
func (logger *Logger) Tracej(obj interface{}) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_TRACE)
logger.doAppendIfLevelEnabled(JsonLogEvent{event: obj,}.Encode(&metadata), LogLevel_TRACE)
logger.doAppendIfLevelEnabled(&JsonLogEvent{event: obj,}, &metadata, LogLevel_TRACE)
} else {
logger.doAppendIfLevelEnabled(JsonLogEvent{event: obj,}.Encode(nil), LogLevel_TRACE)
logger.doAppendIfLevelEnabled(&JsonLogEvent{event: obj,}, nil, LogLevel_TRACE)
}
}

// Debugj encodes as Json binary and calls specified appender to print.
func (logger *Logger) Debugj(obj interface{}) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_DEBUG)
logger.doAppendIfLevelEnabled(JsonLogEvent{event: obj,}.Encode(&metadata), LogLevel_DEBUG)
logger.doAppendIfLevelEnabled(&JsonLogEvent{event: obj,}, &metadata, LogLevel_DEBUG)
} else {
logger.doAppendIfLevelEnabled(JsonLogEvent{event: obj,}.Encode(nil), LogLevel_DEBUG)
logger.doAppendIfLevelEnabled(&JsonLogEvent{event: obj,}, nil, LogLevel_DEBUG)
}
}

// Infoj encodes as Json binary and calls specified appender to print.
func (logger *Logger) Infoj(obj interface{}) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_INFO)
logger.doAppendIfLevelEnabled(JsonLogEvent{event: obj,}.Encode(&metadata), LogLevel_INFO)
logger.doAppendIfLevelEnabled(&JsonLogEvent{event: obj,}, &metadata, LogLevel_INFO)
} else {
logger.doAppendIfLevelEnabled(JsonLogEvent{event: obj,}.Encode(nil), LogLevel_INFO)
logger.doAppendIfLevelEnabled(&JsonLogEvent{event: obj,}, nil, LogLevel_INFO)
}
}

// Warnj encodes as Json binary and calls specified appender to print.
func (logger *Logger) Warnj(obj interface{}) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_WARN)
logger.doAppendIfLevelEnabled(JsonLogEvent{event: obj,}.Encode(&metadata), LogLevel_WARN)
logger.doAppendIfLevelEnabled(&JsonLogEvent{event: obj,}, &metadata, LogLevel_WARN)
} else {
logger.doAppendIfLevelEnabled(JsonLogEvent{event: obj,}.Encode(nil), LogLevel_WARN)
logger.doAppendIfLevelEnabled(&JsonLogEvent{event: obj,}, nil, LogLevel_WARN)
}
}

// Errorj encodes as Json binary and calls specified appender to print.
func (logger *Logger) Errorj(obj interface{}) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_ERROR)
logger.doAppendIfLevelEnabled(JsonLogEvent{event: obj,}.Encode(&metadata), LogLevel_ERROR)
logger.doAppendIfLevelEnabled(&JsonLogEvent{event: obj,}, &metadata, LogLevel_ERROR)
} else {
logger.doAppendIfLevelEnabled(JsonLogEvent{event: obj,}.Encode(nil), LogLevel_ERROR)
logger.doAppendIfLevelEnabled(&JsonLogEvent{event: obj,}, nil, LogLevel_ERROR)
}
}

// Fatalj encodes as Json binary and calls specified appender to print.
func (logger *Logger) Fatalj(obj interface{}) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_FATAL)
logger.doAppendIfLevelEnabled(JsonLogEvent{event: obj,}.Encode(&metadata), LogLevel_FATAL)
logger.doAppendIfLevelEnabled(&JsonLogEvent{event: obj,}, &metadata, LogLevel_FATAL)
} else {
logger.doAppendIfLevelEnabled(JsonLogEvent{event: obj,}.Encode(nil), LogLevel_FATAL)
logger.doAppendIfLevelEnabled(&JsonLogEvent{event: obj,}, nil, LogLevel_FATAL)
}

logger.Close()
Expand All @@ -272,59 +273,59 @@ func (logger *Logger) Fatalj(obj interface{}) {
func (logger *Logger) STrace(logEvent LogEvent) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_TRACE)
logger.doAppendIfLevelEnabled(logEvent.Encode(&metadata), LogLevel_TRACE)
logger.doAppendIfLevelEnabled(logEvent, &metadata, LogLevel_TRACE)
} else {
logger.doAppendIfLevelEnabled(logEvent.Encode(nil), LogLevel_TRACE)
logger.doAppendIfLevelEnabled(logEvent, nil, LogLevel_TRACE)
}
}

// SDebug encodes as user defined logEvent and calls specified appender to print it.
func (logger *Logger) SDebug(logEvent LogEvent) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_DEBUG)
logger.doAppendIfLevelEnabled(logEvent.Encode(&metadata), LogLevel_DEBUG)
logger.doAppendIfLevelEnabled(logEvent, &metadata, LogLevel_DEBUG)
} else {
logger.doAppendIfLevelEnabled(logEvent.Encode(nil), LogLevel_DEBUG)
logger.doAppendIfLevelEnabled(logEvent, nil, LogLevel_DEBUG)
}
}

// SInfo encodes as user defined logEvent and calls specified appender to print it.
func (logger *Logger) SInfo(logEvent LogEvent) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_INFO)
logger.doAppendIfLevelEnabled(logEvent.Encode(&metadata), LogLevel_INFO)
logger.doAppendIfLevelEnabled(logEvent, &metadata, LogLevel_INFO)
} else {
logger.doAppendIfLevelEnabled(logEvent.Encode(nil), LogLevel_INFO)
logger.doAppendIfLevelEnabled(logEvent, nil, LogLevel_INFO)
}
}

// SWarn encodes as user defined logEvent and calls specified appender to print it.
func (logger *Logger) SWarn(logEvent LogEvent) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_WARN)
logger.doAppendIfLevelEnabled(logEvent.Encode(&metadata), LogLevel_WARN)
logger.doAppendIfLevelEnabled(logEvent, &metadata, LogLevel_WARN)
} else {
logger.doAppendIfLevelEnabled(logEvent.Encode(nil), LogLevel_WARN)
logger.doAppendIfLevelEnabled(logEvent, nil, LogLevel_WARN)
}
}

// SError encodes as user defined logEvent and calls specified appender to print it.
func (logger *Logger) SError(logEvent LogEvent) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_ERROR)
logger.doAppendIfLevelEnabled(logEvent.Encode(&metadata), LogLevel_ERROR)
logger.doAppendIfLevelEnabled(logEvent, &metadata, LogLevel_ERROR)
} else {
logger.doAppendIfLevelEnabled(logEvent.Encode(nil), LogLevel_ERROR)
logger.doAppendIfLevelEnabled(logEvent, nil, LogLevel_ERROR)
}
}

// SFatal encodes as user defined logEvent and calls specified appender to print it.
func (logger *Logger) SFatal(logEvent LogEvent) {
if logger.enabledMetadata {
metadata := logger.newMetadata(LogLevel_FATAL)
logger.doAppendIfLevelEnabled(logEvent.Encode(&metadata), LogLevel_FATAL)
logger.doAppendIfLevelEnabled(logEvent, &metadata, LogLevel_FATAL)
} else {
logger.doAppendIfLevelEnabled(logEvent.Encode(nil), LogLevel_FATAL)
logger.doAppendIfLevelEnabled(logEvent, nil, LogLevel_FATAL)
}

logger.Close()
Expand Down
4 changes: 2 additions & 2 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestNewLogger(t *testing.T) {
logger := NewLogger("testLogger", LogLevel_TRACE)
appender := NewByteBufferAppender()
logger.SetAppender(appender)
logger.SInfo(JsonLogEvent{
logger.SInfo(&JsonLogEvent{
event: struct{
Name string `json:"name"`
Address string `json:"address"`
Expand Down Expand Up @@ -119,7 +119,7 @@ func BenchmarkLogger_text_to_buffer(b *testing.B) {
logger.DisableLogEventMetadata()

for i := 0; i< b.N; i++ {
logger.doAppendIfLevelEnabled(TextLogEvent{Event:"ssss"}.Encode(nil), LogLevel_INFO)
logger.doAppendIfLevelEnabled(&TextLogEvent{Event:"ssss"}, nil, LogLevel_INFO)
}
}

Expand Down

0 comments on commit 7e3404a

Please sign in to comment.