Go Reference ยท Logging, Flags and Testing
Go log package
Reference for the Go log package: Println, Printf, Fatal, Panic, custom loggers with log.New, prefixes, flags, output destinations and when to use slog instead.
The package log is Go's minimal logger. It writes a line of text to a destination (standard error by default), prefixed with the date and time. It is safe for concurrent use, needs no setup, and is enough for small tools and quick debugging. For structured, leveled logs in services, prefer log/slog.
There is a ready-made standard logger behind the package-level functions (log.Println, log.Printf, ...) and you can create your own with log.New. Each logger has an output io.Writer, a prefix and a set of flag bits that control the header.
log.Println("starting") // 2026/10/30 12:00:00 starting log.Fatalf("cannot open: %v", err) // prints, then os.Exit(1)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
func Print(v ...any) func Printf(format string, v ...any) func Println(v ...any) func Fatal(v ...any) // Print, then os.Exit(1) func Fatalf(format string, v ...any) func Fatalln(v ...any) func Panic(v ...any) // Print, then panic(message) func Panicf(format string, v ...any) func Panicln(v ...any) func New(out io.Writer, prefix string, flag int) *Logger func Default() *Logger func SetOutput(w io.Writer) func SetPrefix(prefix string) func SetFlags(flag int) func Writer() io.Writer
| Flag | Meaning |
|---|---|
Ldate |
Date as 2009/01/23. |
Ltime |
Time as 01:23:23. |
Lmicroseconds |
Time with microseconds (implies showing the time). |
Llongfile |
Full path and line, /a/b/c.go:23. |
Lshortfile |
File name and line, c.go:23. Overrides Llongfile. |
LUTC |
Use UTC instead of local time. |
Lmsgprefix |
Put the prefix before the message instead of at the start of the line. |
LstdFlags |
Ldate | Ltime, the default. |
Edge cases: a newline is appended if the message does not end with one. Fatal* calls os.Exit without running deferred functions. Panic* panics with the formatted string as its value.
Usage notes
- Default destination is stderr, not stdout. Pipes and redirection that capture stdout will not see
logoutput. - Fatal skips defers. Cleanup in deferred calls does not run, so use
Fatalonly inmainor small programs, and return errors elsewhere. - One logger per component. Make loggers with different prefixes (
log.New(os.Stderr, "db: ", log.LstdFlags)) instead of formatting prefixes by hand. - Concurrency. Each call writes its line with a single
Write, so lines from different goroutines do not interleave, assuming the writer itself is safe. - Source locations (
Lshortfile) cost a stack lookup per call. Avoid them in hot paths. log.Writer()as an adapter. Handlog.Writer()orlog.New(...).Writer()to libraries that want anio.Writer, andslog.NewLogLoggerto bridgesloghandlers to APIs expecting*log.Logger(for examplehttp.Server.ErrorLog).- No levels. The package has no debug or error levels; encode them in the message or switch to
log/slog.