reactLog

package module
Version: v0.0.0-...-091f1c3 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2015 License: MIT Imports: 2 Imported by: 0

README

reactLog

Package reactLog is reaction middle-ware for standard golang log.

Build Status Code Coverage Documentation

Basic usage:

reactLogger := reactLog.New(os.Stderr)

copyBuf := &bytes.Buffer{}
reactLogger.AddReaction("user ID 85", &reactLog.Copy{copyBuf})

log.SetOutput(reactLogger)

log.Println("This is regular log message")
log.Println("This error message concers user ID 85 and will be copied to copyBuf.")

reactLog concept is to filter and add additional functionality to log based on log message content.

If used in main package it enhance log globally with the use of log.SetOutput method.

Any number of trigger words can be registered using AddReaction method each with it's own Reactor.

Reactor is the interface that wraps the Reaction method. reactLog comes with few types that already implements Reactor interface:

  • Discard for discarding log messages.
  • Redirect to redirect log messages to other io.Writer.
  • Copy to write log message both to underlying io.Writer and additional io.Writer.

Feel free to create Reactors for you specific use case by implementing Reactor interface.

See Documentation for more info.

Documentation

Overview

Package reactLog is reaction middleware for standard log.

Basic usage:

	reactLogger := reactLog.New(os.Stderr)

    copyBuf := &bytes.Buffer{}
	reactLogger.AddReaction("user ID 85", &reactLog.Copy{copyBuf})

	log.SetOutput(reactLogger)

	log.PrintLn("This is regular log message")
	log.PrintLn("This error message concers user ID 85 and will be copied to copyBuf.")

reactLog concept is to filter and add additional functionality to log based on log message content. If used in main package it enhance log globally with the use of log.SetOutput method. Any number of trigger words can be registered using AddReaction method each with it's own Reactor.

Reactor is the interface that wraps the Reaction method. reactLog comes with few types that already implements Reactor interface:

Discard for discarding log messages.
Redirect to redirect log messages to other io.Writer.
Copy to write log message both to underlying io.Writer and additional io.Writer.

Feel free to create Reactors for you specific use case by implementing Reactor interface.

See Examples for more info.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Copy

type Copy struct {
	Out io.Writer
}

Copy type implements Reactor with copy functionality. It will copy log to given io.Writer.

Example
package main

import (
	"bytes"
	"fmt"
	"log"
	"os"

	"github.com/MatejB/reactLog"
)

func main() {
	logContainerForUser107 := &bytes.Buffer{} // it can be any io.Writer eq. file

	reactLogger := reactLog.New(os.Stdout) // use os.Stderr for default log functionality
	reactLogger.AddReaction("user ID 107", &reactLog.Copy{logContainerForUser107})

	logger := log.New(reactLogger, "", 0)

	logger.Println("Log concerning user ID 107 with extra data")
	logger.Println("INFO dummy log")

	fmt.Println(logContainerForUser107) // in logContainerForUser107 is copy of log just for USER_107
}
Output:

Log concerning user ID 107 with extra data
INFO dummy log
Log concerning user ID 107 with extra data

func (*Copy) Reaction

func (c *Copy) Reaction(logLine []byte) (passOut bool, err error)

Reaction is to copy log to other writer if trigger is found

type Discard

type Discard struct{}

Discard type implements Reactor with discard functionality.

Example
package main

import (
	"log"
	"os"

	"github.com/MatejB/reactLog"
)

func main() {
	reactLogger := reactLog.New(os.Stdout) // use os.Stderr for default log functionality
	reactLogger.AddReaction("INFO", &reactLog.Discard{})

	logger := log.New(reactLogger, "", 0)
	logger.Println("INFO this will NOT be written")
	logger.Println("ERROR this will be written")
}
Output:

ERROR this will be written

func (*Discard) Reaction

func (d *Discard) Reaction(logLine []byte) (passOut bool, err error)

Reaction is to disacard log if trigger is found

type Logger

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

A Logger is logging object that passes writes to given io.Writer if no appropriate reaction is found or reaction returns true.

func New

func New(out io.Writer) *Logger

New creates a new Logger. Pass-through io.Writer must be given, os.Stderr in most cases.

func (*Logger) AddReaction

func (l *Logger) AddReaction(trigger string, reaction Reactor)

AddReaction adds reaction to be executed when trigger is encountered in log line.

func (*Logger) Write

func (l *Logger) Write(p []byte) (n int, err error)

type Reactor

type Reactor interface {
	Reaction(logLine []byte) (passOut bool, err error)
}

Reactor is the interface that wraps the Reaction method.

Reaction decides if logLine is to be written to underlying io.Writer object by returning true.

type Redirect

type Redirect struct {
	Out io.Writer
}

Redirect type implements Reactor with redirection functionality. It will redirect log output to given io.Writer

Example
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"

	"github.com/MatejB/reactLog"
)

func main() {
	logContainerForUser107 := &bytes.Buffer{} // it can be any io.Writer eq. file

	reactLogger := reactLog.New(ioutil.Discard) // use os.Stderr for default log functionality
	reactLogger.AddReaction("user ID 107", &reactLog.Redirect{logContainerForUser107})

	logger := log.New(reactLogger, "", 0)

	logger.Println("INFO dummy log 1")
	logger.Println("ERROR dummy log 2")

	logger.Println("Log concerning user ID 107 with extra data")

	logger.Println("INFO dummy log 3")
	logger.Println("ERROR dummy log 4")

	fmt.Println(logContainerForUser107)
}
Output:

Log concerning user ID 107 with extra data

func (*Redirect) Reaction

func (r *Redirect) Reaction(logLine []byte) (passOut bool, err error)

Reaction is to redirect log to other writer if trigger is found

Source Files

Jump to

Keyboard shortcuts

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