Esempio n. 1
0
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"))

}
Esempio n. 2
0
File: log.go Progetto: kulasama/gopm
func Trace(format string, args ...interface{}) {
	if PureMode {
		trace(format, args...)
		return
	}

	if !Verbose {
		return
	}
	fmt.Printf("gopm %s %s\n", brush.Blue("TRAC"),
		fmt.Sprintf(format, args...))
}
Esempio n. 3
0
func makeLogMux(conf *Config) (*logMux, error) {

	fileVerb, err := getVerbosityLevel(conf.LogFileVerbosity)
	if err != nil {
		return nil, fmt.Errorf("log file verbosity, %v", err)
	}
	consoleVerb, err := getVerbosityLevel(conf.ConsoleVerbosity)
	if err != nil {
		return nil, fmt.Errorf("console verbosity, %v", err)
	}

	filename := conf.LogFilename

	file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0640)
	if os.IsNotExist(err) {
		file, err = os.Create(filename)
	}
	if err != nil {
		return nil, fmt.Errorf("opening log file %s, %v", filename, err)
	}

	debugPfx := fmt.Sprintf("%s%s%s ",
		brush.DarkGray("["),
		brush.Blue("DEBUG"),
		brush.DarkGray("]"))

	watchPfx := fmt.Sprintf("%s%s%s ",
		brush.DarkGray("["),
		brush.DarkCyan("WATCH"),
		brush.DarkGray("]"))

	infoPfx := fmt.Sprintf(" %s%s%s ",
		brush.DarkGray("["),
		brush.Green("INFO"),
		brush.DarkGray("]"))

	warnPfx := fmt.Sprintf(" %s%s%s ",
		brush.DarkGray("["),
		brush.Yellow("WARN"),
		brush.DarkGray("]"))

	errPfx := fmt.Sprintf("%s%s%s ",
		brush.DarkGray("["),
		brush.Red("ERROR"),
		brush.DarkGray("]"))

	return &logMux{
		logFile:        file,
		debugFile:      log.New(file, "[DEBUG] ", log.LstdFlags),
		watchFile:      log.New(file, "[WATCH] ", log.LstdFlags),
		infoFile:       log.New(file, " [INFO] ", log.LstdFlags),
		warnFile:       log.New(file, " [WARN] ", log.LstdFlags),
		errorFile:      log.New(file, "[ERROR] ", log.LstdFlags),
		debugConsole:   log.New(os.Stdout, debugPfx, log.LstdFlags),
		watchConsole:   log.New(os.Stdout, watchPfx, log.LstdFlags),
		infoConsole:    log.New(os.Stdout, infoPfx, log.LstdFlags),
		warnConsole:    log.New(os.Stdout, warnPfx, log.LstdFlags),
		errorConsole:   log.New(os.Stderr, errPfx, log.LstdFlags),
		fileVerbose:    fileVerb,
		consoleVerbose: consoleVerb,
	}, nil
}