-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Added option to disable logging when serving static assets #147
Conversation
I did some more thinking and realized it's probably better (and more Go-idiomatic?) to use a variadic struct as optional parameter for Static(), similar to how it's handled in render.go. Example usage:
|
cool. I will take a look at this very soon |
Here is a simpler way to do Export defaultReturnHandler func DefaultReturnHandler() ReturnHandler {
// ....
} Add NewSimple type devNull struct{}
func (devNull) Write(p []byte) (int, error) {
return len(p), nil
}
func NewSimple() *Martini {
null, _ := os.Open(os.DevNull) // or null := new(devNull)
m := &Martini{inject.New(), []Handler{}, func() {}, log.New(null, "", 0)}
m.Map(m.logger)
return m
} Example usage: m := martini.NewSimple()
m.Use(logger) // handler
m.Map(myLogger)
m.Map(martini.DefaultReturnHandler()) |
@achun in-case you weren't aware, you can probably replace |
From my understanding this seems as it unfortunately would not do what I needed. I want to stop just the Static handler from logging. ("[Static] Serving ...") It seemed to me to be the simplest way to add an option to the Static handler itself. But anyway, by now in the last change I also added further options as a StaticOptions struct, so it's becomes possible to define the index file for example. I was thinking that maybe if breaking backwards compatibility is still ok (?), to also move the 'directory' string into StaticOptions?
|
@JamesClonk StaticOptions and injector in different code style. BTW: m := &martini.ClassicMartini{martini.New(),martini.NewRouter()}
m.Action(m.Handle)
// m.Map(something)
m.Use(MyLogger())
m.Use(MyRecovery())
m.Use(MyStatic("public"))
http.ListenAndServe(":80", m) |
@@ -9,12 +9,14 @@ import ( | |||
// 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) { | |||
start := time.Now() | |||
log.Printf("Started %s %s", req.Method, req.URL.Path) | |||
if !c.Written() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of this call here? With how Martini works, this handler shouldn't be called if the context is written right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just ran your newly added test without this check and it still passes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, yeah. I noticed that now too. Doh!
Removed it. ;)
This is a welcome change. I think i would prefer to not break changes if it really isn't needed. |
fix those nits and I will land it |
Conflicts: static_test.go
I had to add another check to static.go:
Otherwise when turning off serving an index file would be bad for Martini. Requesting http://localhost:3000/ would not work anymore, since "/" is a directory. |
After some more thinking I removed the option to skip serving index files from StaticOptions. There's just not really any useful situation for this, and it greatly simplifies my changes to static.go and static_test.go |
Awesome. I'm down with that Sent from my iPhone
|
This looks great. Merging! |
Added option to disable logging when serving static assets
I added an optional parameter to Static(), to disable logging when serving static assets.
The reason for this is that my current project has a lot of static CSS, JS, etc. files that are served with each GET request. This produces a lot of "spam" that I don't want / need to have in my logfile/stdout.
The new variadic bool parameter for Static() is called skipLogging.
If it is set to true, no logging output will be produced. Since the default for a missing parameter / bool is false, this change is fully backwards compatible.
Example:
vs. before: