Last updated at Fri, 03 Nov 2017 21:12:22 GMT

Would you like to send logs from your Go program code into your Logentries account?Thanks to the help of Gal Ben-Haim’s, bsphere Golang library for log entries, Go coders can be sending their logs to Logentries in no time at all. Benefits of using this Go Lang library and implementing with your Logentries account include:

  • Remote viewing and analysis of your Go program log events
  • All your logs are sent to one location, and viewed through an easy to use web-based UI
  • You can use Logentries to tag and alert you to specific log events helping you to review your code during runtime.

1. Create An Account

First you’ll need a Logentries Account, which you can get for free at logentries.com. Once you have a Logentries account, you’ll need a destination log in order to store your entries sent from within your Go program. Log into Logentries, and create a new log by clicking the Create Log button. If you already have a Host set up you can add the log to the existing Host by click on the Host in the Web UI and then “Add new log”  as shown below:

Now, click Manual Configuration button from the list of options.


Give your log a name of your choice, and then select “Token TCP”:

Then, click the Register New Log button. A token will be displayed in green. Please record it as we will use it later to configure the library.

2. Download le_go library from Github

I assume you have your $GOPATH variable set. In a command line terminal, navigate to your $GOPATH’s Src folder and enter:

go get github.com/bsphere/le_go

This will download the library required from github. It puts the required GoLang library in the PKG foler under the following path

go/pkg/linux_amd_g4/github.com/bsphere/le_go.a

The /linux_amd_g4/ section of this path may differ depending upon your operating system. Navigate to your $GOPATH workspace directory where you write your programs, mine is called “packages”

cd $GOPATH/packages

In packages, I created a very simple Go program called mygo.go and entered the code below.

3. Set-up your code

Include the following code in your program:

package main
import ( 
"github.com/bsphere/le_go" 
)
func main() { 
le, err := le_go.Connect("########-####-####-####-##########") // replace #s with token 
if err != nil { 
panic(err) 
} 
defer le.Close() 
le.Println("Logentries test log message") 

le.Println("{\"events\": { \"count\":10} }") 
}

….. Replace the ########-####-####-####-########## in your Go program code with the log token we copied earlier.  It connects this logger with that specific log on Logentries.  If you haven’t taken note of the token when creating your log, you can retrieve it by logging in to Logentries. Once logged in, select the host containing the log for which you want to retrieve the token (e.g. from the side bar on the left hand side of the page). All the token-based logs of this host are then displayed with a Token icon on the right hand side. Click this icon to display the token. Be sure you are in the directory in which your Go program is located.  Mine is $GOPATH/packages Run your simple program

go run mygo.go

Your log events called inside your Go program should appear in your specified log on your Logentries account. More information can be found on the golang.org, How to Write Go Code page or you can view the Gal Ben-Haim’s GoLang github page.


4. Check out your Logs

By running your Go program with these additions, the specific log events are produced and sent to Logentries.  You can sign into your Logentries account and select the logs that you want to consult.  Hit the refresh button at the top right corner of the Logentries UI and you’ll see your latest log events. Keep in mind, it can take a few seconds before log events appear.