Debian Codegraph @

plugin/pslog/logger.go

raw symbols SCIP
1 // Copyright (c) 2025 Alexey Mayshev and contributors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Package pslog provides a plug-in otter.Logger wrapping slog.Logger for usage in
16 // an otter.Cache.
17 //
18 // This can be used like so:
19 //
20 // cache := otter.Must(&otter.Options[string, string]{
21 // Logger: pslog.New(slog.Default()),
22 // // ...other opts
23 // })
24 package pslog
25
26 import (
27 "context"
28 "log/slog"
29
30 "github.com/maypok86/otter/v2"
31 )
32
33 var _ otter.Logger = (*Logger)(nil)
34
35 // Option applies options to the logger.
36 type Option func(*options)
37
38 type options struct{}
39
40 // Logger that wraps the slog.Logger.
41 type Logger struct {
42 log *slog.Logger
43 }
44
45 // New returns a new Logger.
46 func New(log *slog.Logger, opts ...Option) *Logger {
47 if log == nil {
48 panic("pslog: log is nil")
49 }
50 return &Logger{
51 log: log,
52 }
53 }
54
55 // Warn is for the otter.Logger interface.
56 func (l *Logger) Warn(ctx context.Context, msg string, err error) {
57 l.log.WarnContext(ctx, msg, slog.Any("err", err))
58 }
59
60 // Error is for the otter.Logger interface.
61 func (l *Logger) Error(ctx context.Context, msg string, err error) {
62 l.log.ErrorContext(ctx, msg, slog.Any("err", err))
63 }