From e542641bb98d116eb6a238c40ee870b4807b1a7c Mon Sep 17 00:00:00 2001 From: Dmytro Momot Date: Mon, 29 Jul 2024 20:42:34 +0300 Subject: [PATCH] Add Slog adapter for async logging Introduce a new `slogAdapter` to integrate `slog` with the `asynq` logging interface. This adapter implements Debug, Info, Warn, Error, and Fatal methods, providing seamless logging at various levels. --- slog_adapter.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 slog_adapter.go diff --git a/slog_adapter.go b/slog_adapter.go new file mode 100644 index 0000000..1bd5f42 --- /dev/null +++ b/slog_adapter.go @@ -0,0 +1,43 @@ +package asyncer + +import ( + "log/slog" + "os" + + "github.com/hibiken/asynq" +) + +type slogAdapter struct { + log slog.Logger +} + +func NewSlogAdapter(log slog.Logger) asynq.Logger { + return &slogAdapter{log: log} +} + +// Debug logs a message at Debug level. +func (s *slogAdapter) Debug(args ...interface{}) { + s.log.Debug("", args...) +} + +// Info logs a message at Info level. +func (s *slogAdapter) Info(args ...interface{}) { + s.log.Info("", args...) +} + +// Warn logs a message at Warning level. +func (s *slogAdapter) Warn(args ...interface{}) { + s.log.Warn("", args...) +} + +// Error logs a message at Error level. +func (s *slogAdapter) Error(args ...interface{}) { + s.log.Error("", args...) +} + +// Fatal logs a message at Fatal level +// and process will exit with status set to 1. +func (s *slogAdapter) Fatal(args ...interface{}) { + s.log.Error("", args...) + os.Exit(1) +}