Skip to content

Commit

Permalink
add log channel
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachard committed Nov 20, 2023
1 parent a955e73 commit f39613a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
35 changes: 31 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"sync"
"time"
)

type Level int
Expand All @@ -22,12 +23,19 @@ const (
prefixFatal = "FATAL: "
)

type LogEntry struct {
Timestamp time.Time
Level Level
Message string
}

type Logger struct {
infoLog *log.Logger
errorLog *log.Logger
fatalLog *log.Logger
mu sync.Mutex
verbose bool
channel chan LogEntry
}

// Init loggers for each log levels
Expand All @@ -42,6 +50,13 @@ func New(verbose bool) *Logger {
return &l
}

// Sets the output to a channel
func (l *Logger) SetOutputChannel(c chan LogEntry) {
l.mu.Lock()
defer l.mu.Unlock()
l.channel = c
}

// Sets the output destination for the loggers
// By default destination is stdout, you can change that
// with this function
Expand Down Expand Up @@ -81,15 +96,27 @@ func (l *Logger) output(level Level, msg string) {

func (l *Logger) Info(format string, v ...interface{}) {
if l.verbose {
l.output(INFO, fmt.Sprintf(format, v...))
if l.channel != nil {
l.channel <- LogEntry{Timestamp: time.Now(), Level: INFO, Message: fmt.Sprintf(format, v...)}
} else {
l.output(INFO, fmt.Sprintf(format, v...))
}
}
}

func (l *Logger) Error(format string, v ...interface{}) {
l.output(ERROR, fmt.Sprintf(format, v...))
if l.channel != nil {
l.channel <- LogEntry{Timestamp: time.Now(), Level: ERROR, Message: fmt.Sprintf(format, v...)}
} else {
l.output(ERROR, fmt.Sprintf(format, v...))
}
}

func (l *Logger) Fatal(v ...interface{}) {
l.output(FATAL, fmt.Sprint(v...))
os.Exit(1)
if l.channel != nil {
l.channel <- LogEntry{Timestamp: time.Now(), Level: FATAL, Message: fmt.Sprint(v...)}
} else {
l.output(FATAL, fmt.Sprint(v...))
os.Exit(1)
}
}
35 changes: 35 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,41 @@ import (

const Pattern_ts = `\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}.\d{6} `

func TestLoggerWithChannel(t *testing.T) {
// Create channel
logChannel := make(chan LogEntry, 10)

// Create logger
lg := New(true)
lg.SetOutputChannel((logChannel))

// logs messages
lg.Info("This is an informational message.")
lg.Error("This is an error message.")
lg.Fatal("This is a fatal message.")

// Expected entries
expectedEntries := []LogEntry{
{Level: INFO, Message: "This is an informational message."},
{Level: ERROR, Message: "This is an error message."},
{Level: FATAL, Message: "This is a fatal message."},
}

for i, expectedEntry := range expectedEntries {
select {
case entry := <-logChannel:
if entry.Level != expectedEntry.Level || entry.Message != expectedEntry.Message {
t.Errorf("Test case %d: Unexpected log entry. Got: %+v, Expected: %+v", i+1, entry, expectedEntry)
}
default:
t.Errorf("Test case %d: No log entry received from the channel.", i+1)
}
}

// cleanup
close(logChannel)
}

func TestLogInfo(t *testing.T) {
// prepare a buffer instead of stdout and a string to search
const msg = "hello world"
Expand Down

0 comments on commit f39613a

Please sign in to comment.