Help us shape the future of Go! Take the 2026 Go Developer Survey

gologger

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

README

go-logger

Language: [ EN | RU ]

go-logger is a small Go logger with message levels, tags, and child loggers.

Features

  • package-level default logger with the MAIN tag;
  • INFO, WARN, and ERROR levels;
  • message formatting using fmt.Fprintf semantics;
  • custom loggers with their own io.Writer;
  • child loggers through Logger.Get;
  • global and per-logger logging enable/disable controls.

Installation

go get github.com/80LK/go-logger

Quick Start

package main

import logger "github.com/80LK/go-logger"

func main() {
	logger.Log("service started on port %d", 8080)
	logger.Warn("cache miss for key %q", "user:42")
	logger.Error("request failed: %s", "timeout")
}

Creating a Logger

Create creates a root logger with the given tag and writer.

package main

import (
	"os"

	logger "github.com/80LK/go-logger"
)

func main() {
	log := logger.Create("APP", os.Stdout)

	log.Log("started")
	log.Warn("configuration value %q is deprecated", "old_mode")
	log.Error("cannot connect to database")
}

Child Loggers

A child logger is created with Get. Calling Get again with the same prefix returns the existing logger.

root := logger.Create("APP", os.Stdout)
db := root.Get("DB")

root.Log("application started")
db.Log("connected to %s", "postgres")

A child logger inherits the writer and current enabled state from its parent when it is created.

Enabling and Disabling

Disabling logging globally affects all loggers:

logger.Disable()
logger.Log("this message will not be written")

logger.Enabled()
logger.Log("logging is enabled again")

A single logger can be disabled independently:

log := logger.Create("APP", os.Stdout)

log.Disable()
log.Log("this message will not be written")

log.Enabled()
log.Log("logging is enabled again")

API

Package Functions
  • Get() *Logger - returns the package-level logger with the MAIN tag.
  • Create(prefix string, writer io.Writer) *Logger - creates a new root logger.
  • Log(raw string, values ...any) - writes an INFO message with the package-level logger.
  • Warn(raw string, values ...any) - writes a WARN message with the package-level logger.
  • Error(raw string, values ...any) - writes an ERROR message with the package-level logger.
  • Enabled() - enables logging globally.
  • Disable() - disables logging globally.
Logger Methods
  • IsEnabled() bool - reports whether the logger can currently write messages.
  • Enabled() - enables the logger.
  • Disable() - disables the logger.
  • Log(raw string, values ...any) - writes an INFO message.
  • Warn(raw string, values ...any) - writes a WARN message.
  • Error(raw string, values ...any) - writes an ERROR message.
  • Get(prefix string) *Logger - returns a child logger.
  • Parent() *Logger - returns the parent logger or nil.

Message Format

Messages include the time, level, tag, and message text. The message text is formatted the same way as fmt.Fprintf.

[2006-01-02 15:04:05][INFO][MAIN] service started

License

This project is distributed under the GPL-3.0 license. See LICENSE for details.

Documentation

Overview

Package gologger provides a small tagged logger with package-level helpers and support for child loggers.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrWriterNil is returned when a logger has no writer.
	ErrWriterNil error = errors.New("Writerr is nil")
)

Functions

func Disable

func Disable()

Disable disables logging globally.

func Enabled

func Enabled()

Enabled enables logging globally.

func Error

func Error(raw string, values ...any)

Error writes an error message with the package-level logger with tag MAIN.

func Log

func Log(raw string, values ...any)

Log writes an informational message with the package-level logger with tag MAIN.

func Warn

func Warn(raw string, values ...any)

Warn writes a warning message with the package-level logger with tag MAIN.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger writes formatted log messages with a tag and severity level.

Loggers can form a hierarchy through Get, which creates or returns a child logger that inherits the parent writer and enabled state.

func Create

func Create(prefix string, writer io.Writer) *Logger

Create returns a new root logger with the given prefix and writer.

func Get

func Get() *Logger

Get returns the package-level logger with tag MAIN.

func (*Logger) Disable

func (logger *Logger) Disable()

Disable disables logging for logger.

func (*Logger) Enabled

func (logger *Logger) Enabled()

Enabled enables logging for logger.

func (Logger) Error

func (logger Logger) Error(raw string, values ...any)

Error writes an error message formatted with fmt.Fprintf semantics.

func (*Logger) Get

func (logger *Logger) Get(prefix string) *Logger

Get returns a child logger with the given prefix.

If a child logger with prefix already exists, Get returns the existing logger. Otherwise, it creates a new child logger that uses the same writer and enabled state as logger.

func (Logger) IsEnabled

func (logger Logger) IsEnabled() bool

IsEnabled reports whether logger can currently write log messages.

It returns false when logging is disabled globally, regardless of the logger's own enabled state.

func (Logger) Log

func (logger Logger) Log(raw string, values ...any)

Log writes an informational message formatted with fmt.Fprintf semantics.

func (*Logger) Parent

func (logger *Logger) Parent() *Logger

Parent returns logger's parent logger, or nil when logger is a root logger.

func (Logger) Warn

func (logger Logger) Warn(raw string, values ...any)

Warn writes a warning message formatted with fmt.Fprintf semantics.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL