logger

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 26, 2020 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Copyright © 2020 Arnoud Kleinloog <arnoud@kleinloog.ch>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Example

This example uses command-line flags to demonstrate various outputs depending on the chosen log level.

package main

import (
	"flag"
	"github.com/akleinloog/http-logger/util/logger"
	"time"

	"github.com/rs/zerolog"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() *logger.Logger {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}

	return logger.NewConsole(true)
}

func main() {
	l := setup()
	debug := flag.Bool("debug", false, "sets log level to debug")

	flag.Parse()

	// Default level for this example is info, unless debug flag is present
	zerolog.SetGlobalLevel(zerolog.InfoLevel)
	if *debug {
		zerolog.SetGlobalLevel(zerolog.DebugLevel)
	}

	l.Debug().Msg("This message appears only when log level set to Debug")
	l.Info().Msg("This message appears when log level set to Debug or Info")

	if e := l.Debug(); e.Enabled() {
		// Compute log output only if enabled.
		value := "bar"
		e.Str("foo", value).Msg("some debug message")
	}

}
Output:

{"level":"info","time":1199811905,"message":"This message appears when log level set to Debug or Info"}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	DefaultLogger = New(config.DefaultConfig)
)

Functions

func IsJSON

func IsJSON(str string) bool

func RequestLogger

func RequestLogger(next http.Handler) http.Handler

RequestLogger is a middleware that logs the start and end of each request, along with some useful data about what was requested, what the response status was, and how long it took to return. When standard output is a TTY, Logger will print in color, otherwise it will print in black and white. Logger prints a request ID if one is provided.

Alternatively, look at https://github.com/goware/httplog for a more in-depth http logger with structured logging support.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is used for logging.

func New

func New(config *config.Config) *Logger

New initializes a new logger

func (*Logger) Ctx

func (l *Logger) Ctx(ctx context.Context) *Logger

Ctx returns the Logger associated with the ctx. If no logger is associated, a disabled logger is returned.

func (*Logger) Debug

func (l *Logger) Debug() *zerolog.Event

Debug starts a new message with debug level.

You must call Msg on the returned event in order to send the event.

func (*Logger) Error

func (l *Logger) Error() *zerolog.Event

Error starts a new message with error level.

You must call Msg on the returned event in order to send the event.

func (*Logger) Fatal

func (l *Logger) Fatal() *zerolog.Event

Fatal starts a new message with fatal level. The os.Exit(1) function is called by the Msg method.

You must call Msg on the returned event in order to send the event.

func (*Logger) Hook

func (l *Logger) Hook(h zerolog.Hook) zerolog.Logger

Hook returns a logger with the h Hook.

func (*Logger) Info

func (l *Logger) Info() *zerolog.Event

Info starts a new message with info level.

You must call Msg on the returned event in order to send the event.

func (*Logger) Level

func (l *Logger) Level(level zerolog.Level) zerolog.Logger

Level creates a child logger with the minimum accepted level set to level.

func (*Logger) Log

func (l *Logger) Log() *zerolog.Event

Log starts a new message with no level. Setting zerolog.GlobalLevel to zerolog.Disabled will still disable events produced by this method.

You must call Msg on the returned event in order to send the event.

func (*Logger) Output

func (l *Logger) Output(w io.Writer) zerolog.Logger

Output duplicates the global logger and sets w as its output.

func (*Logger) Panic

func (l *Logger) Panic() *zerolog.Event

Panic starts a new message with panic level. The message is also sent to the panic function.

You must call Msg on the returned event in order to send the event.

func (*Logger) Print

func (l *Logger) Print(v ...interface{})

Print sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Print.

func (*Logger) Printf

func (l *Logger) Printf(format string, v ...interface{})

Printf sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Sample

func (l *Logger) Sample(s zerolog.Sampler) zerolog.Logger

Sample returns a logger with the s sampler.

func (*Logger) Warn

func (l *Logger) Warn() *zerolog.Event

Warn starts a new message with warn level.

You must call Msg on the returned event in order to send the event.

func (*Logger) With

func (l *Logger) With() zerolog.Context

With creates a child logger with the field added to its context.

func (*Logger) WithLevel

func (l *Logger) WithLevel(level zerolog.Level) *zerolog.Event

WithLevel starts a new message with level.

You must call Msg on the returned event in order to send the event.

type RequestLogEntry

type RequestLogEntry struct {
	Method       string
	URL          string
	UserAgent    string
	Referer      string
	Protocol     string
	RemoteIP     string
	ServerIP     string
	Host         string
	Status       int
	Latency      time.Duration
	RequestBody  []byte
	ResponseBody string
	RequestId    string
}

Jump to

Keyboard shortcuts

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