Skip to content

Commit

Permalink
Added default logger for when logging is enabled but logger is not sp…
Browse files Browse the repository at this point in the history
…ecified. fixes blugelabs#1
  • Loading branch information
voldyman committed Jan 6, 2021
1 parent 96ffb95 commit b0f12b6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions query_string_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package querystr
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
Expand All @@ -43,27 +44,35 @@ type QueryStringOptions struct {
logger *log.Logger
}

var defaultLogger = log.New(os.Stderr, "", log.LstdFlags)

// DefaultOptions creates default options with debug features disabled
func DefaultOptions() QueryStringOptions {
return QueryStringOptions{
dateFormat: time.RFC3339,
logger: defaultLogger,
}
}

// WithDebugParser will make parser print debug messages to logger in options or stderr by default
func (o QueryStringOptions) WithDebugParser(debug bool) QueryStringOptions {
o.debugParser = debug
return o
}

// WithDebugLexer will make lexer print debug messages to logger in options or stderr by default
func (o QueryStringOptions) WithDebugLexer(debug bool) QueryStringOptions {
o.debugLexer = debug
return o
}

// WithDateFormat changes the default date format (RFC 3339) to the specified one
func (o QueryStringOptions) WithDateFormat(dateFormat string) QueryStringOptions {
o.dateFormat = dateFormat
return o
}

// WithLogger changes the log destination to the specified one, default is os.Stderr
func (o QueryStringOptions) WithLogger(logger *log.Logger) QueryStringOptions {
o.logger = logger
return o
Expand Down
15 changes: 15 additions & 0 deletions query_string_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package querystr

import (
"io/ioutil"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -420,6 +421,20 @@ func TestQuerySyntaxParserInvalid(t *testing.T) {
}
}

func TestParserDebugWithNoLogger(t *testing.T) {
// don't print logs in tests
defaultLogger.SetOutput(ioutil.Discard)

r, err := ParseQueryString("text", DefaultOptions().WithDebugParser(true))
if err != nil {
t.Errorf("unexpected error: %+v", err)
}
expectedQuery := bluge.NewBooleanQuery().AddShould(bluge.NewMatchQuery("text"))
if !reflect.DeepEqual(r, expectedQuery) {
t.Error("unexpected query returned")
}
}

var extTokenTypes []int
var extTokens []yySymType

Expand Down

0 comments on commit b0f12b6

Please sign in to comment.