Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Logger ¶
func FromContext ¶
func NewLogger ¶
NewLogger creates a new Logger configured with the specified parameters.
The level parameter configure the default logging level, the parameter is a string and the possible values are:
- `debug`: all the existing logs, usually disabled in production
- `info`: the default logging level
- `warn`: more important than Info, but don't need individual human review
- `error`: high-priority errors, if an application is running smoothly, it shouldn't generate any error-level logs
- `dpanic`: particularly important errors, in development the logger panics after writing the message
- `panic: logs a message, then panics
- `fatal`: logs a message, then calls os.Exit(1)
the production parameter configures the logger for the development environment, if the value is `false`, or for the production environment if the value is `true`
func (*Logger) JsonError ¶
JsonError generate the JSON data to send to the client in case of an error.
JsonError should be called every time you need to return an error to the REST client of your application.
The output will be formatted using the following json template:
{ "code": status code, "status": "textual representation of the status code", "message" "description", "timestamp": "now()", }
The message will only be populated when the description parameter is not an empty value.
The data will also be used by the Middleware function to add context to the logger output. The `description` value will be used as message for the log entry, and add the `err` value will be added as a field to the log body. After the log entry is written, the state of the Logger will be cleaned.
Note: if you need to add context to the logger, but without returning an error to the REST client, you should use the SetError function.
func (*Logger) Middleware ¶
Middleware is used to wrap other HTTP handlers, to log all the requests with the meaningful details needed for an HTTP server
func (*Logger) SetError ¶
SetError saves the error value and an associated message into the Logger state. These values will then be used in the Middleware function to add context to the HTTP error.
SetError should be called every time is important to display the original error text inside a specific log.
The data will be used by the Middleware function to add context to the logger output. The `description` value will be used as message for the log entry, and add the `err` value will be added as a field to the log body. After the log entry is written, the state of the Logger will be cleaned.
Note: if you also need to return an error to the REST client, you should use the JsonError function.
type StatusRecorder ¶
type StatusRecorder struct { http.ResponseWriter Status int Written int }
func (*StatusRecorder) WriteHeader ¶
func (r *StatusRecorder) WriteHeader(status int)