diff --git a/LICENSE b/LICENSE index ff0d4ea..501a3ab 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2015 Charles Iliya Krempeaux :: http://changelog.ca/ +Copyright (c) 2015 Charles Iliya Krempeaux :: http://changelog.ca/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/context.go b/context.go deleted file mode 100644 index 64dbba2..0000000 --- a/context.go +++ /dev/null @@ -1,24 +0,0 @@ -package log - - -func newContext(cascade ...interface{}) map[string]interface{} { - - context := make(map[string]interface{}) - - for _,x := range cascade { - switch xx := x.(type) { - case map[string]string: - for key, value := range xx { - context[key] = value - } - case map[string]interface{}: - for key, value := range xx { - context[key] = value - } - case string: - context["text"] = xx - } - } - - return context -} diff --git a/context_test.go b/context_test.go deleted file mode 100644 index 1f5db50..0000000 --- a/context_test.go +++ /dev/null @@ -1,154 +0,0 @@ -package log - - -import ( - "testing" -) - - -func TestNewContext(t *testing.T) { - - tests := []struct{ - Cascade []interface{} - Expected map[string]interface{} - }{ - { - Cascade: []interface{}{}, - Expected: map[string]interface{}{}, - }, - - - { - Cascade: []interface{}{ - "apple", - }, - Expected: map[string]interface{}{ - "text":"apple", - }, - }, - { - Cascade: []interface{}{ - "apple", - "banana", - }, - Expected: map[string]interface{}{ - "text":"banana", - }, - }, - { - Cascade: []interface{}{ - "apple", - "banana", - "cherry", - }, - Expected: map[string]interface{}{ - "text":"cherry", - }, - }, - - - { - Cascade: []interface{}{ - map[string]string{ - }, - }, - Expected: map[string]interface{}{}, - }, - - - { - Cascade: []interface{}{ - map[string]string{ - "apple":"one", - }, - }, - Expected: map[string]interface{}{ - "apple":"one", - }, - }, - { - Cascade: []interface{}{ - map[string]string{ - "apple":"one", - "banana":"two", - "cherry":"three", - }, - }, - Expected: map[string]interface{}{ - "apple":"one", - "banana":"two", - "cherry":"three", - }, - }, - - - { - Cascade: []interface{}{ - map[string]string{ - "apple":"one", - "banana":"two", - "cherry":"three", - }, - map[string]string{ - "kiwi":"four", - "watermelon":"five", - }, - }, - Expected: map[string]interface{}{ - "apple":"one", - "banana":"two", - "cherry":"three", - "kiwi":"four", - "watermelon":"five", - }, - }, - { - Cascade: []interface{}{ - map[string]string{ - "apple":"one", - "banana":"two", - "cherry":"three", - "fig":"THIS SHOULD BE REPLACED", - }, - map[string]string{ - "fig":"THIS SHOULD REMAIN", - "kiwi":"four", - "watermelon":"five", - }, - }, - Expected: map[string]interface{}{ - "apple":"one", - "banana":"two", - "cherry":"three", - "fig":"THIS SHOULD REMAIN", - "kiwi":"four", - "watermelon":"five", - }, - }, - } - - -TestLoop: - for testNumber, test := range tests { - - context := newContext(test.Cascade...) - - if expected, actual := len(test.Expected), len(context); expected != actual { - t.Errorf("For test #%d, expected length to be %d but actually was %d.", testNumber, expected, actual) - continue TestLoop - } - - for expectedKey, expectedValue := range test.Expected { - if _, ok := context[expectedKey]; !ok { - t.Errorf("For test #%d, expected key %q to be in resulting context but wasn't.", testNumber, expectedKey) - continue TestLoop - } - - if expected, actual := expectedValue, context[expectedKey]; expected != actual { - t.Errorf("For test #%d, expected value for key %q to be %q in resulting context, but was actually %q.", testNumber, expectedKey, expected, actual) - continue TestLoop - } - } - - } -} diff --git a/copying_router.go b/copying_router.go deleted file mode 100644 index f3593fe..0000000 --- a/copying_router.go +++ /dev/null @@ -1,55 +0,0 @@ -package log - - -// NewCopyingRouter returns an initialized CopyingRouter. -func NewCopyingRouter(subrouter Router) *CopyingRouter { - copies := make([]struct{Message string ; Context map[string]interface{}}, 0, 8) - - router := CopyingRouter { - subrouter:subrouter, - copies:copies, - } - - return &router -} - - -// CopyingRouter is a Router that copies a message (and its context) to memory, and then -// re-routes a message (and its context) to a sub-router. -// -// This router is NOT designed to be used for an indefinite number of routings, and instead -// should only be used for a limited amount of routings, as it stores all the copies in -// memory. -type CopyingRouter struct { - subrouter Router - copies []struct{Message string ; Context map[string]interface{}} -} - - -func (router *CopyingRouter) Route(message string, context map[string]interface{}) error { - if nil == router { - return errNilReceiver - } - - copy := struct{Message string ; Context map[string]interface{}}{ - Message: message, - Context: context, - } - router.copies = append(router.copies, copy) - - return router.subrouter.Route(message, context) -} - - -func (router *CopyingRouter) Copies() <-chan struct{Message string ; Context map[string]interface{}} { - ch := make(chan struct{Message string ; Context map[string]interface{}}) - - go func() { - for _, copy := range router.copies { - ch <- copy - } - close(ch) - }() - - return ch -} diff --git a/copying_router_test.go b/copying_router_test.go deleted file mode 100644 index 6a191fc..0000000 --- a/copying_router_test.go +++ /dev/null @@ -1,141 +0,0 @@ -package log - - -import ( - "testing" - - "fmt" - "math/rand" - "time" -) - - -func TestNewCopyingRouter(t *testing.T) { - - randomness := rand.New(rand.NewSource( time.Now().UTC().UnixNano() )) - - - router := NewCopyingRouter(NewDiscardingRouter()) - if nil == router { - t.Errorf("After trying to create a copying router, expected it to be not nil, but was: %v", router) - } - - - lenInitialCopies := 0 - for range router.Copies() { - lenInitialCopies++ - } - if expected, actual := 0, lenInitialCopies; expected != actual { - t.Errorf("After creating a copying router, expected copies to be length %d, but was actually %d,", expected, actual) - } - - - message := fmt.Sprintf("%x", randomness.Int63n(9999999999)) - - context := make(map[string]interface{}) - limit := randomness.Int63n(30) - for i:=int64(0); i%s<(==\n==)>%s<(==", testNumber, expectedNumber, expectContains, actual) - continue - } - } - } - } -} - - -func TestDefaultWritingRouterWithPrefixRoute(t *testing.T) { - - tests := []struct{ - Message string - Context map[string]interface{} - Prefix map[string]interface{} - ExpectContains []string - }{ - { - Message: "Hello world!", - Context: map[string]interface{}{ - "apple": "one", - "banana": 2, - "cherry": 3.3, - "kiwi": true, - "~error": errors.New("test error"), - }, - Prefix: map[string]interface{}{ - "name": "backendapi", - "number": "123", - }, - ExpectContains: []string{ - `"name"="backendapi" "number"="123" "text"="Hello world!" "when"="`, - ` "ctx"."apple"="one" "ctx"."banana"="2" "ctx"."cherry"="3.300000" "ctx"."kiwi"="true"`, - ` "error"."type"="*errors.errorString" "error"."text"="test error" `, - }, - }, - - - { - Message: "Apple\tBANANA\nCherry", - Context: map[string]interface{}{ - "apple": "one", - "banana": 2, - "cherry": 3.3, - "kiwi": true, - "~error": errors.New("test error"), - "more": map[string]interface{}{ - "ONE": "1", - "TWO": "2", - "THREE": "3", - "FOUR": map[string]interface{}{ - "a": "1st", - "b": "2nd", - "c": []string{ - "th", - "i", - "rd", - }, - }, - }, - }, - Prefix: map[string]interface{}{ - "app": map[string]interface{}{ - "name": "backendapi", - "build": map[string]interface{}{ - "number": 123, - "hash": "4a844b2", - }, - }, - }, - ExpectContains: []string{ - `"app"."build"."hash"="4a844b2" "app"."build"."number"="123" "app"."name"="backendapi" "text"="Apple\tBANANA\nCherry" "when"="`, - ` "ctx"."apple"="one" "ctx"."banana"="2" "ctx"."cherry"="3.300000" "ctx"."kiwi"="true"`, - ` "error"."type"="*errors.errorString" "error"."text"="test error" `, - ` "ctx"."more"."FOUR"."a"="1st" "ctx"."more"."FOUR"."b"="2nd" "ctx"."more"."FOUR"."c"=["th","i","rd"] "ctx"."more"."ONE"="1" "ctx"."more"."THREE"="3" "ctx"."more"."TWO"="2"`, - }, - }, - } - - - for testNumber, test := range tests { - - var buffer bytes.Buffer - - var router Router = NewDefaultWritingRouterWithPrefix(&buffer, test.Prefix) - - ctx := map[string]interface{}{} - for n, v := range test.Context { - ctx[n] = v - } - - if err := router.Route(test.Message, ctx); nil != err { - t.Errorf("For test #%d, did not expect an error, but actually got one: (%T) %v", testNumber, err, err) - continue - } - - { - actual := buffer.String() - for expectedNumber, expectContains := range test.ExpectContains { - - if !strings.Contains(actual, expectContains) { - t.Errorf("For test #%d and expected #%d, expect to contain, actual:\n==)>%s<(==\n==)>%s<(==", testNumber, expectedNumber, expectContains, actual) - continue - } - } - } - } -} diff --git a/discarding_router.go b/discarding_router.go deleted file mode 100644 index b398f15..0000000 --- a/discarding_router.go +++ /dev/null @@ -1,24 +0,0 @@ -package log - - -var ( - singltonDiscardingRouter = DiscardingRouter{} -) - - -// NewDiscardingRouter returns an initialized DiscardingRouter. -func NewDiscardingRouter() *DiscardingRouter { - return &singltonDiscardingRouter -} - - -// DiscardingRouter is a Router that discards any message (and its context) -// it is asked to route. -// -// Conceptually it is similar to /dev/null -type DiscardingRouter struct{} - - -func (router *DiscardingRouter) Route(message string, context map[string]interface{}) error { - return nil -} diff --git a/discarding_router_test.go b/discarding_router_test.go deleted file mode 100644 index 3166453..0000000 --- a/discarding_router_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package log - - -import ( - "testing" - - "fmt" - "math/rand" - "time" -) - - -func TestNewDiscardingRouter(t *testing.T) { - - randomness := rand.New(rand.NewSource( time.Now().UTC().UnixNano() )) - - - router := NewDiscardingRouter() - if nil == router { - t.Errorf("After trying to create a discard router, expected it to be not nil, but was: %v", router) - } - - - message := fmt.Sprintf("%x", randomness.Int63n(9999999999)) - - context := make(map[string]interface{}) - limit := randomness.Int63n(30) - for i:=int64(0); i