replay

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

README

The Replay Backend for Deck

The replay backend allows log messages to be recorded and replayed.

The replay backend supports all platforms.

Init

The replay backend does not take any setup parameters.

Attributes

The replay backend does not utilize any custom attributes.

Details & Features

The replay backend is particularly useful as part of a testing framework, when the code under test is already instrumented with deck.

Log Storage & Retrieval

Once attached to a deck, the replay backend will store any newly logged messages in memory. Messages are indexed by their deck level and can be retrieved from the backend using the corresponding level functions (Info(), Error(), etc.). The All() function returns all collected messages in the order they were recorded.

Each retrieval function provides the results in a Bundle. The Bundle is an ordered list of log entries as they were processed by the deck. The Bundle also provides some helper functions to simplify searching the message contents.

Replay always returns a copy of the original Bundle each time a retrieval function is called. The underlying Bundle is kept private to avoid potential conflicts between new log events and user activity.

Bundle Helpers

Bundle.ContainsRE allows the user to use a regular expression to search for matching messages in the Bundle. The Bundle iterates over all messages in the list and attempts to match re, returning true if found.

Bundle.ContainsString allows the user to search the Bundle for a string. This leverages strings.Contains, so substrings are matched as well. The function returns true if a match exists in any of the messages.

Usage

import (
  github.com/google/deck
  github.com/google/deck/backends/replay
)

...
func TestSomething(t *testing.T) {
  d := deck.New()
  r := replay.Init()
  d.Add(r)
  ... execute code that creates logs ...
  if !r.Info().Contains("an expected log event") {
    t.Errorf("expected log event not found")
  }
}

Documentation

Overview

Package replay provides a deck backend for replaying log messages.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DEFAULT is a deck level for any messages not caught as coming from one of the standard levels.
	DEFAULT deck.Level = 1000
)

Functions

This section is empty.

Types

type Bundle

type Bundle []Log

Bundle aggregates message entries as they're written to the deck. Each Replay instance keeps an internal Bundle, and returns copies of the Bundle to the user when queried.

func (Bundle) ContainsRE

func (b Bundle) ContainsRE(re *regexp.Regexp) bool

ContainsRE searches the Bundle for a message which matches the regular expression re.

func (Bundle) ContainsString

func (b Bundle) ContainsString(str string) bool

ContainsString searches the Bundle for a string which contains str. This call uses strings.Contains which will match on substrings in addition to full strings.

func (Bundle) Len

func (b Bundle) Len() int

Len returns the length of the Bundle.

type Log

type Log struct {
	Level   deck.Level
	Message string
}

Log models a log entry as it's written to the Bundle. It tracks the log message but also other metadata that we may want to recall later, like the deck Level.

func (Log) String

func (e Log) String() string

String stringifies Log objects for nicer printing.

type Replay

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

Replay is a log deck backend that records log messages, allowing them to be replayed later.

func Init

func Init() *Replay

Init initializes the replay backend for use in a deck.

func (*Replay) All

func (r *Replay) All() Bundle

All returns all messages recorded to all levels.

func (*Replay) Close

func (r *Replay) Close() error

Close closes the replay backend.

func (*Replay) Debug

func (r *Replay) Debug() Bundle

Debug returns all messages recorded to the debug level.

func (*Replay) Error

func (r *Replay) Error() Bundle

Error returns all messages recorded to the error level.

func (*Replay) Fatal

func (r *Replay) Fatal() Bundle

Fatal returns all messages recorded to the fatal level.

func (*Replay) Info

func (r *Replay) Info() Bundle

Info returns all messages recorded to the info level.

func (*Replay) New

func (r *Replay) New(lvl deck.Level, msg string) deck.Composer

New creates a new replay message.

func (*Replay) Reset

func (r *Replay) Reset()

Reset resets the replay deck to its initial state.

func (*Replay) Warning

func (r *Replay) Warning() Bundle

Warning returns all messages recorded to the warning level.

Jump to

Keyboard shortcuts

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