func main() { // Default Brush are available for your convenience. You can invoke // them directly fmt.Printf("This is %s\n", brush.Red("red")) // or you can create new ones! weird := color.NewBrush(color.PurplePaint, color.CyanPaint) fmt.Printf("This color is %s\n", weird("weird")) // Create a Style, which has convenience methods redBg := color.NewStyle(color.RedPaint, color.YellowPaint) // Style.WithForeground or WithBackground returns a new Style, with the applied // Paint. Styles are immutable so the original one is left unchanged greenFg := redBg.WithForeground(color.GreenPaint) // Style.Brush gives you a Brush that you can invoke directly to colorize strings. green := greenFg.Brush() fmt.Printf("This is %s but not really\n", green("kind of green")) // You can use it with all sorts of things sout := log.New(os.Stdout, "["+brush.Green("OK").String()+"]\t", log.LstdFlags) serr := log.New(os.Stderr, "["+brush.Red("OMG").String()+"]\t", log.LstdFlags) sout.Printf("Everything was going %s until...", brush.Cyan("fine")) serr.Printf("%s killed %s !!!", brush.Red("Locke"), brush.Blue("Jacob")) }
func (l *Logger) write(level Level, format string, a ...interface{}) (n int, err error) { if level < l.level { return } var levelName string = levels[int(level)] var sep = " " var prefix, outstr = l.prefix, "" if l.flags&Fdatetime != 0 { now := time.Now() layout := "" if l.flags&Fdate != 0 { layout += "2006/01/02" } if l.flags&Ftime != 0 { layout += " 15:04:05" } layout = strings.TrimSpace(layout) prefix += now.Format(layout) } if l.flags&Fshortfile != 0 { // Retrieve the stack infos _, file, line, ok := runtime.Caller(2) if !ok { file = "<unknown>" line = -1 } else { file = file[strings.LastIndex(file, "/")+1:] } prefix = fmt.Sprintf("%s %s:%d", prefix, file, line) } outstr += levelName if format == "" { for _, i := range a { outstr += sep + fmt.Sprintf("%v", i) } } else { outstr = outstr + sep + fmt.Sprintf(format, a...) } if !strings.HasSuffix(outstr, "\n") { outstr += "\n" } if l.colorEnable && l.flags&Fcolor != 0 { brush := color.NewBrush("", colors[int(level)]) outstr = brush(outstr) } mu.Lock() defer mu.Unlock() return l.writer.Write([]byte(prefix + sep + outstr)) }
func (y Yellow) String() string { return color.NewBrush("", color.YellowPaint)(string(y)) }
func (d DarkBlue) String() string { return color.NewBrush("", color.DarkBluePaint)(string(d)) }
func (p Purple) String() string { return color.NewBrush("", color.PurplePaint)(string(p)) }
func (r Red) String() string { return color.NewBrush("", color.RedPaint)(string(r)) }
func (c Cyan) String() string { return color.NewBrush("", color.CyanPaint)(string(c)) }
func (g Green) String() string { return color.NewBrush("", color.GreenPaint)(string(g)) }
func (l LightGray) String() string { return color.NewBrush("", color.LightGrayPaint)(string(l)) }
func (b Blue) String() string { return color.NewBrush("", color.BluePaint)(string(b)) }
func (w White) String() string { return color.NewBrush(color.DarkGrayPaint, color.WhitePaint)(string(w)) }
func (b Black) String() string { return color.NewBrush(color.WhitePaint, color.BlackPaint)(string(b)) }
func (d DarkYellow) String() string { return color.NewBrush("", color.DarkYellowPaint)(string(d)) }
func (d DarkRed) String() string { return color.NewBrush("", color.DarkRedPaint)(string(d)) }
func (d DarkGreen) String() string { return color.NewBrush("", color.DarkGreenPaint)(string(d)) }