Skip to content

Commit

Permalink
change golog to use pointers to the sink
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilliams0305 committed Nov 6, 2023
1 parent 28727af commit fc71444
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
14 changes: 9 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func main() {
WriteTo(sink2).MinimuLevel(golog.Information).
CreateLogger()

// GOLOG provide you runtime log level switching.
// When log levels are switched at runtime it will syncronize and revert the level of all sinks.
// In the future you will be able to switch the level of each sink independantly.
var response string

fmt.Print("Enter a new Log Level (verbose, debug, info, warn, error, fatal): ")
Expand All @@ -27,14 +30,15 @@ func main() {
if err != nil {
logger.Error("Proceeding with default levels", err)
} else {
level := golog.CreateLevelFromString(response)
logger.SwitchLevel(level)

}
level := golog.CreateLevelFromString(response)
logger.SwitchLevel(level)

logger.Verbose("Verbose Message %s", "VERNON")
logger.Debug("Debug Message %s %d", "BILLY", 20)
logger.Information("Information Message %s", "IMAC")
logger.Warn("Warn Message %s %d", []interface{}{"Alice", 30})
logger.Warn("Warn Message %s %d", "Alice", 30)
logger.Error("Error Message", errors.New("ERROR"))
logger.Fatal("Fatal Message", errors.New("FATAL"))

Expand All @@ -53,7 +57,7 @@ func formatTemplate(template string, args ...interface{}) string {
type FmtPrinter struct {
}

func (f FmtPrinter) WriteTo(message golog.LogEvent) error {
func (f *FmtPrinter) WriteTo(message golog.LogEvent) error {

_, e := fmt.Println(message.RenderMessage())
return e
Expand All @@ -68,7 +72,7 @@ func (f FmtPrinter) WriteTo(message golog.LogEvent) error {
type FmtPrinterSlow struct {
}

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

duration := 2 * time.Second
time.Sleep(duration)
Expand Down
6 changes: 3 additions & 3 deletions logger/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ func (gl *goLog) Configure(minimuLevel LogLevel, template string) loggerWriter {

func (gl *goLog) WriteTo(sink SinkWriter) createWriters {

config := loggingSink{
sink: sink,
writer := &loggingSink{
sink: &sink,
config: configuration{
level: gl.config.level,
format: gl.config.format,
},
}
gl.sinks = append(gl.sinks, config)
gl.sinks = append(gl.sinks, writer)

gl.sinkIndex++
return gl
Expand Down
8 changes: 4 additions & 4 deletions logger/golog.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ import (
// pointers to your sinks, and implementations of the loger builder.
// While tou will never directly access the golog it a critical component of the framework.
type goLog struct {
sinks []loggingSink
sinks []*loggingSink
config configuration
sinkIndex int16
}

type loggingSink struct {
sink SinkWriter
sink *SinkWriter
config configuration
}

Expand Down Expand Up @@ -127,8 +127,8 @@ func (gl *goLog) write(message string, level LogLevel, args ...interface{}) {

wg.Add(1)

go func(sink SinkWriter) {
writeSink(sink, LogEvent{
go func(sink *SinkWriter) {
writeSink(*sink, LogEvent{
Timestamp: time.Now(),
Level: level,
Message: message,
Expand Down
2 changes: 1 addition & 1 deletion logger/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s LogLevel) ToString() string {
func CreateLevelFromString(s string) LogLevel {

switch strings.ToUpper(s) {
case "VERSOSE":
case "VERBOSE":
return Verbose
case "DEBUG":
return Debug
Expand Down

0 comments on commit fc71444

Please sign in to comment.