Skip to content

Commit

Permalink
add regex option for msg flag
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephGJ committed Jul 31, 2017
1 parent 575375d commit 362b73f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 55 deletions.
19 changes: 15 additions & 4 deletions api/rpc/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logs
import (
"encoding/json"
"errors"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -87,9 +88,13 @@ func (s *Server) Get(ctx context.Context, in *GetRequest) (*GetReply, error) {
masterQuery.Filter(elastic.NewPrefixQuery("node_id", in.Node))
}
if in.Message != "" {
queryString := elastic.NewSimpleQueryStringQuery(in.Message)
queryString.Field("msg")
masterQuery.Filter(queryString)
if in.Regexp {
masterQuery.Filter(elastic.NewRegexpQuery("msg", in.Message))
} else {
queryString := elastic.NewSimpleQueryStringQuery(in.Message)
queryString.Field("msg")
masterQuery.Filter(queryString)
}
}
if !in.IncludeAmpLogs {
masterQuery.MustNot(elastic.NewExistsQuery(dockerToEsLabel(constants.LabelKeyRole)))
Expand Down Expand Up @@ -201,7 +206,13 @@ func match(entry *LogEntry, in *GetRequest) bool {
match = match && strings.HasPrefix(strings.ToLower(entry.NodeId), strings.ToLower(in.Node))
}
if in.Message != "" {
match = match && strings.Contains(strings.ToLower(entry.Msg), strings.ToLower(in.Message))
if in.Regexp {
matched, _ := regexp.MatchString(in.Message, entry.Msg)
match = match && matched
} else {
match = match && strings.Contains(strings.ToLower(entry.Msg), strings.ToLower(in.Message))
}

}
if !in.IncludeAmpLogs {
_, ampLogs := entry.Labels[constants.LabelKeyRole]
Expand Down
98 changes: 53 additions & 45 deletions api/rpc/logs/logs.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions api/rpc/logs/logs.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ message GetRequest {
string container = 1;
string message = 2;
string node = 3;
sint64 size = 4;
string service = 5;
string stack = 6;
string task = 7;
bool includeAmpLogs = 8;
int32 since = 9; // In days
bool regexp = 4;
sint64 size = 5;
string service = 6;
string stack = 7;
string task = 8;
bool includeAmpLogs = 9;
int32 since = 10; // In days
}

message GetReply {
Expand Down
4 changes: 4 additions & 0 deletions api/rpc/logs/logs.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
"node": {
"type": "string"
},
"regexp": {
"type": "boolean",
"format": "boolean"
},
"size": {
"type": "string",
"format": "int64"
Expand Down
3 changes: 3 additions & 0 deletions cli/command/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type LogsOptions struct {
Raw bool
Number int64
Msg string
Regexp bool
Container string
Stack string
Node string
Expand All @@ -34,6 +35,7 @@ func AddLogFlags(flags *pflag.FlagSet, opts *LogsOptions) {
flags.BoolVarP(&opts.Meta, "meta", "m", false, "Display entry metadata")
flags.Int64VarP(&opts.Number, "number", "n", 1000, "Number of results")
flags.StringVar(&opts.Msg, "msg", "", "Filter the message content by the given pattern")
flags.BoolVar(&opts.Regexp, "regexp", false, "Treat '--msg' option as a regular expression")
flags.StringVar(&opts.Container, "container", "", "Filter by the given Container")
flags.StringVar(&opts.Node, "node", "", "Filter by the given node")
flags.BoolVarP(&opts.Raw, "raw", "r", false, "Display raw logs (no prefix)")
Expand Down Expand Up @@ -62,6 +64,7 @@ func GetLogs(c cli.Interface, args []string, opts LogsOptions) error {
request.Service = args[0]
}
request.Message = opts.Msg
request.Regexp = opts.Regexp
request.Container = opts.Container
request.Stack = opts.Stack
request.Node = opts.Node
Expand Down

0 comments on commit 362b73f

Please sign in to comment.