How to log info to mattermost logfile while developing plugin?

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.

Here are my LogSettings:

"LogSettings": {
        "EnableConsole": true,
        "ConsoleLevel": "DEBUG",
        "ConsoleJson": true,
        "EnableFile": true,
        "FileLevel": "DEBUG",
        "FileJson": true,
        "FileLocation": "",
        "EnableWebhookDebugging": false,
        "EnableDiagnostics": true
    },

My intention of logging is to see how the ServeHTTP function catches the api URLs and hence extend it by doing something like:

switch path {
	case "/api/v4/new-endpoint":
		{
			fmt.Fprintf(w, "Handle new endpoint!")
		}
	default:
		fmt.Fprintf(w, "default")
	}

If there are other proper examples / documentation on how to extend the API, do please point me to them.

Hi @haris, thanks for reaching out and apologies for the delay.

Does this ticket describe what you are looking for: https://mattermost.atlassian.net/browse/MM-10566?

If yes, I’ll ask more info from the dev or community member who helped implement it, and if not, I’ll ask our devs for more general info.

Hey, no need to apologize. I can see how busy you are. :slight_smile:

That ticket you’ve pointed to discusses my question, yes, but it doesn’t seem to have an answer. The github issue that it points to ([Help Wanted] [MM-10566] Add logging functions to the plugin API · Issue #8767 · mattermost/mattermost · GitHub) seems to be up for grabs.

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.

Thanks again!

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:

{"level":"debug","ts":1531494669.83655,"caller":"app/plugin_api.go:254","msg":"received http request","plugin_id":"my-plugin","user_agent":"HTTPie/0.9.9"}
{"level":"error","ts":1531494669.8368616,"caller":"app/plugin_api.go:260","msg":"missing referer","plugin_id":""my-plugin"}

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.

Once you’re able to move to Mattermost 5.2, I’d encourage you to checkout https://developers.mattermost.com/extend/plugins/ for lots more documentation and examples, as well as check out our sample plugin and demo plugin.

Hello again @jesse!

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. :smiley:

1 Like