Documentation
¶
Overview ¶
Package logger provides a logging interface and implementations using the charmbracelet/lipgloss and charmbracelet/log packages. It includes a configurable logger (CharmLogger) and a test logger (NullLogger) for capturing log output in tests.
Usage:
Basic usage of CharmLogger:
package main import ( "os" "github.com/yourusername/go-common/pkg/tui/logger/charm" ) func main() { log := logger.New(os.Stdout, logger.LogLevelInfo) log.Info("This is an info message") log.Debug("This is a debug message") // This won't be printed because the level is set to Info }
Using NullLogger for testing:
package main import ( "testing" "github.com/yourusername/go-common/pkg/tui/logger/charm" ) func TestLogging(t *testing.T) { log := logger.NewTestLogger(t) log.Info("This is an info message") log.Debug("This is a debug message") // The log output can be accessed via log.LogOutput.String() if !strings.Contains(log.LogOutput.String(), "This is an info message") { t.Error("Expected info message to be logged") } }
Index ¶
- Constants
- type CharmLogger
- func (l *CharmLogger) Debug(message string, keyvals ...any)
- func (l *CharmLogger) Debugf(format string, keyvals ...any)
- func (l *CharmLogger) Error(message string, keyvals ...any)
- func (l *CharmLogger) Fatal(message string, keyvals ...any)
- func (l *CharmLogger) Info(message string, keyvals ...any)
- func (l *CharmLogger) Infof(format string, keyvals ...any)
- func (l *CharmLogger) IsDebug() bool
- func (l *CharmLogger) IsError() bool
- func (l *CharmLogger) IsInfo() bool
- func (l *CharmLogger) IsTrace() bool
- func (l *CharmLogger) IsWarn() bool
- func (l *CharmLogger) Level() string
- func (l *CharmLogger) Output() io.Writer
- func (l *CharmLogger) Print(message string, keyvals ...any)
- func (l *CharmLogger) SetInteractive(interactive string, isTerminal bool)
- func (l *CharmLogger) SetLevel(level string) error
- func (l *CharmLogger) SetOutput(w io.Writer)
- func (l *CharmLogger) StandardWriter() io.Writer
- func (l *CharmLogger) Trace(message string, keyvals ...any)
- func (l *CharmLogger) Warn(message string, keyvals ...any)
- func (l *CharmLogger) WithPrefix(prefix string) Logger
- type Logger
- type NullLogger
Constants ¶
const ( LogLevelInfo = "info" LogLevelDebug = "debug" LogLevelTrace = "trace" LogLevelWarn = "warn" LogLevelError = "error" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CharmLogger ¶
type CharmLogger struct {
// contains filtered or unexported fields
}
CharmLogger is a charms/lipgloss based logger
func (*CharmLogger) Debug ¶
func (l *CharmLogger) Debug(message string, keyvals ...any)
Debug logs to debug level
func (*CharmLogger) Debugf ¶
func (l *CharmLogger) Debugf(format string, keyvals ...any)
Debugf logs formatted debug level
func (*CharmLogger) Error ¶
func (l *CharmLogger) Error(message string, keyvals ...any)
Error logs to error level
func (*CharmLogger) Fatal ¶
func (l *CharmLogger) Fatal(message string, keyvals ...any)
Fatal logs to fatal level
func (*CharmLogger) Info ¶
func (l *CharmLogger) Info(message string, keyvals ...any)
Info logs to info level
func (*CharmLogger) Infof ¶
func (l *CharmLogger) Infof(format string, keyvals ...any)
Infof logs formatted info level
func (*CharmLogger) IsDebug ¶
func (l *CharmLogger) IsDebug() bool
IsDebug returns true if the logger is set to debug level
func (*CharmLogger) IsError ¶
func (l *CharmLogger) IsError() bool
IsError returns true if the logger is set to error level
func (*CharmLogger) IsInfo ¶
func (l *CharmLogger) IsInfo() bool
IsLevel returns true if the logger is set to the given level
func (*CharmLogger) IsTrace ¶
func (l *CharmLogger) IsTrace() bool
IsTrace returns true if the logger is set to trace level
func (*CharmLogger) IsWarn ¶
func (l *CharmLogger) IsWarn() bool
IsWarn returns true if the logger is set to warn level
func (*CharmLogger) Output ¶
func (l *CharmLogger) Output() io.Writer
Output returns the logger output
func (*CharmLogger) Print ¶
func (l *CharmLogger) Print(message string, keyvals ...any)
Print prints a log message
func (*CharmLogger) SetInteractive ¶
func (l *CharmLogger) SetInteractive(interactive string, isTerminal bool)
SetInteractive sets the logger to use fancy styles if isTerminal is true
func (*CharmLogger) SetLevel ¶
func (l *CharmLogger) SetLevel(level string) error
SetLevel sets the logger level
func (*CharmLogger) SetOutput ¶
func (l *CharmLogger) SetOutput(w io.Writer)
SetOutput sets the logger output
func (*CharmLogger) StandardWriter ¶
func (l *CharmLogger) StandardWriter() io.Writer
StadardWriter returns a writer that can be used to write logs
func (*CharmLogger) Trace ¶
func (l *CharmLogger) Trace(message string, keyvals ...any)
Trace logs to trace level
func (*CharmLogger) Warn ¶
func (l *CharmLogger) Warn(message string, keyvals ...any)
Warn logs to warn level
func (*CharmLogger) WithPrefix ¶
func (l *CharmLogger) WithPrefix(prefix string) Logger
WithPrefix returns a new logger with the given prefix
type Logger ¶
type Logger interface { // Set the logger level SetLevel(level string) error // Level returns the logger level Level() string // Set the logger output SetOutput(w io.Writer) // Output returns the logger output Output() io.Writer // Print prints a log message Print(message string, keyvals ...any) // Info logs to info level Info(message string, keyvals ...any) // Debug logs to debug level Debug(message string, keyvals ...any) // Error logs to error level Error(message string, keyvals ...any) // Fatal logs to fatal level Fatal(message string, keyvals ...any) // Warn logs to warn level Warn(message string, keyvals ...any) // Trace logs to trace level Trace(message string, keyvals ...any) // Infof logs formatted info level Infof(format string, keyvals ...any) // Debugf logs formatted debug level Debugf(format string, keyvals ...any) // StandardWriter returns a writer that can be used to write logs StandardWriter() io.Writer // IsLevel returns true if the logger is set to the given level IsInfo() bool // IsDebug returns true if the logger is set to debug level IsDebug() bool // IsError returns true if the logger is set to error level IsError() bool // IsTrace returns true if the logger is set to trace level IsTrace() bool // IsWarn returns true if the logger is set to warn level IsWarn() bool // WithPrefix returns a new logger with the given prefix WithPrefix(string) Logger // SetInteractive sets the logger to use fancy styles if isTerminal is true SetInteractive(string, bool) }
Logger defines an abstract logger that can be used to log to the output
func NewTestLogger ¶
Logger that sends all output to a string buffer the captured log output can be retrieved by accessing the string buffer at LogOutput In the instance of a test failure, the log output is written to StdOut
type NullLogger ¶
NullLogger is a logger implementation that captures log output in a string builder. It embeds the Logger interface and provides a LogOutput field to access the captured logs. Useful for tests