slog

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2025 License: MIT Imports: 6 Imported by: 3

README

slog-kratos

Go Report Card Go.Dev reference

A simple and efficient adapter to use Go's standard structured logging library (slog) as a logger within the Kratos framework.

This library implements the log.Logger interface from the Kratos project, allowing you to seamlessly integrate slog into your Kratos applications for powerful, structured, and level-based logging.

✨ Features

  • Seamless Integration: Implements the log.Logger interface for direct use in Kratos.
  • Structured Logging: Leverage the full power of slog for structured, key-value pair logging.
  • Level-based Logging: Supports different log levels (Debug, Info, Warn, Error).
  • Customizable: Easily configure the underlying slog.Handler (e.g., slog.TextHandler, slog.JSONHandler) to control log format and output.
  • High Performance: Built on Go's standard library, ensuring minimal overhead.

🚀 Installation

To install slog-kratos, use go get:

go get github.com/origadmin/slog-kratos

💡 Usage

Integrating the logger into your Kratos application is straightforward. You can create a new slog logger and pass it to the Kratos application.

Here is a basic example:

package main

import (
	"context"
	"log/slog"
	"os"

	kratoslog "github.com/go-kratos/kratos/v2/log"
	slogkratos "github.com/origadmin/slog-kratos"
)

func main() {
	// 1. Create a standard slog handler (e.g., JSONHandler)
	slogHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
		Level: slog.LevelDebug, // Set your desired log level
	})

	// 2. Create the slog-kratos logger adapter
	logger := slogkratos.NewLogger(slogHandler)

	// 3. Use it as a Kratos logger
	kratosLogger := kratoslog.NewHelper(logger)

	// 4. Log messages with key-value pairs
	kratosLogger.WithContext(context.Background()).Info("message", "hello world", "user", "kratos")
	kratosLogger.WithContext(context.Background()).Warnw("key1", "value1", "key2", 123)
	kratosLogger.Error("This is an error message")

	// Example Output (JSON):
	// {"time":"2023-10-27T10:00:00.000Z","level":"INFO","msg":"hello world","user":"kratos"}
	// {"time":"2023-10-27T10:00:00.000Z","level":"WARN","msg":"","key1":"value1","key2":123}
	// {"time":"2023-10-27T10:00:00.000Z","level":"ERROR","msg":"This is an error message"}
}

🤝 Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

Please see CONTRIBUTING.md for guidelines.

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Overview

Package slog provides a structured logging system.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

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

Logger represents a structured logger.

func NewLogger

func NewLogger(opts ...Option) *Logger

NewLogger returns a new Logger instance with the given options.

func (*Logger) Close

func (l *Logger) Close() error

Close closes the logger.

func (*Logger) Context

func (l *Logger) Context() context.Context

Context returns the context associated with the logger.

func (*Logger) Log

func (l *Logger) Log(level log.Level, keyvals ...interface{}) error

Log logs a message at the given level with the given key-value pairs.

type Option

type Option = func(log *Logger)

Option is a function that configures a Logger instance.

func WithContext

func WithContext(ctx context.Context) Option

WithContext returns an Option that sets the context for the logger.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger returns an Option that sets the underlying slog.Logger instance.

func WithMessageKey

func WithMessageKey(key string) Option

WithMessageKey returns an Option that sets the message key for the logger.

Jump to

Keyboard shortcuts

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