Skip to content

Commit

Permalink
implement POST /logs for apiV4
Browse files Browse the repository at this point in the history
  • Loading branch information
cpanato committed Apr 18, 2017
1 parent 92d8fa4 commit b26f4c9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
29 changes: 29 additions & 0 deletions api4/system.go
Expand Up @@ -29,6 +29,7 @@ func InitSystem() {
BaseRoutes.ApiRoot.Handle("/caches/invalidate", ApiSessionRequired(invalidateCaches)).Methods("POST")

BaseRoutes.ApiRoot.Handle("/logs", ApiSessionRequired(getLogs)).Methods("GET")
BaseRoutes.ApiRoot.Handle("/logs", ApiSessionRequired(postLog)).Methods("POST")
}

func getSystemPing(c *Context, w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -160,6 +161,34 @@ func getLogs(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(model.ArrayToJson(lines)))
}

func postLog(c *Context, w http.ResponseWriter, r *http.Request) {
if !*utils.Cfg.ServiceSettings.EnableDeveloper && !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
return
}

m := model.MapFromJson(r.Body)
lvl := m["level"]
msg := m["message"]

if len(msg) > 400 {
msg = msg[0:399]
}

if lvl == "ERROR" {
err := &model.AppError{}
err.Message = msg
err.Id = msg
err.Where = "client"
c.LogError(err)
} else {
l4g.Debug(msg)
}

m["message"] = msg
w.Write([]byte(model.MapToJson(m)))
}

func getClientConfig(c *Context, w http.ResponseWriter, r *http.Request) {
format := r.URL.Query().Get("format")

Expand Down
20 changes: 20 additions & 0 deletions api4/system_test.go
Expand Up @@ -317,3 +317,23 @@ func TestGetLogs(t *testing.T) {
_, resp = Client.GetLogs(0, 10)
CheckUnauthorizedStatus(t, resp)
}

func TestPostLog(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Client := th.Client

message := make(map[string]string)
message["level"] = "ERROR"
message["message"] = "this is a test"

_, resp := Client.PostLog(message)
CheckForbiddenStatus(t, resp)

logMessage, resp := th.SystemAdminClient.PostLog(message)
CheckNoError(t, resp)
if len(logMessage) == 0 {
t.Fatal("should return the log message")
}

}
12 changes: 12 additions & 0 deletions model/client4.go
Expand Up @@ -2207,6 +2207,18 @@ func (c *Client4) GetLogs(page, perPage int) ([]string, *Response) {
}
}

// PostLog is a convenience Web Service call so clients can log messages into
// the server-side logs. For example we typically log javascript error messages
// into the server-side. It returns the log message if the logging was successful.
func (c *Client4) PostLog(message map[string]string) (map[string]string, *Response) {
if r, err := c.DoApiPost("/logs", MapToJson(message)); err != nil {
return nil, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return MapFromJson(r.Body), BuildResponse(r)
}
}

// Commands Section

// CreateCommand will create a new command if the user have the right permissions.
Expand Down

0 comments on commit b26f4c9

Please sign in to comment.