Documentation ¶
Overview ¶
Example ¶
package main import ( "net/http" "time" "github.com/Hunsin/beaver/httplog" ) var handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // do your staff w.WriteHeader(http.StatusOK) w.Write([]byte("OK")) }) func main() { // create a log instance which writes data to stdout l := httplog.New(nil) // set output to a specific file, creating it if not existed l.File("http.log") // or, simply call httplog.File() l = httplog.File("http.log") // you can chain you configurations l.File("new.log").Prefix("my-app").TimeFormat(time.RFC1123) // use the logger as HTTP middleware http.ListenAndServe(":8000", l.Listen(handler)) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
A Logger represents an logging object that records a series of HTTP requests, writing data to an io.Writer. It guarantees to serialize access to the Writer.
func New ¶
New returns an initialized Logger which output destination is w. If w is nil, the os.Stdout is applied. The default time format is set to time.RFC3339Nano.
func (*Logger) File ¶
File sets the output destination to the named file. If the file doesn't exist, a new file with permission mode 0644 is created. If any error occurred when opening the file, it panics.
func (*Logger) Listen ¶
func (l *Logger) Listen(h http.Handler) http.HandlerFunc
Listen is a http middleware that records the request event and calls the given http.Handler.
func (*Logger) Recover ¶
func (l *Logger) Recover(h PanicHandler) *Logger
Recover sets the PanicHandler of l. If h is nil, a default handler is applied. In other words, the handler of l can not be removed once it is set.
func (*Logger) TimeFormat ¶
TimeFormat sets the time format for the logger. The format rule follows the function Time.Format.
type PanicHandler ¶
type PanicHandler func(http.ResponseWriter, *http.Request, interface{})
A PanicHandler handles HTTP requests and panics recovered from http handlers.