Esta página se muestra en inglés. Una traducción al español está en curso.
ScriptsMay 15, 2026·3 min de lectura

Logrus — Structured Logging Library for Go

A structured logger for Go with pluggable hooks, formatters, and level-based filtering. Drop-in replacement for the standard library logger with JSON and text output built in.

Listo para agents

Este activo puede ser leído e instalado directamente por agents

TokRepo expone un comando CLI universal, contrato de instalación, metadata JSON, plan según adaptador y contenido raw para que los agents evalúen compatibilidad, riesgo y próximos pasos.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
Logrus Overview
Comando CLI universal
npx tokrepo install 7fac8067-5036-11f1-9bc6-00163e2b0d79

Introduction

Logrus is a structured logger for Go that produces both human-readable text and machine-parsable JSON output. It serves as a drop-in replacement for the standard log package while adding structured fields, log levels, and a hook system for routing entries to external services.

What Logrus Does

  • Emits structured log entries with arbitrary key-value fields
  • Supports seven severity levels from Trace to Fatal
  • Provides pluggable formatters including JSON and colored text
  • Offers a hook interface for sending entries to Syslog, Logstash, Elasticsearch, or custom backends
  • Works as a drop-in replacement for the Go standard library logger

Architecture Overview

Logrus centers on an Entry struct that carries a map of fields plus metadata (timestamp, level, message). Each Logger instance holds a formatter, output writer, and a slice of hooks. When a log method is called, the logger creates an Entry, runs all registered hooks for that level, formats the entry, and writes it to the output. Thread safety is achieved via a mutex on the logger.

Setup & Configuration

  • Import with the alias log to replace the standard logger seamlessly
  • Set the global level with log.SetLevel(log.DebugLevel)
  • Switch to JSON output via log.SetFormatter(&log.JSONFormatter{})
  • Register hooks with log.AddHook(myHook) for side-channel delivery
  • Create multiple logger instances with logrus.New() for isolated configuration

Key Features

  • Field-based structured logging with WithFields and WithField
  • Fatal and Panic levels that call os.Exit(1) or panic() after logging
  • Thread-safe by default with per-logger mutexes
  • Caller reporting via log.SetReportCaller(true) for file and line info
  • Large ecosystem of third-party hooks and formatters

Comparison with Similar Tools

  • Zap — higher throughput with zero-allocation design but less ergonomic API
  • Zerolog — even faster zero-allocation logger focused on JSON output
  • slog (Go 1.21+) — standard library structured logging, fewer features but no dependency
  • Apex/log — similar API with a handler-based architecture instead of hooks

FAQ

Q: Is Logrus still maintained? A: The author considers it feature-complete and in maintenance mode. Security patches and critical fixes are still applied, but new features are unlikely.

Q: How does Logrus compare to slog? A: slog is built into Go 1.21+ and has lower overhead. Logrus offers a richer hook ecosystem and more formatters but adds an external dependency.

Q: Can I use Logrus in performance-critical paths? A: Logrus allocates on each log call. For hot paths, consider Zap or Zerolog which offer zero-allocation modes.

Q: Does Logrus support context-based logging? A: Yes, use log.WithContext(ctx) to attach a context, and hooks can extract values from it.

Sources

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados