Skip to content

Commit

Permalink
added concurrency to write
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilliams0305 committed Nov 6, 2023
1 parent 483a8a7 commit 840c07e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
33 changes: 29 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package main
import (
"errors"
"fmt"
"time"

golog "github.com/ewilliams0305/golog/logger"
)

func main() {

sink1 := &FmtPrinter{}
sink2 := &FmtPrinter{}
sink2 := &FmtPrinterSlow{}

logger := golog.LoggingConfiguration().
Configure(golog.Verbose, "[%l %t] %m").
WriteTo(sink1).MinimuLevel(golog.Fatal).
WriteTo(sink1).MinimuLevel(golog.Verbose).
WriteTo(sink2).MinimuLevel(golog.Verbose).
CreateLogger()

Expand All @@ -27,9 +28,33 @@ func main() {

}

/***************************
*
* Mock Logger that is FAST
*
****************************/

type FmtPrinter struct {
}

func (f FmtPrinter) WriteTo(message golog.LogEvent) {
fmt.Println(message.RenderMessage())
func (f FmtPrinter) WriteTo(message golog.LogEvent) error {
_, e := fmt.Println(message.RenderMessage())
return e
}

/***************************
*
* Mock Logger that is FAST
*
****************************/

type FmtPrinterSlow struct {
}

func (f FmtPrinterSlow) WriteTo(message golog.LogEvent) error {

duration := 2 * time.Second
time.Sleep(duration)
_, e := fmt.Println(message.RenderMessage())
return e
}
53 changes: 31 additions & 22 deletions logger/golog.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*
The golog package is a logging framework designed around abstracting the output if logging messages.
The golog framework helps to:
- Describe a log message with structure
Expand Down Expand Up @@ -27,11 +26,11 @@ func main() {
logger.Fatal("Fatal Message", errors.New("FATAL"), nil)
}
*/
package golog

import (
"sync"
"time"
)

Expand Down Expand Up @@ -66,61 +65,71 @@ type Logger interface {

func (gl *goLog) Verbose(message string, props properties) {

//if gl.config.level <= Verbose {
gl.write(message, Verbose, props)
//}
}

func (gl *goLog) Debug(message string, props properties) {

//if gl.config.level <= Debug {
gl.write(message, Debug, props)
//}
}

func (gl *goLog) Information(message string, props properties) {

//if gl.config.level <= Information {
gl.write(message, Information, props)
//}
}

func (gl *goLog) Warn(message string, props properties) {

//if gl.config.level <= Warn {
gl.write(message, Warn, props)
//}
}

func (gl *goLog) Error(message string, err error, props properties) {

//if gl.config.level <= Error {
gl.write(message, Error, props)
//}
}

func (gl *goLog) Fatal(message string, err error, props properties) {

//if gl.config.level <= Fatal {
gl.write(message, Fatal, props)
//}
}

func (gl *goLog) write(message string, level LogLevel, props properties) {

c := make(chan string)
resultChan := make(chan string, len(gl.sinks))
var wg sync.WaitGroup

for _, s := range gl.sinks {
if s.config.level <= level {
writeSink(s.sink, LogEvent{
timestamp: time.Now(),
level: level,
message: message,
props: props,
}, c)

wg.Add(1)

go func(sink SinkWriter) {
writeSink(sink, LogEvent{
timestamp: time.Now(),
level: level,
message: message,
props: props,
}, resultChan)
defer wg.Done()
}(s.sink)
}
}

go func() {
wg.Wait()
close(resultChan)
}()

for range resultChan {
}
}

func writeSink(s SinkWriter, e LogEvent, c chan string) {
s.WriteTo(e)

err := s.WriteTo(e)
if err != nil {
c <- "Error: " + err.Error()
} else {
c <- "Success"
}
}
2 changes: 1 addition & 1 deletion logger/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ type SinkWriter interface {
// The write method will be invoked when a log messge is created.
// The write method will only be invoked if
// - The log message is greater than the sinks current level.
WriteTo(message LogEvent)
WriteTo(message LogEvent) error
}

0 comments on commit 840c07e

Please sign in to comment.