Skip to content

Commit

Permalink
Expose loglevels with + in kv pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
desa committed Oct 6, 2017
1 parent e083476 commit 4007a40
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/kapacitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2298,7 +2298,8 @@ func logsUsage() {
Watch arbitrary kapacitor logs.
$ kapacitor logs service=http lvl=info
$ kapacitor logs service=http lvl=error
$ kapacitor logs service=http lvl=info+
`
fmt.Fprintln(os.Stderr, u)
}
Expand Down
9 changes: 8 additions & 1 deletion services/diagnostic/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"strings"

"github.com/influxdata/kapacitor/services/httpd"
"github.com/influxdata/kapacitor/uuid"
Expand Down Expand Up @@ -85,19 +86,25 @@ func (s *SessionService) NewLogger() *sessionsLogger {
func (s *SessionService) handleSessions(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
tags := []tag{}
var level Level

for k, v := range params {
if len(v) != 1 {
httpd.HttpError(w, "query params cannot contain duplicate pairs", true, http.StatusBadRequest)
return
}

if k == "lvl" && strings.HasSuffix(v[0], "+") {
level = logLevelFromName(strings.TrimSuffix(v[0], "+"))
continue
}

tags = append(tags, tag{key: k, value: v[0]})
}

contentType := r.Header.Get("Content-Type")

session := s.SessionsStore.Create(w, contentType, tags)
session := s.SessionsStore.Create(w, contentType, level, tags)
defer s.SessionsStore.Delete(session)

header := w.Header()
Expand Down
2 changes: 1 addition & 1 deletion services/diagnostic/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func TestSessionsLoggerWithoutContext(t *testing.T) {
service := diagnostic.NewSessionService()
// TODO: we need ths?
_ = service.NewLogger()
session := service.SessionsStore.Create(&httpResponseWriter{buf: buf}, "application/json", nil)
session := service.SessionsStore.Create(&httpResponseWriter{buf: buf}, "application/json", diagnostic.DebugLevel, nil)
defer service.SessionsStore.Delete(session)

tests := []struct {
Expand Down
15 changes: 9 additions & 6 deletions services/diagnostic/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type SessionsStore interface {
Create(w http.ResponseWriter, contentType string, tags []tag) *Session
Create(w http.ResponseWriter, contentType string, level Level, tags []tag) *Session
Delete(s *Session) error
Each(func(*Session))
SetDiagnostic(Diagnostic)
Expand All @@ -31,7 +31,7 @@ func (kv *sessionsStore) SetDiagnostic(d Diagnostic) {

}

func (kv *sessionsStore) Create(w http.ResponseWriter, contentType string, tags []tag) *Session {
func (kv *sessionsStore) Create(w http.ResponseWriter, contentType string, logLevel Level, tags []tag) *Session {
kv.mu.Lock()

wf, ok := w.(WriteFlusher)
Expand All @@ -43,6 +43,7 @@ func (kv *sessionsStore) Create(w http.ResponseWriter, contentType string, tags
id: uuid.New(),
tags: tags,
w: wf,
level: logLevel,
contentType: contentType,
}

Expand Down Expand Up @@ -92,6 +93,8 @@ type Session struct {
mu sync.Mutex
id uuid.UUID

level Level

tags []tag

buf bytes.Buffer
Expand All @@ -100,25 +103,25 @@ type Session struct {
}

func (s *Session) Error(msg string, context, fields []Field) {
if match(s.tags, msg, "error", context, fields) {
if s.level <= ErrorLevel && match(s.tags, msg, "error", context, fields) {
s.Log(time.Now(), "error", msg, context, fields)
}
}

func (s *Session) Warn(msg string, context, fields []Field) {
if match(s.tags, msg, "warn", context, fields) {
if s.level <= WarnLevel && match(s.tags, msg, "warn", context, fields) {
s.Log(time.Now(), "warn", msg, context, fields)
}
}

func (s *Session) Debug(msg string, context, fields []Field) {
if match(s.tags, msg, "debug", context, fields) {
if s.level <= DebugLevel && match(s.tags, msg, "debug", context, fields) {
s.Log(time.Now(), "debug", msg, context, fields)
}
}

func (s *Session) Info(msg string, context, fields []Field) {
if match(s.tags, msg, "info", context, fields) {
if s.level <= InfoLevel && match(s.tags, msg, "info", context, fields) {
s.Log(time.Now(), "info", msg, context, fields)
}
}
Expand Down

0 comments on commit 4007a40

Please sign in to comment.