Send data via GoLang to your Logstash instance provided by Logit.io

GoLang

Ship GoLang application logs to logstash

Configure your GoLang application to send logs to Logstash & Elasticsearch.

Step 1 - Create Go Logging Application

Paste the following into a Go file to create a generic logging application.

This is not a straight plug and play piece of code. You will need to add this to your own application and tell it when to log Info, Warn, Error & Fatal messages in order for this to work. This is just an example configuration to get you started.

package main

import (
        "github.com/bshuster-repo/logrus-logstash-hook"
        "github.com/sirupsen/logrus"
        "net"
)

func main() {
        log := logrus.New()
        conn, err := net.Dial("tcp", ["your-logstash-host:your-port"])
        if err != nil {
                log.Fatal(err)
        }
        hook := logrustash.New(conn, logrustash.DefaultFormatter(logrus.Fields{"type": "myappName"}))
        
        log.Hooks.Add(hook)
        ctx := log.WithFields(logrus.Fields{
                "method": "main",
        })
        ctx.Info("Info Message")
        ctx.Error("Error Message")
        ctx.Warn("Warn Message")
        ctx.Fatal("Fatal Message")
}

In the code above you can edit the message displayed by editing the text between the brackets of the ctx.Info, ctx.Error etc.

Step 2 - Go Package Requirements

Now you need to run the following commands to install the logging package requirements

go get github.com/bshuster-repo/logrus-logstash-hook
go get github.com/sirupsen/logrus

Step 3 - Run the application

If you called the example configuration file logs.go you would run the following command and then be able to see the Info, Warn, Error & Fatal message in Kibana.

go run logs.go

This is how it will look like

{
  "@timestamp" => "2020-04-07T22:22:43.000Z",
    "@version" => "1",
       "level" => "info",
     "message" => "Info Message",
      "method" => "main",
        "host" => "172.17.0.1",
        "port" => 15678,
        "type" => "myappName"
}
expand view

Expand View

compact view

Compact View

Return to Search
Sign Up