Logy|Docs

Quick start

Install Logy, create a logger, and configure your first output

Introduction

Logy is a configurable logger for Go applications. In this guide, you install the package, create a logger, write messages, attach request context, and enable structured output.

If the package is not installed yet, add it first:

Terminal
go get go.codnect.io/logy

Create a logger

Use logy.Get when the logger should be named from the package where it is called. This keeps logs tied to the code that produced them without passing names around manually.

main.go
package main

import "go.codnect.io/logy"

func main() {
	log := logy.Get()

	log.Info("application started")
	log.Warn("cache miss for key {}", "user:42")
	log.Error("request failed")
	log.Debug("loaded {} routes", 12)
	log.Trace("created internal cache")
}

Info, Warn, Error, Debug, and Trace write at different levels. Message arguments use {} placeholders, so call sites stay compact while the logger handles formatting.

Run the program:

Terminal
go run .

The console output stays readable while keeping time, level, logger name, and message visible:

terminal output
2023-03-19 20:05:36.264481INFOgithub.com/codnect/logy/test:This is an information message2023-03-19 20:05:36.264622WARNgithub.com/codnect/logy/test:This is a warning message2023-03-19 20:05:36.264625ERRORgithub.com/codnect/logy/test:This is an error message2023-03-19 20:05:36.264627DEBUGgithub.com/codnect/logy/test:This is a debug message2023-03-19 20:05:36.264628TRACEgithub.com/codnect/logy/test:This is a trace message

Add request context

Use context fields when each request, job, or operation should carry values such as trace IDs, span IDs, tenant IDs, or user IDs.

main.go
package main

import (
	"context"

	"go.codnect.io/logy"
)

func main() {
	log := logy.Get()

	ctx := logy.WithValue(context.Background(), "traceId", "trace-123")
	ctx = logy.WithValue(ctx, "spanId", "span-456")

	log.I(ctx, "request completed")
}

Context values are attached to the log record by the handler. Your application code can keep the same logger while request-specific metadata moves through context.Context.

With trace and span values in context, the formatted output includes them next to the timestamp:

context output
2023-03-19 20:18:10.062[anyTraceId,anySpanId]INFOgithub.com/codnect/logy/test:info message2023-03-19 20:18:10.062[anyTraceId,anySpanId]WARNgithub.com/codnect/logy/test:warning message2023-03-19 20:18:10.062[anyTraceId,anySpanId]ERRORgithub.com/codnect/logy/test:error message2023-03-19 20:18:10.062[anyTraceId,anySpanId]DEBUGgithub.com/codnect/logy/test:debug message2023-03-19 20:18:10.062[anyTraceId,anySpanId]TRACEgithub.com/codnect/logy/test:trace message

Configure output

Use logy.LoadConfig when output settings are assembled by the application. This example enables JSON records for the console handler.

main.go
err := logy.LoadConfig(&logy.Config{
	Console: &logy.ConsoleConfig{
		Enabled: true,
		Json: &logy.JsonConfig{
			Enabled: true,
		},
	},
})
if err != nil {
	panic(err)
}

Configuration controls levels, handlers, output format, JSON fields, and package specific behavior without changing the logging calls.

What happened?

logy.Get created a package-based logger. The level methods wrote messages, the context helpers attached request metadata, and LoadConfig changed how records are emitted.

Continue with Configuration to customize levels, handlers, formats, and JSON fields.