Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewCombinedHandler ¶
NewCombinedHandler will return a single CombinedHandler that writes logs to multiple streams via all the handlers passed in the arguments.
Example ¶
package main import ( "log/slog" "os" "github.com/ksdfg/loggy" ) func main() { // Create handlers for different log levels debugTextLogHandler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}) infoJSONLogHandler := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo}) warnTextLogHandler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelWarn, AddSource: true}) errorJSONLogHandler := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError, AddSource: true}) // Add attributes to each handler so that we know which logs came from which handler debugTextLogHandlerWithAttr := debugTextLogHandler.WithAttrs( []slog.Attr{slog.String("logger", "debugTextLogHandler")}, ) infoJSONLogHandlerWithAttr := infoJSONLogHandler.WithAttrs( []slog.Attr{slog.String("logger", "infoJSONLogHandler")}, ) warnTextLogHandlerWithAttr := warnTextLogHandler.WithAttrs( []slog.Attr{slog.String("logger", "warnTextLogHandler")}, ) errorJSONLogHandlerWithAttr := errorJSONLogHandler.WithAttrs( []slog.Attr{slog.String("logger", "errorJSONLogHandler")}, ) // Create a combined handler with the above handlers combinedHandler := loggy.NewCombinedHandler( debugTextLogHandlerWithAttr, infoJSONLogHandlerWithAttr, warnTextLogHandlerWithAttr, errorJSONLogHandlerWithAttr, ) // Create a new logger with the combined handler, and set it as the default logger := slog.New(combinedHandler) slog.SetDefault(logger) // Log a debug message slog.Debug("this is a debug log") // Log an info message slog.Info("this is an info log") // Log a warning message slog.Warn("this is a warning log") // Log an error message slog.Error("this is an error log") }
Output: time=2023-09-17T20:01:50.364+05:30 level=DEBUG msg="this is a debug log" logger=debugTextLogHandler time=2023-09-17T20:01:50.364+05:30 level=INFO msg="this is an info log" logger=debugTextLogHandler {"time":"2023-09-17T20:01:50.364653675+05:30","level":"INFO","msg":"this is an info log","logger":"infoJSONLogHandler"} time=2023-09-17T20:01:50.364+05:30 level=WARN msg="this is a warning log" logger=debugTextLogHandler {"time":"2023-09-17T20:01:50.364658189+05:30","level":"WARN","msg":"this is a warning log","logger":"infoJSONLogHandler"} time=2023-09-17T20:01:50.364+05:30 level=WARN source=/home/ksdfg/code/loggy/combined_handler_test.go:56 msg="this is a warning log" logger=warnTextLogHandler time=2023-09-17T20:01:50.364+05:30 level=ERROR msg="this is an error log" logger=debugTextLogHandler {"time":"2023-09-17T20:01:50.36466392+05:30","level":"ERROR","msg":"this is an error log","logger":"infoJSONLogHandler"} time=2023-09-17T20:01:50.364+05:30 level=ERROR source=/home/ksdfg/code/loggy/combined_handler_test.go:58 msg="this is an error log" logger=warnTextLogHandler {"time":"2023-09-17T20:01:50.36466392+05:30","level":"ERROR","source":{"function":"github.com/ksdfg/loggy_test.TestNewCombinedHandler","file":"/home/ksdfg/code/loggy/combined_handler_test.go","line":58},"msg":"this is an error log","logger":"errorJSONLogHandler"}
func NewConsoleLogHandler ¶
func NewConsoleLogHandler(options ...ConsoleLogWriterOpts) slog.Handler
NewConsoleLogHandler initializes a new stderr log writer based on the given options. It returns a slog.Handler that uses the log writer to write log messages to stderr.
Example ¶
package main import ( "log/slog" "github.com/ksdfg/loggy" ) func main() { // Create a new slog.Handler that writes JSON text logs with source info to stderr opts := loggy.ConsoleLogWriterOpts{ JSON: true, LogToStdout: true, HandlerOptions: slog.HandlerOptions{AddSource: true, Level: slog.LevelDebug}, } handler := loggy.NewConsoleLogHandler(opts) // Create a new logger with the above handler, and set it as the default logger := slog.New(handler) slog.SetDefault(logger) // Log a debug message slog.Debug("this is a debug log") // Log an info message slog.Info("this is an info log") // Log a warning message slog.Warn("this is a warning log") // Log an error message slog.Error("this is an error log") }
Output: {"time":"2023-09-17T17:44:01.603539649+05:30","level":"DEBUG","source":{"function":"github.com/ksdfg/loggy_test.ExampleNewStderrLogHandler","file":"/home/ksdfg/code/loggy/example_test.go","line":63},"msg":"this is a debug log"} {"time":"2023-09-17T17:44:01.60354488+05:30","level":"INFO","source":{"function":"github.com/ksdfg/loggy_test.ExampleNewStderrLogHandler","file":"/home/ksdfg/code/loggy/example_test.go","line":65},"msg":"this is an info log"} {"time":"2023-09-17T17:44:01.603548573+05:30","level":"WARN","source":{"function":"github.com/ksdfg/loggy_test.ExampleNewStderrLogHandler","file":"/home/ksdfg/code/loggy/example_test.go","line":67},"msg":"this is a warning log"} {"time":"2023-09-17T17:44:01.603551182+05:30","level":"ERROR","source":{"function":"github.com/ksdfg/loggy_test.ExampleNewStderrLogHandler","file":"/home/ksdfg/code/loggy/example_test.go","line":69},"msg":"this is an error log"}
Types ¶
type CombinedHandler ¶
type CombinedHandler struct {
// contains filtered or unexported fields
}
CombinedHandler is a handler that delegates to multiple other handlers.
func (CombinedHandler) Enabled ¶
Enabled reports whether the CombinedHandler handles records at the given level.
The CombinedHandler ignores records whose level is lower. This method is called early, before any arguments are processed, to save effort if the log event should be discarded.
If called from a Logger method, the first argument is the context passed to that method, or context.Background() if nil was passed or the method does not take a context. The context is passed so Enabled can use its values to make a decision.
Parameters:
- ctx: The context passed to the method, or context.Background() if nil.
- level: The log level to check.
Returns:
- enabled: A boolean indicating whether the CombinedHandler is enabled for the given level.
func (CombinedHandler) Handle ¶
Handle handles the Record. It will only be called when Enabled returns true. The Context argument is as for Enabled. It is present solely to provide Handlers access to the context's values. Canceling the context should not affect record processing. (Among other things, log messages may be necessary to debug a cancellation-related problem.)
func (CombinedHandler) WithAttrs ¶
func (h CombinedHandler) WithAttrs(attrs []slog.Attr) slog.Handler
WithAttrs returns a new CombinedHandler whose child handlers' attributes consist of both the child handlers' attributes and the arguments. The CombinedHandler owns the slice: it may retain, modify or discard it.
func (CombinedHandler) WithGroup ¶
func (h CombinedHandler) WithGroup(name string) slog.Handler
WithGroup returns a new CombinedHandler with the given group appended to the child handlers' existing groups. The keys of all subsequent attributes, whether added by With or in a Record, should be qualified by the sequence of group names.
If the name is empty, WithGroup returns the receiver.
type ConsoleLogWriter ¶
type ConsoleLogWriter struct {
// contains filtered or unexported fields
}
ConsoleLogWriter represents a logger that writes log messages to stderr.
It implements the `slog.Writer` interface, allowing it to be used as a logger handler.
func (ConsoleLogWriter) Write ¶
func (w ConsoleLogWriter) Write(p []byte) (n int, err error)
Write writes the log message to the standard error output.
If noColour is false, it colorizes the log message according to the log levels: red for error, yellow for warning, and blue for info.
Parameters: - p: The byte slice containing the log message.
Returns: - n: The number of bytes written. - err: An error, if any occurred.
type ConsoleLogWriterOpts ¶
type ConsoleLogWriterOpts struct { // JSON specifies whether to use JSON format for log messages. JSON bool // LogToStdout specifies whether to write log messages to stdout. By default, // the ConsoleLogWriter will write logs to stderr. LogToStdout bool // HandlerOptions contains additional options for the logger handler. HandlerOptions slog.HandlerOptions }
ConsoleLogWriterOpts represents the options for configuring the behavior of the `ConsoleLogWriter`.