Display the source code reference that’s writing a log message.

<p>ntroduces a new package to the standard library called&nbsp;<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&nbsp;<code>slog</code>&nbsp;is here.</p> <h2>Text Handlers</h2> <p>In the natural Go fashion, the development team provided the ability to construct a new&nbsp;<code>slog.Logger</code>&nbsp;instance by calling the package&rsquo;s&nbsp;<code>New</code>&nbsp;function and passing a variable that implements the&nbsp;<code>slog.Handler</code>&nbsp;interface. I won&rsquo;t be implementing this interface, but rather, use the existing&nbsp;<code>slog.TextHandler</code>&nbsp;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&nbsp;<code>slog</code>&nbsp;package&rsquo;s&nbsp;<code>NewTextHandler</code>&nbsp;which acts as a factory function to create a new instance of the&nbsp;<code>TextHandler</code>&nbsp;type. The first parameter is an&nbsp;<code>io.Writer</code>&nbsp;and I&rsquo;ll be passing&nbsp;<code>os.Stdout</code>&nbsp;to write to standard output; the second parameter is where the magic happens and will be where I&rsquo;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>
Tags: Code Source