Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an interface for logger #433

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 0 additions & 11 deletions Godeps/Godeps.json

This file was deleted.

5 changes: 5 additions & 0 deletions go.mod
@@ -0,0 +1,5 @@
module github.com/go-martini/martini

go 1.15

require github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0
2 changes: 2 additions & 0 deletions go.sum
@@ -0,0 +1,2 @@
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 h1:sDMmm+q/3+BukdIpxwO365v/Rbspp2Nt5XntgQRXq8Q=
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM=
8 changes: 6 additions & 2 deletions logger.go
@@ -1,14 +1,18 @@
package martini

import (
"log"
"net/http"
"time"
)

type logger interface {
Printf(s string, a ...interface{})
Fatalln(args ...interface{})
}

// Logger returns a middleware handler that logs the request as it goes in and the response as it goes out.
func Logger() Handler {
return func(res http.ResponseWriter, req *http.Request, c Context, log *log.Logger) {
return func(res http.ResponseWriter, req *http.Request, c Context, log logger) {
start := time.Now()

addr := req.Header.Get("X-Real-IP")
Expand Down
5 changes: 2 additions & 3 deletions martini.go
Expand Up @@ -31,7 +31,7 @@ type Martini struct {
inject.Injector
handlers []Handler
action Handler
logger *log.Logger
logger logger
}

// New creates a bare bones Martini instance. Use this method if you want to have full control over the middleware that is used.
Expand All @@ -58,7 +58,7 @@ func (m *Martini) Action(handler Handler) {
}

// Logger sets the logger
func (m *Martini) Logger(logger *log.Logger) {
func (m *Martini) Logger(logger logger) {
m.logger = logger
m.Map(m.logger)
}
Expand Down Expand Up @@ -118,7 +118,6 @@ type ClassicMartini struct {
func Classic() *ClassicMartini {
r := NewRouter()
m := New()
m.Use(Logger())
m.Use(Recovery())
m.Use(Static("public"))
m.MapTo(r, (*Routes)(nil))
Expand Down
3 changes: 1 addition & 2 deletions recovery.go
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"runtime"

Expand Down Expand Up @@ -113,7 +112,7 @@ func function(pc uintptr) []byte {
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
// While Martini is in development mode, Recovery will also output the panic as HTML.
func Recovery() Handler {
return func(c Context, log *log.Logger) {
return func(c Context, log logger) {
defer func() {
if err := recover(); err != nil {
stack := stack(3)
Expand Down