<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sumudu Liyanage</title>
    <description>The latest articles on DEV Community by Sumudu Liyanage (@sumuduliyanage).</description>
    <link>https://dev.to/sumuduliyanage</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1453947%2F64744ab4-0edd-445a-83a6-ce4fdd05f415.jpg</url>
      <title>DEV Community: Sumudu Liyanage</title>
      <link>https://dev.to/sumuduliyanage</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sumuduliyanage"/>
    <language>en</language>
    <item>
      <title>Insights Into Go Logging</title>
      <dc:creator>Sumudu Liyanage</dc:creator>
      <pubDate>Sun, 28 Apr 2024 09:23:10 +0000</pubDate>
      <link>https://dev.to/sumuduliyanage/insights-into-go-logging-2egh</link>
      <guid>https://dev.to/sumuduliyanage/insights-into-go-logging-2egh</guid>
      <description>&lt;p&gt;Logging is indeed very important when developing software applications, because logs help us to debug, monitor, and analyze our software applications. There are several go packages available for logging. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;log&lt;/li&gt;
&lt;li&gt;logrus&lt;/li&gt;
&lt;li&gt;zap&lt;/li&gt;
&lt;li&gt;zerolog&lt;/li&gt;
&lt;li&gt;glog&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. log
&lt;/h2&gt;

&lt;p&gt;This is the logging package that comes with the standard  Go Library. This has very basic logging capabilities. So, this package is good for logging in small applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Logging
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;log.Print("Hello, log!")
log.Println("Hello with newline")
log.Printf("Hello, %s!", "formatted")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output:
2024/04/28 11:28:37 Hello, log!
2024/04/28 11:28:37 Hello with newline
2024/04/28 11:28:37 Hello, formatted!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Log Prefix and Flags
&lt;/h3&gt;

&lt;p&gt;The log package allows you to set a prefix to each log entry and control the logging format via flags.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;log.SetPrefix("prefix: ")
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
log.Println("This is a log with prefix-1")
log.Println("This is a log with prefix-2")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output:
prefix: 2024/04/28 11:33:17 main.go:33: This is a log with prefix-1
prefix: 2024/04/28 11:33:17 main.go:34: This is a log with prefix-2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Levels of Severity
&lt;/h3&gt;

&lt;p&gt;The log package doesn’t support log levels directly, but you can simulate them using different prefixes or by defining custom logging functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func info(args ...interface{}) {
   log.SetPrefix("INFO: ")
   log.Print(args...)
}


func error(args ...interface{}) {
   log.SetPrefix("ERROR: ")
   log.Print(args...)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Fatal and Panic Logging
&lt;/h3&gt;

&lt;p&gt;log.Fatal* functions will log the message and then call os.Exit(1).&lt;br&gt;
log.Panic* functions will log the message and then panic&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;log.Fatal("This is a fatal error")
log.Panic("This is a panic")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. logrus
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;logrus&lt;/code&gt; is a powerful logging package which extends the capabilities of &lt;code&gt;log&lt;/code&gt; package. &lt;code&gt;logrus&lt;/code&gt; package can be used for logging in more complex applications. Some benefits of &lt;code&gt;logrus&lt;/code&gt; are as below.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Structured Logging: Logrus allows for logging in a structured format, particularly in JSON format.&lt;/li&gt;
&lt;li&gt;Log Levels: Logrus supports multiple logging levels (debug, info, warn, error, fatal, and panic), making it easy to output messages according to their severity.&lt;/li&gt;
&lt;li&gt;Hooks: You can add hooks to send logs to external systems or to trigger actions based on certain log messages.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Basic Logging
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;logrus.Info("This is an info message")
logrus.Warn("This is a warning message")
logrus.Error("This is an error message")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output:
INFO[0000] This is an info message                      
WARN[0000] This is a warning message                    
ERRO[0000] This is an error message
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Structured Logging
&lt;/h3&gt;

&lt;p&gt;Use structured logging to include additional fields in the log entries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;logrus.WithFields(logrus.Fields{
       "username": "Sumudu Liyanage",
       "id":       123,
   }).Info("User logged in")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output:
INFO[0000] User logged in id=123 username="Sumudu Liyanage"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Customizing Output
&lt;/h3&gt;

&lt;p&gt;Logrus allows you to customize the output format. For instance, to log in JSON format.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;logrus.SetFormatter(&amp;amp;logrus.JSONFormatter{})
   logrus.WithFields(logrus.Fields{
       "username": "Sumudu Liyanage",
       "id":       123,
   }).Info("User logged in")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output:
{"id":123,"level":"info","msg":"User logged in","time":"2024-04-28T11:56:35+05:30","username":"Sumudu Liyanage"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Hooks
&lt;/h3&gt;

&lt;p&gt;You can add hooks to integrate with other systems. For example, adding a hook to send error-level logs to a monitoring service.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. zap
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;zap&lt;/code&gt; is a high performance logging library. It's designed for applications that require fast, structured logging with minimal overhead. Choosing &lt;code&gt;zap&lt;/code&gt; over &lt;code&gt;logrus&lt;/code&gt; will be because of the speed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;logger, err := zap.NewProduction()
   if err != nil {
       panic(err)
   }


   logger.Info("This is an info message", zap.String("key", "value"), zap.Int("number", 1))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output:
{"level":"info","ts":1714287036.2461052,"caller":"hello-world/main.go:14","msg":"This is an info message","key":"value","number":1}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. zerolog
&lt;/h2&gt;

&lt;p&gt;zerolog is another high-performance logging library for Go, specifically designed for zero-allocation JSON logging. zerolog minimizes memory allocations by writing JSON log events directly to output (like io.Writer), avoiding the overhead of temporary objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Set global log level
   zerolog.SetGlobalLevel(zerolog.InfoLevel)


   // Configure zerolog to write to stdout with human-friendly formatting
   log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})


   // Logging with context
   log.Info().Str("role", "myrole").Msg("This is an info message")
   log.Debug().Int("id", 123).Msg("This is a debug message that won't be displayed")
   log.Error().Msg("This is an error message")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output:
12:38PM INF This is an info message role=myrole
12:38PM ERR This is an error message
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  glog
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;glog&lt;/code&gt; offers a robust feature set for handling verbosity-based logging, which is quite useful for detailed control over log output granularity. &lt;/p&gt;

&lt;p&gt;Unlike traditional log levels (info, warn, error), &lt;code&gt;glog&lt;/code&gt; uses verbosity levels represented by integers. Higher numbers indicate more verbose logs, allowing developers to specify in great detail how much logging should be emitted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/ Basic logging
   glog.Info("This is an informational message")
   glog.Warning("This is a warning message")
   glog.Error("This is an error message")


   // Conditional logging based on verbosity level
   if glog.V(2) {
       glog.Info("This is a verbose level 2 message")
   }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Is fmt also a log package?
&lt;/h3&gt;

&lt;p&gt;The fmt package in Go (Golang) is not primarily a logging package but is used for formatting and printing output. It's part of the Go standard library and provides functions to format strings, numbers, and other data into strings and to print them to standard output (your console) or other outputs.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>go</category>
    </item>
  </channel>
</rss>
