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 ¶
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
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
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 (*Logger) AddReaction ¶
AddReaction adds reaction to be executed when trigger is encountered in log line.
type Reactor ¶
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 ¶
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