Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Elevate Your Golang Logging with the Strength of Logrus


Sign upSign InSign upSign InMehranFollowITNEXT--ListenShareHello there, code lovers! If you’re anything like me, then you know that logging is a critical part of the software development process. That unsung hero gives us insight into how our application runs, acting as our eyes and ears into the intricate workings of our code. Today, I’ll introduce you to a fantastic logging library for Go — Logrus.Now, let’s move to the star of today’s post — Logrus. Logrus is a structured logger for Golang, completely API-compatible with the standard library logger. It’s packed with nifty features like:So, without further ado, let’s dive into the fun part — examples! We’ll look at thirteen different uses of Logrus: basic logging, formatting logs, implementing hooks, etc.First things first, we need to install Logrus. Run the following command:Now, let’s log a simple error message:When you run this program, you’ll see “ERRO[0000] Something went wrong!” printed in your console. ‘ERRO’ is the error level, and ‘[0000]’ is the timestamp.One of the beautiful things about Logrus is its ability to format logs. Let’s say you want your logs in JSON format instead of the default text format:Here, we’re setting the log format to JSON and logging an informational message. When you run this, you’ll get a JSON-formatted log entry like this: {"level":"info","msg":"This is an informational message.","time":"2023-07-23T14:56:00Z"}.Logrus hooks can be extremely useful for sending log entries to external services that Logrus don’t already support. Here’s an example of a hook that logs all warnings and above to a hypothetical bug tracker:In this example, we first define a struct BugTrackerHook implementing Levels() method to indicate which levels this hook should be fired for and Fire() method to act with a log entry. Then we add this hook to Logrus using. AddHook() The method and create a warning log. If you have a bug tracker service, you can add code to send the log entry to it within Fire() method.Logrus provides several log levels. You can set the log level to control the severity of the logs that will be written.If you have a severe error that needs to terminate the program, you can use Fatal which logs the error and calls os.Exit(1).In case you need to log an error message and panic, use Panic.If you want even more detail than Debug, Logrus provides a Trace level.Logrus can also write its output to any. io.Writer.You can set your preferred timezone for the log timestamps.You can maintain a consistent order of log entry fields.Sensitive data can be redacted from logs.You can create custom formatters to control how log entries are formatted.Logrus is a fantastic library that adds robust, flexible logging to your Golang applications. Whether you’re working on a small side project or an enterprise-level application, Logrus can fit in seamlessly and provide the level of logging detail you need.From a simple print to console to sophisticated hooks triggering actions on specific log levels, Logrus offers unparalleled flexibility in Go logging.So, try Logrus, and let me know how it goes. Until next time, happy coding, folks!----ITNEXTTech Team Lead | Cloud, Video & Microservices Expert | Insights on streaming innovations & programming. #ContinuousLearningMehraninITNEXT--1Juntao QiuinITNEXT--9Daniel GlejznerinITNEXT--11MehraninITNEXT--2Rico FritzscheinLevel Up Coding--16Germain SIGETY--Juliano Silva--Anurag Verma--1Java M--Dom Jocubeit--HelpStatusWritersBlogCareersPrivacyTermsAboutText to speechTeams

This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Elevate Your Golang Logging with the Strength of Logrus