Overview
Thelog package implements simple logging functionality. It defines the Logger type with methods for formatting output. The package also has a predefined ‘standard’ Logger accessible through helper functions that write to standard error.
Types
Logger
A Logger represents an active logging object that generates lines of output to an io.Writer.Constructor
Creates a new Logger. The out variable sets the destination to which log data will be written. The prefix appears at the beginning of each generated log line. The flag argument defines the logging properties.
Methods
Calls Output to print to the logger. Arguments are handled in the manner of fmt.Print.
Calls Output to print to the logger. Arguments are handled in the manner of fmt.Printf.
Calls Output to print to the logger. Arguments are handled in the manner of fmt.Println.
Equivalent to Print() followed by a call to os.Exit(1).
Equivalent to Printf() followed by a call to os.Exit(1).
Equivalent to Println() followed by a call to os.Exit(1).
Equivalent to Print() followed by a call to panic().
Equivalent to Printf() followed by a call to panic().
Equivalent to Println() followed by a call to panic().
Sets the output destination for the logger.
Sets the output flags for the logger.
Sets the output prefix for the logger.
Returns the output flags for the logger.
Returns the output prefix for the logger.
Returns the output destination for the logger.
Constants
These flags define which text to prefix to each log entry.The date in the local time zone: 2009/01/23
The time in the local time zone: 01:23:23
Microsecond resolution: 01:23:23.123123 (assumes Ltime)
Full file name and line number: /a/b/c/d.go:23
Final file name element and line number: d.go:23 (overrides Llongfile)
If Ldate or Ltime is set, use UTC rather than the local time zone
Move the “prefix” from the beginning of the line to before the message
Initial values for the standard logger (Ldate | Ltime)
Package-Level Functions
These functions write to the standard logger.Calls Output to print to the standard logger.
Calls Output to print to the standard logger.
Calls Output to print to the standard logger.
Equivalent to Print followed by os.Exit(1).
Equivalent to Printf followed by os.Exit(1).
Equivalent to Println followed by os.Exit(1).
Equivalent to Print followed by panic().
Equivalent to Printf followed by panic().
Equivalent to Println followed by panic().
Sets the output destination for the standard logger.
Sets the output flags for the standard logger.
Sets the output prefix for the standard logger.
Returns the standard logger used by the package-level output functions.
log/slog - Structured Logging
Thelog/slog package provides structured logging with levels (Debug, Info, Warn, Error) and key-value pairs.
Types
A Logger records structured information about each call to its Log, Debug, Info, Warn, and Error methods.
A Handler handles log records produced by a Logger.
A Level is the importance or severity of a log event.Standard levels:
LevelDebug(-4)LevelInfo(0)LevelWarn(4)LevelError(8)
A Record holds information about a log event.
An Attr is a key-value pair.
Functions
Creates a new Logger with the given non-nil Handler.
Returns the default Logger.
Makes l the default Logger.
Calls Logger.Debug on the default logger.
Calls Logger.Info on the default logger.
Calls Logger.Warn on the default logger.
Calls Logger.Error on the default logger.
Built-in Handlers
Creates a JSONHandler that writes Records to w as line-delimited JSON objects.
Creates a TextHandler that writes Records to w as a sequence of key=value pairs.
Examples
Basic Logging
Custom Logger
Custom Flags
Structured Logging with slog
slog with Levels
slog with Groups
slog with Context
Custom slog Handler
Comparison: log vs slog
| Feature | log | slog |
|---|---|---|
| Output Format | Unstructured text | Structured (JSON/text) |
| Performance | Fast | Optimized for structured |
| Levels | None (manual) | Built-in (Debug/Info/Warn/Error) |
| Context | No | Yes |
| Attributes | No | Yes (key-value pairs) |
| Use Case | Simple logging | Production apps, observability |