1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-09-16 09:06:20 +02:00

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.
This commit is contained in:
Aaron
2015-03-30 09:55:03 -07:00
parent d8051d9aa5
commit bd0d3c5f68
7 changed files with 56 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ package auth
import (
"errors"
"html/template"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
@@ -16,6 +17,7 @@ func testSetup() (a *Auth, s *mocks.MockStorer) {
s = mocks.NewMockStorer()
authboss.Cfg = authboss.NewConfig()
authboss.Cfg.LogWriter = ioutil.Discard
authboss.Cfg.Layout = template.Must(template.New("").Parse(`{{template "authboss" .}}`))
authboss.Cfg.Storer = s
authboss.Cfg.XSRFName = "xsrf"

View File

@@ -3,6 +3,7 @@ package authboss
import (
"database/sql"
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
@@ -11,6 +12,7 @@ import (
func TestMain(main *testing.M) {
RegisterModule("testmodule", testMod)
Cfg.LogWriter = ioutil.Discard
Init()
code := main.Run()
os.Exit(code)
@@ -18,6 +20,7 @@ func TestMain(main *testing.M) {
func TestAuthBossInit(t *testing.T) {
Cfg = NewConfig()
Cfg.LogWriter = ioutil.Discard
err := Init()
if err != nil {
t.Error("Unexpected error:", err)
@@ -26,6 +29,7 @@ func TestAuthBossInit(t *testing.T) {
func TestAuthBossCurrentUser(t *testing.T) {
Cfg = NewConfig()
Cfg.LogWriter = ioutil.Discard
Cfg.Storer = mockStorer{"joe": Attributes{"email": "john@john.com", "password": "lies"}}
Cfg.SessionStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
return mockClientStore{SessionKey: "joe"}

View File

@@ -170,7 +170,7 @@ func NewConfig() *Config {
LockWindow: 5 * time.Minute,
LockDuration: 5 * time.Hour,
LogWriter: ioutil.Discard,
LogWriter: NewDefaultLogger(),
Callbacks: NewCallbacks(),
Mailer: LogMailer(ioutil.Discard),
}

20
logger.go Normal file
View File

@@ -0,0 +1,20 @@
package authboss
import (
"log"
"os"
)
// DefaultLogger is a basic logger.
type DefaultLogger log.Logger
// NewDefaultLogger creates a logger to stdout.
func NewDefaultLogger() *DefaultLogger {
return ((*DefaultLogger)(log.New(os.Stdout, "", log.LstdFlags)))
}
// Write writes to the internal logger.
func (d *DefaultLogger) Write(b []byte) (int, error) {
((*log.Logger)(d)).Printf("%s", b)
return len(b), nil
}

25
logger_test.go Normal file
View File

@@ -0,0 +1,25 @@
package authboss
import (
"bytes"
"io"
"log"
"strings"
"testing"
)
func TestDefaultLogger(t *testing.T) {
logger := NewDefaultLogger()
if logger == nil {
t.Error("Logger was not created.")
}
}
func TestDefaultLoggerOutput(t *testing.T) {
buffer := &bytes.Buffer{}
logger := (*DefaultLogger)(log.New(buffer, "", log.LstdFlags))
io.WriteString(logger, "hello world")
if s := buffer.String(); !strings.HasSuffix(s, "hello world\n") {
t.Error("Output was wrong:", s)
}
}

View File

@@ -2,16 +2,18 @@ package authboss
import (
"bytes"
"io/ioutil"
"strings"
"testing"
)
func TestMailer(t *testing.T) {
NewConfig()
Cfg = NewConfig()
mailServer := &bytes.Buffer{}
Cfg.Mailer = LogMailer(mailServer)
Cfg.Storer = mockStorer{}
Cfg.LogWriter = ioutil.Discard
Init()
err := SendMail(Email{

View File

@@ -19,7 +19,7 @@ func NewRouter() http.Handler {
for name, mod := range modules {
for route, handler := range mod.Routes() {
fmt.Fprintf(Cfg.LogWriter, "%-10s Register Route: %s\n", "["+name+"]", path.Join(Cfg.MountPath, route))
fmt.Fprintf(Cfg.LogWriter, "%-10s Route: %s\n", "["+name+"]", path.Join(Cfg.MountPath, route))
mux.Handle(path.Join(Cfg.MountPath, route), contextRoute{handler})
}
}