# README
logging
Logging is a collection of collection of packages to make easy the logging experience.
Logging is currently in active development and it's API WILL CHANGE WITHOUT NOTICE.
logging/httplog - the HTTP middleware
Httplog implements and HTTP middleware that can optionally log the raw request and responses.
package main
import (
"fmt"
"log"
"net/http"
"github.com/smallstep/logging"
"github.com/smallstep/logging/httplog"
)
func main() {
logger, err := logging.New("scim",
logging.WithLogResponses(),
logging.WithLogRequests(),
)
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello World!")
})
srv := &http.Server{
Addr: ":8080",
Handler: httplog.Middleware(logger, http.DefaultServeMux),
ErrorLog: logger.StdLogger(logging.ErrorLevel),
}
logger.Infof("start listening at %s.", srv.Addr)
log.Fatal(srv.ListenAndServe())
}
A simple curl http://localhost:8080
will print the following logs:
$ go run examples/httplog.go
{"level":"info","ts":1582944317.382856,"caller":"logging/logger.go:87","msg":"start listening at :8080."}
{"level":"info","ts":1582944330.861353,"caller":"httplog/handler.go:121","msg":"","name":"scim","request-id":"bpct0ijipt3avli7utp0","remote-address":"::1","time":"2020-02-28T18:45:30-08:00","duration":0.000064785,"duration-ns":64785,"method":"GET","path":"/","protocol":"HTTP/1.1","status":200,"size":13,"referer":"","user-agent":"curl/7.64.1","request":"R0VUIC8gSFRUUC8xLjENCkhvc3Q6IGxvY2FsaG9zdDo4MDgwDQpBY2NlcHQ6ICovKg0KVXNlci1BZ2VudDogY3VybC83LjY0LjENClgtVHJhY2UtSWQ6IGJwY3QwaWppcHQzYXZsaTd1dHAwDQoNCg==","response":"SGVsbG8gV29ybGQhCg=="}
# Functions
FromContext returns a logger from the given context.
GetName returns the log name from the context if it exists.
GetTraceparent returns the tracing id from the context if it exists.
New initializes the logger with the given options.
NewContext adds the given logger to the context.
NewTraceparent generates a new traceparent.
Tracing returns a new middleware that gets the given header and sets it in the context so it can be written in the logger.
WithCallerSkip increases the number of callers skipped by caller annotation.
WithConfig uses a JSON to configure the logger.
WithFormatJSON configures the format of the logs as JSON.
WithFormatText configures the format of the logs as text.
WithLogLevel sets the verbosity of the logger.
WithLogRequests enables the log of the requests.
WithLogResponses enables the log of responses.
WithName returns a new context with the given name in the context.
WithTimeFormat sets a specific format for the time fields.
WithTraceHeader defines the name of the header used for tracing.
WithTraceparent returns a new context with the given tracing id added to the context.
# Constants
No description provided by the author
DefaultTraceHeader is the default header used as a trace id.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author