Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

feat: logging to console #7

Merged
merged 7 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions pkg/aspirador.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package pkg

import (
"log"
)

const (
defaultLoggerFlag = log.Ldate | log.Ltime | log.Lshortfile | log.Lmicroseconds
)

var (
global Aspirador
)

type aspiradorRecord struct {
Message string
Level Level
}

type aspiradorWriter interface {
Write(ar aspiradorRecord)
}

type Aspirador struct {
writers []aspiradorWriter
}
rutesantos4 marked this conversation as resolved.
Show resolved Hide resolved

// Default to Console.
func NewAspirador() {
writers := make([]aspiradorWriter, 1)
writers[0] = NewConsoleWriter()

global = Aspirador{
writers: writers,
}
}
rutesantos4 marked this conversation as resolved.
Show resolved Hide resolved

func Trace(msg string) {
global.log(TRACE, msg)
}

func Info(msg string) {
global.log(INFO, msg)
}

func Warning(msg string) {
global.log(WARNING, msg)
}

func Error(msg string) {
global.log(ERROR, msg)
}
rutesantos4 marked this conversation as resolved.
Show resolved Hide resolved

func (as Aspirador) log(lvl Level, msg string) {
record := aspiradorRecord{
Level: lvl,
Message: msg,
}

for _, v := range as.writers {
v.Write(record)
}
freitzzz marked this conversation as resolved.
Show resolved Hide resolved

}
36 changes: 36 additions & 0 deletions pkg/consolewriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package pkg

import (
"io"
"log"
"os"
)

var stdout io.Writer = os.Stdout
rutesantos4 marked this conversation as resolved.
Show resolved Hide resolved

type ConsoleWriter struct {
logs []*log.Logger
rutesantos4 marked this conversation as resolved.
Show resolved Hide resolved
}

func NewConsoleWriter() ConsoleWriter {
logs := make([]*log.Logger, len(levelStrings))

for i, v := range levelStrings {
logs[i] = log.New(stdout, v, defaultLoggerFlag)
}

return ConsoleWriter{
logs: logs,
}
}

func (cw ConsoleWriter) Write(ar aspiradorRecord) {
log := cw.logs[ar.Level]

if log == nil {
//ERROR
return
}
rutesantos4 marked this conversation as resolved.
Show resolved Hide resolved

log.Println(ar.Message)
}
21 changes: 21 additions & 0 deletions pkg/level.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pkg

const (
TRACE = iota
INFO
WARNING
ERROR
rutesantos4 marked this conversation as resolved.
Show resolved Hide resolved
)

var (
levelStrings = [...]string{"TRACE: ", "INFO: ", "WARNING: ", "ERROR: "}
rutesantos4 marked this conversation as resolved.
Show resolved Hide resolved
rutesantos4 marked this conversation as resolved.
Show resolved Hide resolved
)

type Level int

func (l Level) String() string {
if l < 0 || int(l) > len(levelStrings) {
return "UNKNOWN"
}
rutesantos4 marked this conversation as resolved.
Show resolved Hide resolved
rutesantos4 marked this conversation as resolved.
Show resolved Hide resolved
return levelStrings[int(l)]
}