Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log/slog adapter #74

Open
ptman opened this issue Sep 14, 2023 · 2 comments
Open

log/slog adapter #74

ptman opened this issue Sep 14, 2023 · 2 comments

Comments

@ptman
Copy link

ptman commented Sep 14, 2023

No description provided.

@ptman
Copy link
Author

ptman commented Oct 4, 2023

Starting point could be:

type slogAdapter struct {
        logger *slog.Logger
}

func (sa *slogAdapter) Log(ctx context.Context, level sqldblogger.Level,
        msg string, data map[string]any) {
        args := make([]any, 0, len(data))

        for k, v := range data {
                if k == "time" {
                        continue
                }

                k := k
                v := v
                args = append(args, slog.Any(k, v))
        }

        switch level { //nolint:exhaustive
        case sqldblogger.LevelError:
                sa.logger.ErrorContext(ctx, msg, args...)
        case sqldblogger.LevelInfo:
                sa.logger.InfoContext(ctx, msg, args...)
        case sqldblogger.LevelDebug:
                sa.logger.DebugContext(ctx, msg, args...)
        default:
                sa.logger.DebugContext(ctx, msg, args...)    
        }                                              
}                                    

@SimonWoidig
Copy link

This snippet is good enough for me I guess.

/*
MIT License

# Copyright (c) 2024 Šimon Woidig

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package slogadapter

import (
	"context"
	"log/slog"

	sqldblogger "github.com/simukti/sqldb-logger"
)

// SqlDBLoggerLevelSlog implements slog.Leveler.
// This level is used to convert sqldblogger.Level to slog.Level.
type SqlDBLoggerLevelSlog sqldblogger.Level

// Level implements slog.Leveler.
func (s *SqlDBLoggerLevelSlog) Level() slog.Level {
	switch *s {
	case SqlDBLoggerLevelSlog(sqldblogger.LevelTrace):
		fallthrough
	case SqlDBLoggerLevelSlog(sqldblogger.LevelDebug):
		return slog.LevelDebug
	case SqlDBLoggerLevelSlog(sqldblogger.LevelInfo):
		return slog.LevelInfo
	case SqlDBLoggerLevelSlog(sqldblogger.LevelError):
		return slog.LevelError
	default:
		return slog.LevelInfo
	}
}

// Interface guard
var _ slog.Leveler = (*SqlDBLoggerLevelSlog)(nil)

// SlogAdapter implements sqldblogger.Logger.
// This adapter logs using the standard `log/slog` package.
type SlogAdapter struct {
	logger *slog.Logger
}

// Log implements sqldblogger.Logger.
func (s *SlogAdapter) Log(ctx context.Context, level sqldblogger.Level, msg string, data map[string]interface{}) {
	slogLevel := SqlDBLoggerLevelSlog(level)
	s.logger.Log(ctx, slogLevel.Level(), msg, slog.Any("data", data))
}

// Interface guard
var _ sqldblogger.Logger = (*SlogAdapter)(nil)

// NewSlogAdapter creates a new SlogAdapter from a given slog.Logger.
func NewSlogAdapter(logger *slog.Logger) *SlogAdapter {
	return &SlogAdapter{
		logger: logger,
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants