Skip to content

Commit bd0d3c5

Browse files
committed
Add a default logger.
- Having the default logger set to nil was troublesome because some errors are hard to detect without a logger. This falls under "sane default" changes and so should be made.
1 parent d8051d9 commit bd0d3c5

7 files changed

Lines changed: 56 additions & 3 deletions

File tree

auth/auth_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package auth
33
import (
44
"errors"
55
"html/template"
6+
"io/ioutil"
67
"net/http"
78
"net/http/httptest"
89
"strings"
@@ -16,6 +17,7 @@ func testSetup() (a *Auth, s *mocks.MockStorer) {
1617
s = mocks.NewMockStorer()
1718

1819
authboss.Cfg = authboss.NewConfig()
20+
authboss.Cfg.LogWriter = ioutil.Discard
1921
authboss.Cfg.Layout = template.Must(template.New("").Parse(`{{template "authboss" .}}`))
2022
authboss.Cfg.Storer = s
2123
authboss.Cfg.XSRFName = "xsrf"

authboss_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package authboss
33
import (
44
"database/sql"
55
"errors"
6+
"io/ioutil"
67
"net/http"
78
"net/http/httptest"
89
"os"
@@ -11,13 +12,15 @@ import (
1112

1213
func TestMain(main *testing.M) {
1314
RegisterModule("testmodule", testMod)
15+
Cfg.LogWriter = ioutil.Discard
1416
Init()
1517
code := main.Run()
1618
os.Exit(code)
1719
}
1820

1921
func TestAuthBossInit(t *testing.T) {
2022
Cfg = NewConfig()
23+
Cfg.LogWriter = ioutil.Discard
2124
err := Init()
2225
if err != nil {
2326
t.Error("Unexpected error:", err)
@@ -26,6 +29,7 @@ func TestAuthBossInit(t *testing.T) {
2629

2730
func TestAuthBossCurrentUser(t *testing.T) {
2831
Cfg = NewConfig()
32+
Cfg.LogWriter = ioutil.Discard
2933
Cfg.Storer = mockStorer{"joe": Attributes{"email": "john@john.com", "password": "lies"}}
3034
Cfg.SessionStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
3135
return mockClientStore{SessionKey: "joe"}

config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func NewConfig() *Config {
170170
LockWindow: 5 * time.Minute,
171171
LockDuration: 5 * time.Hour,
172172

173-
LogWriter: ioutil.Discard,
173+
LogWriter: NewDefaultLogger(),
174174
Callbacks: NewCallbacks(),
175175
Mailer: LogMailer(ioutil.Discard),
176176
}

logger.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package authboss
2+
3+
import (
4+
"log"
5+
"os"
6+
)
7+
8+
// DefaultLogger is a basic logger.
9+
type DefaultLogger log.Logger
10+
11+
// NewDefaultLogger creates a logger to stdout.
12+
func NewDefaultLogger() *DefaultLogger {
13+
return ((*DefaultLogger)(log.New(os.Stdout, "", log.LstdFlags)))
14+
}
15+
16+
// Write writes to the internal logger.
17+
func (d *DefaultLogger) Write(b []byte) (int, error) {
18+
((*log.Logger)(d)).Printf("%s", b)
19+
return len(b), nil
20+
}

logger_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package authboss
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"log"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func TestDefaultLogger(t *testing.T) {
12+
logger := NewDefaultLogger()
13+
if logger == nil {
14+
t.Error("Logger was not created.")
15+
}
16+
}
17+
18+
func TestDefaultLoggerOutput(t *testing.T) {
19+
buffer := &bytes.Buffer{}
20+
logger := (*DefaultLogger)(log.New(buffer, "", log.LstdFlags))
21+
io.WriteString(logger, "hello world")
22+
if s := buffer.String(); !strings.HasSuffix(s, "hello world\n") {
23+
t.Error("Output was wrong:", s)
24+
}
25+
}

mailer_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ package authboss
22

33
import (
44
"bytes"
5+
"io/ioutil"
56
"strings"
67
"testing"
78
)
89

910
func TestMailer(t *testing.T) {
10-
NewConfig()
11+
Cfg = NewConfig()
1112
mailServer := &bytes.Buffer{}
1213

1314
Cfg.Mailer = LogMailer(mailServer)
1415
Cfg.Storer = mockStorer{}
16+
Cfg.LogWriter = ioutil.Discard
1517
Init()
1618

1719
err := SendMail(Email{

router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func NewRouter() http.Handler {
1919

2020
for name, mod := range modules {
2121
for route, handler := range mod.Routes() {
22-
fmt.Fprintf(Cfg.LogWriter, "%-10s Register Route: %s\n", "["+name+"]", path.Join(Cfg.MountPath, route))
22+
fmt.Fprintf(Cfg.LogWriter, "%-10s Route: %s\n", "["+name+"]", path.Join(Cfg.MountPath, route))
2323
mux.Handle(path.Join(Cfg.MountPath, route), contextRoute{handler})
2424
}
2525
}

0 commit comments

Comments
 (0)