How do I log in JSON format using logrus in Go?

In Go, you can use the Logrus package to log structured logs in JSON format. Here's a quick example of how to achieve this.

Go, Logrus, JSON logging, structured logging
This example demonstrates how to configure Logrus for JSON logging in a Go application.
package main import ( "github.com/sirupsen/logrus" ) func main() { // Set the log format to JSON logrus.SetFormatter(&logrus.JSONFormatter{}) // Log an example message logrus.WithFields(logrus.Fields{ "event": "user_login", "username": "johndoe", }).Info("User logged in") }

Go Logrus JSON logging structured logging