SoFunction
Updated on 2025-03-02

Introduction to the usage of golang structured log/slog package

The previous article explainedCorrect wrapping method for LogValuer and logging functions in log/slog package, this article explains in detail how to use structures and points that need to be paid attention to.

The Record type is used to save the information of log events, and is defined as follows:

type Record struct {
    Time 
    Message string
    Level Level
    PC uintptr
    front [nAttrsInline]Attr
    nFront int
    back []Attr
}

There are several methods:

func (r Record) Clone() Record, returns a copy that does not share state. Both Record and its cloned copy can be modified and will not interfere with each other.

func (r Record) NumAttrs() int, returns the number of attributes in the log record.

func (r Record) Attrs(f func(Attr) bool), uses the function f to act on each property in the log record, and if f returns false, iterating stops.

func (r *Record) AddAttrs(attrs ...Attr), appends the given attribute to the attribute list of logging.

func (r *Record) Add(args ...any), convert the parameter to the Attrs described in , and then append the Attrs to the Attrs list in the logging.

func (r Record) source() *Source, returns the Source of the log event.

Example of usage

You can use a function to create a Record object. The example code for recording logs using Record is as follows:

package main
 
import (
    "context"
    "log/slog"
    "os"
    "time"
)
 
func main() {
    logger := ((, nil))
    r := ((), , "test", 1)
    ("title", "Ludoshin's Blog")
    _ = ().Handle((), r)
}

Run the program and the output result is as follows:

time=2023-10-17T21:01:12.041+08:00 level=INFO msg=test title=Lu Duoxin's blog

In daily usage scenarios, there will be a situation where you first create a basic Record object, and then use different handlers to do different processing based on this object. In this scenario, you need to call the Clone method to clone a copy of the Record object and then pass it to other Handlers for processing. In this way, the modification of the copy will not affect the original object, and the modification of the original object will not affect the copy. The sample code is as follows:

package main
 
import (
    "context"
    "log/slog"
    "os"
    "time"
)
 
func main() {
    logger := ((, nil))
    r := ((), , "test", 1)
    ("title", "Ludoshin's Blog")
    _ = ().Handle((), r)
 
    r2 := ()
    ("subTitle", "Lu Duosin's thoughts")
    l := (, nil)
    _ = ((), r2)
}

The output results of running the program are as follows:

time=2023-10-17T21:24:23.588+08:00 level=INFO msg=test title=Lu Duoxin's blog
{"time":"2023-10-17T21:24:23.588517+08:00","level":"INFO","msg":"test","title":"Lu Duoxin's blog","subTitle":"Lu Duoxin's thoughts"}

summary

This article explains the simple usage methods of structures and points to be paid attention to when using them. The corresponding methods are relatively simple to use. They will no longer be explained one by one, and you can try it yourself.

This is the introduction to this article about the usage of golang structured log/slog package. For more related go structured log slog content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!