Skip to content

Commit

Permalink
Merge pull request #42 from linkdata/logerror
Browse files Browse the repository at this point in the history
Logerror
  • Loading branch information
linkdata committed Jun 13, 2024
2 parents bc9efbd + 2a72bee commit 7d3a105
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
11 changes: 5 additions & 6 deletions jaws.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"fmt"
"html/template"
"io"
"log"
"net"
"net/http"
"net/netip"
Expand All @@ -41,9 +40,9 @@ const (
type Jid = jid.Jid // convenience alias

type Jaws struct {
CookieName string // Name for session cookies, defaults to "jaws"
Logger *log.Logger // If not nil, send debug info and errors here
Debug bool // set to true to enable debugging output
CookieName string // Name for session cookies, defaults to "jaws"
Logger any // If not nil, send debug info and errors here
Debug bool // set to true to enable debugging output
doneCh <-chan struct{}
bcastCh chan Message
subCh chan subscription
Expand Down Expand Up @@ -167,7 +166,7 @@ func (jw *Jaws) RequestCount() (n int) {
// Returns err.
func (jw *Jaws) Log(err error) error {
if err != nil && jw != nil && jw.Logger != nil {
jw.Logger.Println(err.Error())
LogError(jw.Logger, err.Error())
}
return err
}
Expand All @@ -178,7 +177,7 @@ func (jw *Jaws) Log(err error) error {
func (jw *Jaws) MustLog(err error) {
if err != nil {
if jw != nil && jw.Logger != nil {
jw.Logger.Println(err.Error())
LogError(jw.Logger, err.Error())
} else {
panic(err)
}
Expand Down
29 changes: 29 additions & 0 deletions logerror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package jaws

import (
"fmt"
"io"
"strings"
)

type printlner interface {
Println(v ...any)
}

type errorer interface {
Error(msg string, args ...any)
}

// LogError is an adapter allowing logging an informational message
// to a log.Logger, slog.Logger or io.Writer.
func LogError(logger any, format string, args ...any) {
msg := fmt.Sprintf(strings.TrimRight(format, "\n"), args...)
switch x := logger.(type) {
case printlner:
x.Println(msg)
case errorer:
x.Error(msg)
case io.Writer:
_, _ = fmt.Fprintln(x, msg)
}
}
46 changes: 46 additions & 0 deletions logerror_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package jaws

import (
"bytes"
"fmt"
"testing"
)

type printlnerImpl struct {
str string
}

func (x *printlnerImpl) Println(args ...any) {
x.str += fmt.Sprint(args...)
}

type errorerImpl struct {
str string
}

func (x *errorerImpl) Error(msg string, args ...any) {
x.str += msg
}

func TestLogError(t *testing.T) {
a := printlnerImpl{}
LogError(&a, "a")
LogError(&a, "\na\n")
if a.str != "a\na" {
t.Errorf("%q", a.str)
}

b := errorerImpl{}
LogError(&b, "b")
LogError(&b, "\nb\n")
if b.str != "b\nb" {
t.Errorf("%q", b.str)
}

var c bytes.Buffer
LogError(&c, "c")
LogError(&c, "\nc\n")
if c.String() != "c\n\nc\n" {
t.Errorf("%q", c.String())
}
}

0 comments on commit 7d3a105

Please sign in to comment.