-
-
Save aymanbagabas/64f2c21ad99ce18ff13600317661de56 to your computer and use it in GitHub Desktop.
Logger with custom level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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