Get a DemoStart Free TrialSign In

Golang Logging

Ship GoLang application logs to logstash

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

Follow this step by step guide to get 'logs' from your system to Logit.io:

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"
        "crypto/tls"
)
func main() {
        log := logrus.New()
        conn, err := tls.Dial("tcp", "your-logstash-host:your-ssl-port", &tls.Config{ RootCAs: nil }) 
        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

You should see output similar to the following

{
  "@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"
}

Step 4 - Check Logit.io for your logs

Data should now have been sent to your Stack.

View my data

If you don't see logs take a look at How to diagnose no data in Stack below for how to diagnose common issues.

Step 5 - how to diagnose no data in Stack

If you don't see data appearing in your Stack after following the steps, visit the Help Centre guide for steps to diagnose no data appearing in your Stack or Chat to support now.

Step 6 - Golang Logging Overview

Golang (also known as Go) is an open-source programming language created in 2009 by Google. It forms the backbone of many platforms, including the core tools involved in containerisation (Kubernetes & Docker). While many application logs include a variety of statuses, Golang only records log messages when an error is generated. Golang error logs include a variety of valuable data for troubleshooting, including timestamps, contextual data, and what level of error has been encountered.

It may not be sustainable for large organisations to track these log messages reliably if your Go logs are deployed across numerous hosts. These will likely require tailing in a centralised logging environment.

Using a specialised Golang logger makes root cause analysis, application monitoring, and reporting at scale far easier than relying on distributed logs located across all your Go-based applications.

The Logit.io platform includes everything you need for Golang logging & is built to enable you to drive data-driven insights for error resolution fast. Our platform is fully integrated with a number of third-party alerting tools so you can be notified in real-time when your program encounters an exception.

If you need any more help with migrating your Go log files to Logstash the Logit.io team is here to help. Just get in contact with us via live chat & we'll be happy to assist.

Return to Search
Sign Up

© 2024 Logit.io Ltd, All rights reserved.