Skip to content
/ logslog Public

A proxy drop-in replacement for the standard log into log/slog with proper levels

Notifications You must be signed in to change notification settings

lzap/logslog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

log slog proxy

Package logslog provides a compatibility layer for the standard log package.
It allows to use slog.Logger as a drop-in replacement for the standard log.Logger.
The package provides a Logger type that implements the standard log.Logger interface.
The Logger type is a proxy for slog.Logger and forwards all log messages to the slog logger,
normal messages as INFO and fatal messages as ERROR.

While the log/slog package provides a compatiblity legacy standard logger, it does print
all messages to the same level. This package provides a more fine-grained control over
the log levels.

EXAMPLE

package main

import (
	"log/slog"
	"os"

	log "github.com/lzap/logslog"
)

func main() {
	log.Print("Uninitialized Hello", "world!")
	log.Printf("Uninitialized Hello %s!", "world")
	log.Println("Uninitialized Hello world!")

	log.SetDefault(log.NewSlog(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))))

	log.Print("Hello", "world!")
	log.Printf("Hello %s!", "world")
	log.Println("Hello world!")

	log.Fatal("Exit")
}

Prints something like:

2025/01/24 15:03:37 INFO Uninitialized Hello world!
2025/01/24 15:03:37 INFO Uninitialized Hello world!
2025/01/24 15:03:37 INFO Uninitialized Hello world!
time=2025-01-24T15:03:37.907+01:00 level=INFO msg="Hello world!"
time=2025-01-24T15:03:37.907+01:00 level=INFO msg="Hello world!"
time=2025-01-24T15:03:37.907+01:00 level=INFO msg="Hello world!\n"
time=2025-01-24T15:03:37.907+01:00 level=ERROR msg=Exit

LICENSE

Copyright 2009 The Go Authors. All rights reserved. Copyright 2025 Lukáš Zapletal. All rights reserved.

Use of this source code is governed by a BSD-style license that can be found in the LICENSE file
shipped with the Go language.

About

A proxy drop-in replacement for the standard log into log/slog with proper levels

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages