github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/message/handler.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package message provides a rich set of functions for displaying messages to the user.
     5  package message
     6  
     7  import (
     8  	"context"
     9  	"log/slog"
    10  )
    11  
    12  // JackalHandler is a simple handler that implements the slog.Handler interface
    13  type JackalHandler struct{}
    14  
    15  // Enabled is always set to true as jackal logging functions are already aware of if they are allowed to be called
    16  func (z JackalHandler) Enabled(_ context.Context, _ slog.Level) bool {
    17  	return true
    18  }
    19  
    20  // WithAttrs is not suppported
    21  func (z JackalHandler) WithAttrs(_ []slog.Attr) slog.Handler {
    22  	return z
    23  }
    24  
    25  // WithGroup is not supported
    26  func (z JackalHandler) WithGroup(_ string) slog.Handler {
    27  	return z
    28  }
    29  
    30  // Handle prints the respective logging function in jackal
    31  // This function ignores any key pairs passed through the record
    32  func (z JackalHandler) Handle(_ context.Context, record slog.Record) error {
    33  	level := record.Level
    34  	message := record.Message
    35  
    36  	switch level {
    37  	case slog.LevelDebug:
    38  		Debug(message)
    39  	case slog.LevelInfo:
    40  		Info(message)
    41  	case slog.LevelWarn:
    42  		Warn(message)
    43  	case slog.LevelError:
    44  		Warn(message)
    45  	}
    46  	return nil
    47  }