Micro-services Using go-kit: Logging Features

Every application needs logging. Logging is very important, so you can consider it as a first class citizen. Therefore, application cannot be deployed to production without logging implementation.

Sharing is caring!

Why Logging is Important

Image taken from https://www.claromentis.com/features/audit-logs/

During my professional experiences, I got reports regarding why the application behaved unexpectedly. Facing this kind of situation, my first step was always looking the log files. And trace line by line to determine the root cause. Believed it or not, I was grateful because the application has a logging feature. Otherwise, I will be lost in the ocean of codes, debugging thousands line of codes to find the problem.

Go-kit Logging Feature

The very cool feature about go-kit is about having a middleware functionality. Basically, middleware is a decorator function that takes an endpoint as an input and return an endpoint. By using this functionality, we can add logging feature into our application.

Use Case

I am still in love with my lorem ipsum application. In fact, I am a little bit lazy to make another example :). Therefore, I will use it as an example (again). Also, it is a good example to show go-kit composability. I will show how easy to add logging feature into existing application. Such a plug and play feature.

Whenever you want to know about lorem application, please visit my previous article here. For this demo, I duplicate the lorem example and rename the package into lorem-logging.

Step 1: Modify service.go

Create new type inside service.go and give it a name ServiceMiddleware. This type is a function that takes a Service interface as an input and return a Service. This will be useful to make a function chain Service call in the main function.

Step 2: Implement Logging Features

Create a file and give it a name logging.go. Inside this file, we will create a type and named it loggingMiddleware. This type has two properties, which are Service and Logger.

Next one is to create a function to return ServiceMiddleware function (see Step 1). This function take one input parameter, which is Logger variable.

And the last step for logging functionality is implementing Service functions. Inside each function implementation, we will log function name, input values, output value and how long the function invocation.

Step 3: Modify main.go

After finishing log functionality, next step is wiring it into main.go.

See line 15 to wire log functionality. As simple as that.

Step 4: Run

Now it is time to run our main.go file. Go to shell command and run it just like the previous one. If everything goes well, you will see similar output:

Conclusion

What I bring into this article is about basic logging functionality, by using go-kit framework. However, to make it production ready, it is still not enough. Because it will huge number of logs data that will cause problem to analyze manually. Therefore, in my opinion, the log analyzer is still needed. The ELK stack would be a great tool to do that.

I want to write another article about integrating go-kit logging features with ELK stack. But it will take some time to make a simulation between those two. I am still looking for the Vagrant option to do that. Hopefully, I can bring this topic in the near future.

Meanwhile, this is my post for today. As usual, you can see the sample on my github, under folder lorem-logging. Have a blessed day!

PS: I would like to see another options about this ELK thing. Whenever you guys have any idea, just comment or email me. Would be very much appreciated.

Author: ru rocker

I have been a professional software developer since 2004. Java, Python, NodeJS, and Go-lang are my favorite programming languages. I also have an interest in DevOps. I hold professional certifications: SCJP, SCWCD, PSM 1, AWS Solution Architect Associate, and AWS Solution Architect Professional.

One thought on “Micro-services Using go-kit: Logging Features”

Leave a Reply

Your email address will not be published. Required fields are marked *