Skip to content

Commit

Permalink
Created shortly-log test and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
burkaydurdu committed Mar 2, 2022
1 parent ae73591 commit 1febf70
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
31 changes: 31 additions & 0 deletions internal/server/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package server

import (
"fmt"
"log"
)

type ShortlyLog struct {
Error error
Message string
Tag string
format string
}

func (s *ShortlyLog) ZapError(message string, error error) {
s.Message = message
s.Error = error
s.format = fmt.Sprintf("[ERROR] %s, %v", s.Message, s.Error)
log.Println(s.format)
}

func (s *ShortlyLog) Zap(message string) {
s.Message = message

if s.Tag == "" {
s.Tag = "INFO"
}

s.format = fmt.Sprintf("[%s] %s", s.Tag, s.Message)
log.Println(s.format)
}
30 changes: 30 additions & 0 deletions internal/server/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build unit
// +build unit

package server

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestServer_Log(t *testing.T) {
l := ShortlyLog{
Tag: "Tag",
}

l.Zap("Test")
assert.Equal(t, l.format, "[Tag] Test")

l.Zap("Test2")
assert.Equal(t, l.format, "[Tag] Test2")

l.Tag = "Warning"
l.Zap("Warn")
assert.Equal(t, l.format, "[Warning] Warn")

l.ZapError("couldn't convert type", errors.New("invalid type"))
assert.Equal(t, l.format, "[ERROR] couldn't convert type, invalid type")
}

0 comments on commit 1febf70

Please sign in to comment.