consolelog

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

Console writer for Zerolog

This is a wrapper around zerolog's console writer to provide better readability when printing logs to the console. The package doesn't come with many colorize options or field formatting but it does provide interfaces to modify them, which is what this package does.

At some point in any application there's going to be various preferences and needs in terms of log output and debugging, so the hope is that this package allows for easier extension of the underlying logger.

You can consume it similar to below:

package main

import (
  "github.com/rs/zerolog"

  "github.com/theopenlane/core/pkg/logx/consolelog"
)

func main() {
  output := consolelog.NewConsoleWriter()
  logger := zerolog.New(output).With().Timestamp().Logger()

  logger.Info().Str("foo", "bar").Msg("Hello world")

  // => 3:50PM INF Hello world foo=bar
}
Custom configuration
package main

import (
  "fmt"
  "strings"
  "time"

  "github.com/rs/zerolog"

  "github.com/theopenlane/core/pkg/logx/consolelog"
)

func main() {
  output := consolelog.NewConsoleWriter(
    // Customize time formatting
    //
    func(w *consolelog.ConsoleWriter) {
      w.TimeFormat = time.Stamp
    },
    // Customize "level" formatting
    //
    func(w *consolelog.ConsoleWriter) {
      w.SetFormatter(
        zerolog.LevelFieldName,
        func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%-5s", i)) })
    },
  )

  logger := zerolog.New(output).With().Timestamp().Logger()

  logger.Info().Str("foo", "bar").Msg("Hello world")

  // => Jul 19 15:50:00 INFO  Hello world foo=bar
}

Documentation

Overview

Package console log is a zerolog consolewriter output formatter that can be used generically with any zerolog instantiation so that it's not specific to a particular application

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConsoleWriter

type ConsoleWriter struct {
	Out        io.Writer
	TimeFormat string
	PartsOrder []string

	FieldsExclude []string
	// contains filtered or unexported fields
}

ConsoleWriter parses the JSON input and writes an ANSI-colorized output to out

func NewConsoleWriter

func NewConsoleWriter(options ...func(w *ConsoleWriter)) ConsoleWriter

NewConsoleWriter creates and initializes a new ConsoleWriter

Example
package main

import (
	"github.com/rs/zerolog"
	"github.com/theopenlane/core/pkg/logx/consolelog"
)

func main() {
	output := consolelog.NewConsoleWriter()
	logger := zerolog.New(output)

	logger.Info().Str("foo", "bar").Msg("hello world")
}
Output:

INF hello world foo=bar
Example (Custom)
package main

import (
	"fmt"
	"strings"
	"time"

	"github.com/rs/zerolog"
	"github.com/theopenlane/core/pkg/logx/consolelog"
)

func main() {
	output := consolelog.NewConsoleWriter(
		// Customize time formatting
		//
		func(w *consolelog.ConsoleWriter) {
			w.TimeFormat = time.RFC822
		},
		// Customize "level" formatting
		//
		func(w *consolelog.ConsoleWriter) {
			w.SetFormatter(
				zerolog.LevelFieldName,
				func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%-5s", i)) })
		},
	)

	logger := zerolog.New(output).With().Timestamp().Logger()

	logger.Info().Str("foo", "bar").Msg("hello world")
	// => 19 Jul 18 15:50 CEST INFO  hello world foo=bar
}
Output:

func (ConsoleWriter) Formatter

func (w ConsoleWriter) Formatter(id string) Formatter

Formatter returns a formatter by id or the default formatter if none is found

func (ConsoleWriter) SetFormatter

func (w ConsoleWriter) SetFormatter(id string, f Formatter)

SetFormatter registers a formatter function by id

func (ConsoleWriter) Write

func (w ConsoleWriter) Write(p []byte) (n int, err error)

Write appends the output to Out.

type Formatter

type Formatter func(interface{}) string

Formatter transforms the input into a string

Jump to

Keyboard shortcuts

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