-
Notifications
You must be signed in to change notification settings - Fork 15
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
Comments
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...)
}
} |
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
No description provided.
The text was updated successfully, but these errors were encountered: