diff options
author | Carlos Tadeu Panato Junior <ctadeu@gmail.com> | 2017-04-21 11:16:35 +0200 |
---|---|---|
committer | George Goldberg <george@gberg.me> | 2017-04-21 10:16:35 +0100 |
commit | b57b5abce814cfcdf3889d4e267492e809245593 (patch) | |
tree | 6cd8d56eb272c0ba7cc8b7698c8112fc14b22271 /api4 | |
parent | 08822def53059550e168bef93fbdc0bd7d53e741 (diff) | |
download | chat-b57b5abce814cfcdf3889d4e267492e809245593.tar.gz chat-b57b5abce814cfcdf3889d4e267492e809245593.tar.bz2 chat-b57b5abce814cfcdf3889d4e267492e809245593.zip |
implement POST /logs for apiV4 (#6143)
Diffstat (limited to 'api4')
-rw-r--r-- | api4/system.go | 29 | ||||
-rw-r--r-- | api4/system_test.go | 20 |
2 files changed, 49 insertions, 0 deletions
diff --git a/api4/system.go b/api4/system.go index dfc702c8c..55be559bf 100644 --- a/api4/system.go +++ b/api4/system.go @@ -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) { @@ -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") diff --git a/api4/system_test.go b/api4/system_test.go index b2b5d3f43..ba30dd1eb 100644 --- a/api4/system_test.go +++ b/api4/system_test.go @@ -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") + } + +} |