> For the complete documentation index, see [llms.txt](https://ineelhere.gitbook.io/code-with-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ineelhere.gitbook.io/code-with-go/73-logging.md).

# 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:

```go
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. **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. **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. **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. **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:

```bash
go run main.go
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/73-logging.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
