├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── examples_test.go ├── reactLog.go └── reactLog_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Matej Baćo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # reactLog 2 | Package reactLog is reaction middle-ware for standard golang log. 3 | 4 | [![Build Status](https://travis-ci.org/MatejB/reactLog.svg)](https://travis-ci.org/MatejB/reactLog) [![Code Coverage](http://gocover.io/_badge/github.com/MatejB/reactLog)](http://gocover.io/github.com/MatejB/reactLog) [![Documentation](https://godoc.org/github.com/MatejB/reactLog?status.svg)](https://godoc.org/github.com/MatejB/reactLog) 5 | 6 | Basic usage: 7 | ```go 8 | reactLogger := reactLog.New(os.Stderr) 9 | 10 | copyBuf := &bytes.Buffer{} 11 | reactLogger.AddReaction("user ID 85", &reactLog.Copy{copyBuf}) 12 | 13 | log.SetOutput(reactLogger) 14 | 15 | log.Println("This is regular log message") 16 | log.Println("This error message concers user ID 85 and will be copied to copyBuf.") 17 | ``` 18 | 19 | reactLog concept is to filter and add additional functionality to log 20 | based on log message content. 21 | 22 | If used in main package it enhance log globally with the use of log.SetOutput method. 23 | 24 | Any number of trigger words can be registered using AddReaction 25 | method each with it's own Reactor. 26 | 27 | Reactor is the interface that wraps the Reaction method. 28 | reactLog comes with few types that already implements Reactor interface: 29 | * Discard for discarding log messages. 30 | * Redirect to redirect log messages to other io.Writer. 31 | * Copy to write log message both to underlying io.Writer and additional io.Writer. 32 | 33 | Feel free to create Reactors for you specific use case by implementing Reactor interface. 34 | 35 | See [Documentation](https://godoc.org/github.com/MatejB/reactLog) for more info. 36 | -------------------------------------------------------------------------------- /examples_test.go: -------------------------------------------------------------------------------- 1 | package reactLog_test 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "io/ioutil" 7 | "log" 8 | "os" 9 | 10 | "github.com/MatejB/reactLog" 11 | ) 12 | 13 | func ExampleDiscard() { 14 | reactLogger := reactLog.New(os.Stdout) // use os.Stderr for default log functionality 15 | reactLogger.AddReaction("INFO", &reactLog.Discard{}) 16 | 17 | logger := log.New(reactLogger, "", 0) 18 | logger.Println("INFO this will NOT be written") 19 | logger.Println("ERROR this will be written") 20 | // Output: ERROR this will be written 21 | } 22 | 23 | func ExampleRedirect() { 24 | logContainerForUser107 := &bytes.Buffer{} // it can be any io.Writer eq. file 25 | 26 | reactLogger := reactLog.New(ioutil.Discard) // use os.Stderr for default log functionality 27 | reactLogger.AddReaction("user ID 107", &reactLog.Redirect{logContainerForUser107}) 28 | 29 | logger := log.New(reactLogger, "", 0) 30 | 31 | logger.Println("INFO dummy log 1") 32 | logger.Println("ERROR dummy log 2") 33 | 34 | logger.Println("Log concerning user ID 107 with extra data") 35 | 36 | logger.Println("INFO dummy log 3") 37 | logger.Println("ERROR dummy log 4") 38 | 39 | fmt.Println(logContainerForUser107) 40 | // Output: Log concerning user ID 107 with extra data 41 | } 42 | 43 | func ExampleCopy() { 44 | logContainerForUser107 := &bytes.Buffer{} // it can be any io.Writer eq. file 45 | 46 | reactLogger := reactLog.New(os.Stdout) // use os.Stderr for default log functionality 47 | reactLogger.AddReaction("user ID 107", &reactLog.Copy{logContainerForUser107}) 48 | 49 | logger := log.New(reactLogger, "", 0) 50 | 51 | logger.Println("Log concerning user ID 107 with extra data") 52 | logger.Println("INFO dummy log") 53 | 54 | fmt.Println(logContainerForUser107) // in logContainerForUser107 is copy of log just for USER_107 55 | // Output: 56 | // Log concerning user ID 107 with extra data 57 | // INFO dummy log 58 | // Log concerning user ID 107 with extra data 59 | } 60 | -------------------------------------------------------------------------------- /reactLog.go: -------------------------------------------------------------------------------- 1 | /* 2 | Package reactLog is reaction middleware for standard log. 3 | 4 | Basic usage: 5 | reactLogger := reactLog.New(os.Stderr) 6 | 7 | copyBuf := &bytes.Buffer{} 8 | reactLogger.AddReaction("user ID 85", &reactLog.Copy{copyBuf}) 9 | 10 | log.SetOutput(reactLogger) 11 | 12 | log.PrintLn("This is regular log message") 13 | log.PrintLn("This error message concers user ID 85 and will be copied to copyBuf.") 14 | 15 | reactLog concept is to filter and add additional functionality to log 16 | based on log message content. 17 | If used in main package it enhance log globally with the use of log.SetOutput method. 18 | Any number of trigger words can be registered using AddReaction 19 | method each with it's own Reactor. 20 | 21 | Reactor is the interface that wraps the Reaction method. 22 | reactLog comes with few types that already implements Reactor interface: 23 | Discard for discarding log messages. 24 | Redirect to redirect log messages to other io.Writer. 25 | Copy to write log message both to underlying io.Writer and additional io.Writer. 26 | Feel free to create Reactors for you specific use case by implementing Reactor interface. 27 | 28 | See Examples for more info. 29 | */ 30 | package reactLog 31 | 32 | import ( 33 | "bytes" 34 | "io" 35 | ) 36 | 37 | // A Logger is logging object that passes writes to given 38 | // io.Writer if no appropriate reaction is found or reaction 39 | // returns true. 40 | type Logger struct { 41 | out io.Writer 42 | reactors map[string]Reactor 43 | } 44 | 45 | // New creates a new Logger. Pass-through io.Writer 46 | // must be given, os.Stderr in most cases. 47 | func New(out io.Writer) *Logger { 48 | return &Logger{out: out, reactors: make(map[string]Reactor)} 49 | } 50 | 51 | func (l *Logger) Write(p []byte) (n int, err error) { 52 | allTriggers: 53 | for key := range l.reactors { 54 | trigger := []byte(key) 55 | if len(trigger) > len(p) { 56 | continue allTriggers 57 | } 58 | 59 | // find start 60 | maxPos := len(p) - len(trigger) + 2 61 | for i := 0; i < maxPos; i = i + 1 { 62 | if p[i] == trigger[0] { 63 | if bytes.Equal(p[i:i+len(trigger)], trigger) { 64 | output, err := l.reactors[key].Reaction(p) 65 | if err != nil { 66 | return 0, err 67 | } 68 | if output { 69 | n, err = l.out.Write(p) 70 | } 71 | return n, nil 72 | } 73 | } 74 | } 75 | } 76 | 77 | // default behaviour 78 | n, err = l.out.Write(p) 79 | 80 | return 81 | } 82 | 83 | // Reactor is the interface that wraps the Reaction method. 84 | // 85 | // Reaction decides if logLine is to be written to underlying 86 | // io.Writer object by returning true. 87 | type Reactor interface { 88 | Reaction(logLine []byte) (passOut bool, err error) 89 | } 90 | 91 | // AddReaction adds reaction to be executed when 92 | // trigger is encountered in log line. 93 | func (l *Logger) AddReaction(trigger string, reaction Reactor) { 94 | l.reactors[trigger] = reaction 95 | } 96 | 97 | // Discard type implements Reactor with discard functionality. 98 | type Discard struct{} 99 | 100 | // Reaction is to disacard log if trigger is found 101 | func (d *Discard) Reaction(logLine []byte) (passOut bool, err error) { 102 | return false, nil 103 | } 104 | 105 | // Redirect type implements Reactor with redirection functionality. 106 | // It will redirect log output to given io.Writer 107 | type Redirect struct { 108 | Out io.Writer 109 | } 110 | 111 | // Reaction is to redirect log to other writer if trigger is found 112 | func (r *Redirect) Reaction(logLine []byte) (passOut bool, err error) { 113 | _, err = r.Out.Write(logLine) 114 | return false, err 115 | } 116 | 117 | // Copy type implements Reactor with copy functionality. 118 | // It will copy log to given io.Writer. 119 | type Copy struct { 120 | Out io.Writer 121 | } 122 | 123 | // Reaction is to copy log to other writer if trigger is found 124 | func (c *Copy) Reaction(logLine []byte) (passOut bool, err error) { 125 | _, err = c.Out.Write(logLine) 126 | return true, err 127 | } 128 | -------------------------------------------------------------------------------- /reactLog_test.go: -------------------------------------------------------------------------------- 1 | package reactLog 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | ) 7 | 8 | func TestSimpleWrite(t *testing.T) { 9 | buf := &bytes.Buffer{} 10 | 11 | logger := New(buf) 12 | 13 | _, err := logger.Write([]byte("This is a test")) 14 | if err != nil { 15 | t.Fatal("Unxepceted error, ", err) 16 | } 17 | 18 | if buf.String() != "This is a test" { 19 | t.Fatalf("Expected %v, recived %v", "This is a test", buf.String()) 20 | } 21 | } 22 | 23 | func TestDiscard(t *testing.T) { 24 | buf := &bytes.Buffer{} 25 | 26 | logger := New(buf) 27 | logger.AddReaction("INFO", &Discard{}) 28 | 29 | _, err := logger.Write([]byte("INFO This is a test")) 30 | if err != nil { 31 | t.Fatal("Unxepceted error, ", err) 32 | } 33 | if buf.String() != "" { 34 | t.Fatalf("Expected %v, recived %v", "", buf.String()) 35 | } 36 | 37 | _, err = logger.Write([]byte("ERROR This is a test")) 38 | if err != nil { 39 | t.Fatal("Unxepceted error, ", err) 40 | } 41 | if buf.String() != "ERROR This is a test" { 42 | t.Fatalf("Expected %v, recived %v", "ERROR This is a test", buf.String()) 43 | } 44 | } 45 | 46 | func TestRedirect(t *testing.T) { 47 | buf := &bytes.Buffer{} 48 | redirectBuf := &bytes.Buffer{} 49 | 50 | logger := New(buf) 51 | logger.AddReaction("INFO", &Redirect{redirectBuf}) 52 | 53 | _, err := logger.Write([]byte("INFO This is a test")) 54 | if err != nil { 55 | t.Fatal("Unxepceted error, ", err) 56 | } 57 | if buf.String() != "" { 58 | t.Fatalf("Expected %v, recived %v", "", buf.String()) 59 | } 60 | if redirectBuf.String() != "INFO This is a test" { 61 | t.Fatalf("Expected %v, recived %v", "INFO This is a test", redirectBuf.String()) 62 | } 63 | 64 | redirectBuf.Reset() 65 | _, err = logger.Write([]byte("ERROR This is a test")) 66 | if err != nil { 67 | t.Fatal("Unxepceted error, ", err) 68 | } 69 | if buf.String() != "ERROR This is a test" { 70 | t.Fatalf("Expected %v, recived %v", "ERROR This is a test", buf.String()) 71 | } 72 | if redirectBuf.String() != "" { 73 | t.Fatalf("Expected %v, recived %v", "", redirectBuf.String()) 74 | } 75 | } 76 | 77 | func TestCopy(t *testing.T) { 78 | buf := &bytes.Buffer{} 79 | copyBuf := &bytes.Buffer{} 80 | 81 | logger := New(buf) 82 | logger.AddReaction("user ID 85", &Copy{copyBuf}) 83 | 84 | _, err := logger.Write([]byte("This error message concers user ID 85 and will be copied to copyBuf.")) 85 | if err != nil { 86 | t.Fatal("Unxepceted error, ", err) 87 | } 88 | if buf.String() != "This error message concers user ID 85 and will be copied to copyBuf." { 89 | t.Fatalf("Expected %v, recived %v", "This error message concers user ID 85 and will be copied to copyBuf.", buf.String()) 90 | } 91 | if copyBuf.String() != "This error message concers user ID 85 and will be copied to copyBuf." { 92 | t.Fatalf("Expected %v, recived %v", "This error message concers user ID 85 and will be copied to copyBuf.", copyBuf.String()) 93 | } 94 | } 95 | 96 | func TestNonWord(t *testing.T) { 97 | buf := &bytes.Buffer{} 98 | 99 | logger := New(buf) 100 | logger.AddReaction("custom string sample", &Discard{}) 101 | 102 | _, err := logger.Write([]byte("this is complex custom string sample sentence")) 103 | if err != nil { 104 | t.Fatal("Unxepceted error, ", err) 105 | } 106 | if buf.String() != "" { 107 | t.Fatalf("Expected %v, recived %v", "", buf.String()) 108 | } 109 | } 110 | 111 | func TestPositions(t *testing.T) { 112 | buf := &bytes.Buffer{} 113 | 114 | logger := New(buf) 115 | logger.AddReaction("TRIGGER WORD", &Discard{}) 116 | 117 | _, err := logger.Write([]byte("TRIGGER WORD at the begining")) 118 | if err != nil { 119 | t.Fatal("Unxepceted error, ", err) 120 | } 121 | if buf.String() != "" { 122 | t.Fatalf("Expected %v, recived %v", "", buf.String()) 123 | } 124 | 125 | buf.Reset() 126 | _, err = logger.Write([]byte("At the end is TRIGGER WORD")) 127 | if err != nil { 128 | t.Fatal("Unxepceted error, ", err) 129 | } 130 | if buf.String() != "" { 131 | t.Fatalf("Expected %v, recived %v", "", buf.String()) 132 | } 133 | 134 | buf.Reset() 135 | _, err = logger.Write([]byte("In the middle is TRIGGER WORD is'n it")) 136 | if err != nil { 137 | t.Fatal("Unxepceted error, ", err) 138 | } 139 | if buf.String() != "" { 140 | t.Fatalf("Expected %v, recived %v", "", buf.String()) 141 | } 142 | } 143 | 144 | func TestUtf8(t *testing.T) { 145 | buf := &bytes.Buffer{} 146 | 147 | logger := New(buf) 148 | logger.AddReaction("ČćžđšŽĆš", &Discard{}) 149 | 150 | _, err := logger.Write([]byte("This are croatian charcaters ČćžđšŽĆš")) 151 | if err != nil { 152 | t.Fatal("Unxepceted error, ", err) 153 | } 154 | if buf.String() != "" { 155 | t.Fatalf("Expected %v, recived %v", "", buf.String()) 156 | } 157 | } 158 | --------------------------------------------------------------------------------