This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
-
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.
Merge pull request #18 from palavrapasse/14-feat-allow-clients-to-spe…
…cify-logging-levels-to-enable feat: allow clients to specify logging levels to enable
- Loading branch information
Showing
8 changed files
with
224 additions
and
34 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 |
---|---|---|
|
@@ -2,4 +2,5 @@ package pkg | |
|
||
type Client interface { | ||
Write(ar Record) | ||
SupportsLevel(l Level) bool | ||
} |
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 |
---|---|---|
@@ -1,26 +1,31 @@ | ||
package pkg | ||
|
||
import ( | ||
"log" | ||
"os" | ||
) | ||
|
||
type ConsoleClient struct { | ||
loggers []*log.Logger | ||
loggers LevelLogger | ||
} | ||
|
||
func NewConsoleClient() ConsoleClient { | ||
loggers := make([]*log.Logger, len(levelPrefix)) | ||
|
||
for i, v := range levelPrefix { | ||
loggers[i] = log.New(os.Stdout, v, defaultLoggerFlag) | ||
} | ||
func NewConsoleClient(levels ...Level) ConsoleClient { | ||
loggers := NewLevelLogger(os.Stdout, defaultLoggerFlag, levels) | ||
|
||
return ConsoleClient{ | ||
loggers: loggers, | ||
} | ||
} | ||
|
||
func (cc ConsoleClient) Write(ar Record) { | ||
cc.loggers[ar.Level].Println(ar.Message) | ||
logger, exists := cc.loggers[ar.Level] | ||
|
||
if !exists { | ||
return | ||
} | ||
|
||
logger.Println(ar.Message) | ||
} | ||
|
||
func (cc ConsoleClient) SupportsLevel(l Level) bool { | ||
return cc.loggers.ContainsLevel(l) | ||
} |
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 |
---|---|---|
@@ -1,32 +1,37 @@ | ||
package pkg | ||
|
||
import ( | ||
"log" | ||
"os" | ||
) | ||
|
||
type FileClient struct { | ||
loggers []*log.Logger | ||
loggers LevelLogger | ||
} | ||
|
||
func NewFileClient(fp string) (FileClient, error) { | ||
func NewFileClient(fp string, levels ...Level) (FileClient, error) { | ||
file, err := os.OpenFile(fp, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0770) | ||
|
||
if err != nil { | ||
return FileClient{}, err | ||
} | ||
|
||
loggers := make([]*log.Logger, len(levelPrefix)) | ||
|
||
for i, v := range levelPrefix { | ||
loggers[i] = log.New(file, v, defaultLoggerFlag) | ||
} | ||
loggers := NewLevelLogger(file, defaultLoggerFlag, levels) | ||
|
||
return FileClient{ | ||
loggers: loggers, | ||
}, nil | ||
} | ||
|
||
func (fc FileClient) Write(ar Record) { | ||
fc.loggers[ar.Level].Println(ar.Message) | ||
logger, exists := fc.loggers[ar.Level] | ||
|
||
if !exists { | ||
return | ||
} | ||
|
||
logger.Println(ar.Message) | ||
} | ||
|
||
func (fc FileClient) SupportsLevel(l Level) bool { | ||
return fc.loggers.ContainsLevel(l) | ||
} |
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,30 @@ | ||
package pkg | ||
|
||
import ( | ||
"io" | ||
"log" | ||
) | ||
|
||
type LevelLogger map[Level]*log.Logger | ||
|
||
func NewLevelLogger(out io.Writer, flag int, levels []Level) LevelLogger { | ||
size := len(levels) | ||
|
||
if size == 0 { | ||
levels = []Level{TRACE, INFO, WARNING, ERROR} | ||
size = len(levels) | ||
} | ||
|
||
result := make(map[Level]*log.Logger, size) | ||
|
||
for _, v := range levels { | ||
result[v] = log.New(out, levelPrefix[v], flag) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func (ll LevelLogger) ContainsLevel(l Level) bool { | ||
_, exists := ll[l] | ||
return exists | ||
} |
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,150 @@ | ||
package pkg | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestNewLevelLoggerWithoutLevelsReturnsLevelLoggerWithLenght4(t *testing.T) { | ||
|
||
result := NewLevelLogger(os.Stdout, defaultLoggerFlag, []Level{}) | ||
size := len(result) | ||
|
||
if size != 4 { | ||
t.Fatalf("No levels provided so it should return a map with 4 elements, but it has %d", size) | ||
} | ||
} | ||
|
||
func TestNewLevelLoggerWithTRACELevelReturnsLevelLoggerWithTRACELevel(t *testing.T) { | ||
|
||
result := NewLevelLogger(os.Stdout, defaultLoggerFlag, []Level{TRACE}) | ||
|
||
_, ok := result[TRACE] | ||
|
||
if !ok { | ||
t.Fatalf("No levels provided so it should return a map with TRACE level") | ||
} | ||
} | ||
|
||
func TestNewLevelLoggerWithTRACELevelReturnsLevelLoggerWithLength1(t *testing.T) { | ||
|
||
result := NewLevelLogger(os.Stdout, defaultLoggerFlag, []Level{TRACE}) | ||
size := len(result) | ||
|
||
if size != 1 { | ||
t.Fatalf("One level provided so it should return a map with 1 elements, but it has %d", size) | ||
} | ||
} | ||
|
||
func TestNewLevelLoggerWithINFOLevelReturnsLevelLoggerWithINFOLevel(t *testing.T) { | ||
|
||
result := NewLevelLogger(os.Stdout, defaultLoggerFlag, []Level{INFO}) | ||
|
||
_, ok := result[INFO] | ||
|
||
if !ok { | ||
t.Fatalf("No levels provided so it should return a map with INFO level") | ||
} | ||
} | ||
|
||
func TestNewLevelLoggerWithINFOLevelReturnsLevelLoggerWithLength1(t *testing.T) { | ||
|
||
result := NewLevelLogger(os.Stdout, defaultLoggerFlag, []Level{INFO}) | ||
size := len(result) | ||
|
||
if size != 1 { | ||
t.Fatalf("One level provided so it should return a map with 1 elements, but it has %d", size) | ||
} | ||
} | ||
|
||
func TestNewLevelLoggerWithoutLevelsReturnsLevelLoggerWithWARNINGLevel(t *testing.T) { | ||
|
||
result := NewLevelLogger(os.Stdout, defaultLoggerFlag, []Level{WARNING}) | ||
|
||
_, ok := result[WARNING] | ||
|
||
if !ok { | ||
t.Fatalf("No levels provided so it should return a map with WARNING level") | ||
} | ||
} | ||
|
||
func TestNewLevelLoggerWithWARNINGLevelReturnsLevelLoggerWithLenght1(t *testing.T) { | ||
|
||
result := NewLevelLogger(os.Stdout, defaultLoggerFlag, []Level{WARNING}) | ||
size := len(result) | ||
|
||
if size != 1 { | ||
t.Fatalf("One level provided so it should return a map with 1 elements, but it has %d", size) | ||
} | ||
} | ||
|
||
func TestNewLevelLoggerWithERRORLevelReturnsLevelLoggerWithERRORLevel(t *testing.T) { | ||
|
||
result := NewLevelLogger(os.Stdout, defaultLoggerFlag, []Level{ERROR}) | ||
|
||
_, ok := result[ERROR] | ||
|
||
if !ok { | ||
t.Fatalf("No levels provided so it should return a map with ERROR level") | ||
} | ||
} | ||
|
||
func TestNewLevelLoggerWithERRORLevelReturnsLevelLoggerWithLenght1(t *testing.T) { | ||
|
||
result := NewLevelLogger(os.Stdout, defaultLoggerFlag, []Level{TRACE}) | ||
size := len(result) | ||
|
||
if size != 1 { | ||
t.Fatalf("One level provided so it should return a map with 1 elements, but it has %d", size) | ||
} | ||
} | ||
|
||
func TestNewLevelLoggerWithMultipleLevelsReturnsLevelLoggerWithTheSameLenghtAsTheLevels(t *testing.T) { | ||
|
||
levels := []Level{TRACE, WARNING} | ||
result := NewLevelLogger(os.Stdout, defaultLoggerFlag, levels) | ||
|
||
expected := len(levels) | ||
size := len(result) | ||
|
||
if size != expected { | ||
t.Fatalf("%d levels provided so it should return a map with %d elements, but it has %d", expected, expected, size) | ||
} | ||
} | ||
|
||
func TestNewLevelLoggerWithMultipleLevelsReturnsLevelLoggerWithTheLevelsProvided(t *testing.T) { | ||
|
||
levels := []Level{TRACE, WARNING} | ||
result := NewLevelLogger(os.Stdout, defaultLoggerFlag, levels) | ||
|
||
for _, v := range levels { | ||
_, ok := result[v] | ||
|
||
if !ok { | ||
t.Fatalf("No levels provided so it should return a map with %d level", v) | ||
} | ||
|
||
} | ||
} | ||
|
||
func TestContaisLevelWithLevelPresentReturnsTrue(t *testing.T) { | ||
|
||
levels := []Level{TRACE} | ||
ll := NewLevelLogger(os.Stdout, defaultLoggerFlag, levels) | ||
result := ll.ContainsLevel(TRACE) | ||
|
||
if !result { | ||
t.Fatalf("Level is present in LevelLogger so it should return true") | ||
} | ||
} | ||
|
||
func TestContaisLevelWithLevelNotPresentReturnsFalse(t *testing.T) { | ||
|
||
levels := []Level{TRACE} | ||
ll := NewLevelLogger(os.Stdout, defaultLoggerFlag, levels) | ||
result := ll.ContainsLevel(ERROR) | ||
|
||
if result { | ||
t.Fatalf("Level is not present in LevelLogger so it should return false") | ||
} | ||
} |
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