Skip to content

Instantly share code, notes, and snippets.

@aymanbagabas
Last active March 15, 2024 17:53
Show Gist options
  • Save aymanbagabas/64f2c21ad99ce18ff13600317661de56 to your computer and use it in GitHub Desktop.
Save aymanbagabas/64f2c21ad99ce18ff13600317661de56 to your computer and use it in GitHub Desktop.
Logger with custom level
package main
import (
"os"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
)
const SuccessLevel = log.InfoLevel + 1
type Logger struct {
*log.Logger
}
func (l *Logger) Success(msg string, args ...any) {
l.Log(SuccessLevel, msg, args...)
}
func NewLogger() *Logger {
l := new(Logger)
logger := log.New(os.Stderr)
styles := log.DefaultStyles()
styles.Levels[SuccessLevel] = lipgloss.NewStyle().
SetString("SUCCESS").
Bold(true).
MaxWidth(4).
Foreground(lipgloss.Color("86"))
logger.SetStyles(styles)
l.Logger = logger
return l
}
func main() {
logger := NewLogger()
logger.Success("blah")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment