ProgramGuru

The log Package

Start with The log Package

The log package records operational messages; structured logging builds on that foundation for production observability.

What you’ll learn

  • Explain the main purpose of the log package.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

A log.Logger can write to a supplied output stream with configurable prefix and flags. The example uses an in-memory bytes.Buffer and disables timestamps so its output is deterministic. Avoid logging credentials or personal secrets.

The log/slog package provides structured key/value logging; it is better suited to many production applications where log records need machine-readable fields. Logging is not a replacement for returning errors to callers.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for logLogging gives operations context but does not replace error handling.Application eventStep 1Logger formattingStep 2Output destinationStep 3Developer reads logStep 4Logging gives operations context but does not replace error handling.
Logging gives operations context but does not replace error handling.

Tools, syntax and practical notes

For structured logging, see log/slog.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"bytes"
	"fmt"
	"log"
)

func main() {
	var output bytes.Buffer
	logger := log.New(&output, "APP: ", 0)
	logger.Println("task started")
	logger.Println("task completed")
	fmt.Print(output.String())
}

Output

APP: task started
APP: task completed

Trace what the program does

The logger writes two prefixed lines to a buffer. Flags zero removes date/time decoration, so printing the buffer yields the same text on every run.

Avoid common mistakes

  • Default loggers often include timestamps, which makes literal output vary.
  • Avoid logging sensitive tokens or duplicating the same returned error at every layer.

Try it yourself

Set a different logger prefix and log the start and completion of a second task.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Why construct a logger with flags set to 0 in this example?
Choose one answer. You can try again.

Answer and explanation

To omit automatic timestamps for consistent output

Correct. The logger writes two prefixed lines to a buffer.

What you should remember

You have practiced The log Package in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Introduction to Generics →