Code with Go
Ask or search…
K
Links

Logging

This program demonstrates the use of the standard log package and the log/slog package (structured logger) in Go. Let me break down the code and provide some explanations:
package main
import (
"bytes"
"fmt"
"log"
"os"
"log/slog"
)
func main() {
// Standard logger usage
log.Println("standard logger")
// Modify standard logger flags to include microseconds
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
log.Println("with micro")
// Modify standard logger flags to include file and line
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.Println("with file/line")
// Creating a custom logger (mylog) with a prefix and specific flags
mylog := log.New(os.Stdout, "my:", log.LstdFlags)
mylog.Println("from mylog")
// Changing prefix for the custom logger
mylog.SetPrefix("ohmy:")
mylog.Println("from mylog")
// Creating a logger (buflog) that writes to a buffer
var buf bytes.Buffer
buflog := log.New(&buf, "buf:", log.LstdFlags)
buflog.Println("hello")
// Printing the content of the buffer
fmt.Print("from buflog:", buf.String())
// Using the slog package to create a structured logger (myslog)
jsonHandler := slog.NewJSONHandler(os.Stderr, nil)
myslog := slog.New(jsonHandler)
// Logging messages with additional key-value pairs
myslog.Info("hi there")
myslog.Info("hello again", "key", "val", "age", 25)
}

Output

2024/01/25 17:41:04 standard logger
2024/01/25 17:41:04.107521 with micro
2024/01/25 17:41:04 logging.go:20: with file/line
my:2024/01/25 17:41:04 from mylog
ohmy:2024/01/25 17:41:04 from mylog
from buflog:buf:2024/01/25 17:41:04 hello
{"time":"2024-01-25T17:41:04.1080462+05:30","level":"INFO","msg":"hi there"}
{"time":"2024-01-25T17:41:04.1080462+05:30","level":"INFO","msg":"hello again","key":"val","age":25}
Explanation:
  1. 1.
    Standard Logger (log package):
    • The program starts by using the standard logger (log.Println) with various log messages.
    • Flags for the standard logger are modified to include microseconds and file/line information.
  2. 2.
    Custom Logger (log.New):
    • A custom logger (mylog) is created using log.New with a specific output (os.Stdout), prefix ("my:"), and flags.
    • The prefix is later modified to "ohmy:".
  3. 3.
    Buffered Logger (log.New with bytes.Buffer):
    • Another logger (buflog) is created that writes log messages to a buffer (bytes.Buffer).
    • The content of the buffer is printed to the standard output.
  4. 4.
    Structured Logger (log/slog package):
    • The slog package is used to create a structured logger (myslog) with a JSON formatter.
    • Log messages are provided with additional key-value pairs.
You can run the program to observe the different log messages and how they are formatted based on the logger configurations:
go run main.go