What’s the proper way to log info to the mattermost log while developing a plugin? MM version 5.1.
This is my function:
package main
import (
"fmt"
"log"
"net/http"
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/plugin"
"github.com/mattermost/mattermost-server/plugin/rpcplugin"
)
type Plugin struct {
api plugin.API
}
func (p *Plugin) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var path = r.URL.Path
fmt.Fprintf(w, path)
mlog.Error("Error")
mlog.Debug("Debug")
// log.Fatal("Fatal error")
}
func main() {
rpcplugin.Main(&Plugin{})
}
If I directly visit http://localhost:8065/plugins/com.mattermost.server-hello-world/ on the browser, then I can see the output from fmt.Fprintf(w, path). However, nothing appears on the browser or the logs for the mlog lines. The log.Fatal crashes the app.
And the hint A good approach might be to mirror the [mlog.Debug, mlog.Error, etc on the ticket is something that I tried to achieve (As you can see in the code example in my question) but to no avail.
Once again, what I am trying to achieve with logging is to understand how the URLs are handled within the plugin. So if there is documentation regarding how to extend the API (with examples), then pointing me to them would be helpful as well.
Hi @haris! In Mattermost 5.2, we revamped the beta plugin architecture and officially exposed logging via the API:
func (p *MyPlugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
p.API.LogDebug("received http request", "user_agent", r.UserAgent())
if r.Referer() == "" {
p.API.LogError("missing referer")
}
}
As well, any STDERR output from your plugin (e.g. logging via the standard log package) would also go to the Mattermost server logs. This above code snippet ends up in the logs as something like:
All that being said, I recognize that a) Mattermost 5.2 isn’t out for another week, and b) you might not be ready to upgrade past Mattermost 5.1 immediately. Unfortunately, there’s not a good story around logging from plugins in v5.1. My suggestion might be to leverage the built-in log package and call SetOutput to redirect your debugging logs to a file of your own for analysis.
Seems like life will be a lot easier going forward with 5.2. Although we have 5.1 in production, we currently use it as a backup for slack. Our intention is to switch MM as the primary platform soon. So let me see if we can plan on switching to 5.2 within a week and then spend time with the new plugins and stuff.
Cool, thanks a ton again! Will hopefully soon be back with my war stories.