ntroduces a new package to the standard library called slog; This package gives Go developers the ability to record log entries in a structured format without too much hassle. One of my favorite features about this package is the ability to print out the line number and file a log entry is coming from. Have you ever found yourself searching for the origin of a log entry within your source code? Well, search no more, for slog is here.
Text Handlers
In the natural Go fashion, the development team provided the ability to construct a new slog.Logger instance by calling the package’s New function and passing a variable that implements the slog.Handler interface. I won’t be implementing this interface, but rather, use the existing slog.TextHandler type.
Listing 1
func NewTextHandler(w io.Writer, opts *HandlerOptions) *TextHandler
Listing 1 depicts the signature of the slog package’s NewTextHandler which acts as a factory function to create a new instance of the TextHandler type. The first parameter is an io.Writer and I’ll be passing os.Stdout to write to standard output; the second parameter is where the magic happens and will be where I’ll specify if the logger should specify the file and line number a log entry is coming from.