Display the source code reference that’s writing a log message.
<p>ntroduces a new package to the standard library called <code>slog</code>; 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 <code>slog</code> is here.</p>
<h2>Text Handlers</h2>
<p>In the natural Go fashion, the development team provided the ability to construct a new <code>slog.Logger</code> instance by calling the package’s <code>New</code> function and passing a variable that implements the <code>slog.Handler</code> interface. I won’t be implementing this interface, but rather, use the existing <code>slog.TextHandler</code> type.</p>
<p><strong>Listing 1</strong></p>
<pre>
func NewTextHandler(w io.Writer, opts *HandlerOptions) *TextHandler</pre>
<p>Listing 1 depicts the signature of the <code>slog</code> package’s <code>NewTextHandler</code> which acts as a factory function to create a new instance of the <code>TextHandler</code> type. The first parameter is an <code>io.Writer</code> and I’ll be passing <code>os.Stdout</code> 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.</p>
<p><a href="https://blog.stackademic.com/display-the-source-code-reference-writing-a-log-message-dc660b758b3e">Read More</a></p>