zlogger

package
v0.0.0-...-533ea73 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewContext

func NewContext(parent context.Context, z *Logger) context.Context

Types

type Logger

type Logger struct {
	*zap.Logger
	// contains filtered or unexported fields
}

func FromContext

func FromContext(ctx context.Context) *Logger

func NewLogger

func NewLogger(level string, production bool) (*Logger, error)

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

func (l *Logger) JsonError(w http.ResponseWriter, code int, description string, err error)

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

func (l *Logger) Middleware(next http.Handler) http.Handler

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

func (l *Logger) SetError(description string, err error)

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.

func (*Logger) Sync

func (l *Logger) Sync()

Sync synchronize the logger output by flushing any buffered log entries. Applications should take care to call Sync before exiting.

type StatusRecorder

type StatusRecorder struct {
	http.ResponseWriter
	Status  int
	Written int
}

func (*StatusRecorder) Write

func (r *StatusRecorder) Write(b []byte) (int, error)

func (*StatusRecorder) WriteHeader

func (r *StatusRecorder) WriteHeader(status int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL