codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

38 / 72 topics
27Standard Library Overview28IO Package29Net Package30HTTP Package31JSON Package32XML Package33Time Package34OS Package35Fmt Package36Math Package37Regexp Package38Log Package39Flag Package40Context Package41Embed Package
Tutorials/Go (Golang)/Log Package
🐹Go (Golang)

Log Package

Updated 2026-04-20
3 min read

Log Package

Introduction

Logging is a crucial aspect of software development, enabling developers to monitor and debug applications effectively. In Go (Golang), the log package provides a simple yet powerful logging mechanism that can be used across different parts of an application. This tutorial will cover the basics of using the log package, including how to create loggers, set log levels, format logs, and handle log output.

Basic Usage

The log package in Go is part of the standard library, so you don't need to install anything extra to use it. Here's a simple example to get started:

package main

import (
	"log"
)

func main() {
	log.Println("This is an informational message")
}

In this example, log.Println outputs a log message with the current date and time followed by the provided text.

Loggers

The log package provides a default logger that can be used directly. However, you can also create custom loggers using the New function:

package main

import (
	"log"
	"os"
)

func main() {
	// Create a new logger with specific flags and output destination
	logger := log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)

	// Use the custom logger
	logger.Println("This is an informational message")
}

In this example, we create a new logger that writes to os.Stdout with a prefix of "INFO: " and includes the date, time, and file name in the log output.

Log Flags

The log.New function allows you to specify flags that control the format of the log output. Here are some commonly used flags:

  • log.Ldate: Include the current date.
  • log.Ltime: Include the current time.
  • log.Lmicroseconds: Include the current time with microseconds precision.
  • log.Llongfile: Include the full path and file name.
  • log.Lshortfile: Include just the file name.
  • log.LstdFlags: Equivalent to Ldate | Ltime.

Log Levels

The log package does not provide built-in support for log levels like "info", "warning", or "error". However, you can implement this functionality using custom loggers and prefixes:

package main

import (
	"log"
	"os"
)

func main() {
	infoLogger := log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime)
	warningLogger := log.New(os.Stdout, "WARNING: ", log.Ldate|log.Ltime)
	errorLogger := log.New(os.Stdout, "ERROR: ", log.Ldate|log.Ltime)

	infoLogger.Println("This is an informational message")
	warningLogger.Println("This is a warning message")
	errorLogger.Println("This is an error message")
}

In this example, we create three loggers with different prefixes to represent different log levels.

Handling Log Output

By default, the log package writes log messages to os.Stderr. However, you can redirect logs to other destinations like files or network connections:

package main

import (
	"log"
	"os"
)

func main() {
	file, err := os.OpenFile("app.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		log.Fatalf("Failed to open log file: %v", err)
	}
	defer file.Close()

	logger := log.New(file, "INFO: ", log.Ldate|log.Ltime)

	logger.Println("This is an informational message")
}

In this example, we create a logger that writes logs to a file named app.log.

Best Practices

  1. Use Structured Logging: While the log package provides basic logging, consider using structured logging libraries like zap or logrus for more advanced features such as JSON output and log levels.

  2. Avoid Mixing Loggers: Use consistent logger configurations across your application to maintain a uniform log format.

  3. Handle Errors Gracefully: Always check for errors when opening files or setting up loggers to avoid silent failures.

  4. Use Contextual Logging: Include contextual information like request IDs in logs to make them more useful for debugging and monitoring.

  5. Rotate Log Files: For long-running applications, implement log rotation to prevent log files from growing indefinitely and consuming disk space.

Conclusion

The log package in Go provides a simple yet flexible logging mechanism that can be used to add basic logging functionality to your applications. While it lacks some advanced features found in other logging libraries, it is sufficient for many use cases. By understanding how to create custom loggers, set log levels, and handle log output, you can effectively monitor and debug your Go applications.

For more advanced logging needs, consider exploring third-party libraries that offer additional features like structured logging, log rotation, and asynchronous logging.


PreviousRegexp PackageNext Flag Package

Recommended Gear

Regexp PackageFlag Package