mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
CLI: Use colorized console output
This extracts some of the colored logging functionality into some convenience functions to log directly to the console (stdout) w/o the usual logging prefixes and flags. It's intended for console messages when using grafana commands.
This commit is contained in:
@@ -47,8 +47,7 @@ func listAccounts(c *cli.Context) {
|
|||||||
|
|
||||||
accountsQuery := m.GetAccountsQuery{}
|
accountsQuery := m.GetAccountsQuery{}
|
||||||
if err := bus.Dispatch(&accountsQuery); err != nil {
|
if err := bus.Dispatch(&accountsQuery); err != nil {
|
||||||
log.Error(3, "Failed to find accounts", err)
|
log.ConsoleFatalf("Failed to find accounts: %s", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
w := tabwriter.NewWriter(os.Stdout, 20, 1, 4, ' ', 0)
|
w := tabwriter.NewWriter(os.Stdout, 20, 1, 4, ' ', 0)
|
||||||
@@ -66,8 +65,7 @@ func createAccount(c *cli.Context) {
|
|||||||
sqlstore.EnsureAdminUser()
|
sqlstore.EnsureAdminUser()
|
||||||
|
|
||||||
if !c.Args().Present() {
|
if !c.Args().Present() {
|
||||||
fmt.Printf("Account name arg is required\n")
|
log.ConsoleFatal("Account name arg is required")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
name := c.Args().First()
|
name := c.Args().First()
|
||||||
@@ -75,16 +73,15 @@ func createAccount(c *cli.Context) {
|
|||||||
adminQuery := m.GetUserByLoginQuery{LoginOrEmail: setting.AdminUser}
|
adminQuery := m.GetUserByLoginQuery{LoginOrEmail: setting.AdminUser}
|
||||||
|
|
||||||
if err := bus.Dispatch(&adminQuery); err == m.ErrUserNotFound {
|
if err := bus.Dispatch(&adminQuery); err == m.ErrUserNotFound {
|
||||||
log.Error(3, "Failed to find default admin user", err)
|
log.ConsoleFatalf("Failed to find default admin user: %s", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
adminUser := adminQuery.Result
|
adminUser := adminQuery.Result
|
||||||
|
|
||||||
cmd := m.CreateAccountCommand{Name: name, UserId: adminUser.Id}
|
cmd := m.CreateAccountCommand{Name: name, UserId: adminUser.Id}
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
log.Error(3, "Failed to create account", err)
|
log.ConsoleFatalf("Failed to create account: %s", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
fmt.Printf("Account %s created for admin user %s\n", name, adminUser.Email)
|
|
||||||
|
log.ConsoleInfof("Account %s created for admin user %s\n", name, adminUser.Email)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package log
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -21,15 +22,26 @@ func NewBrush(color string) Brush {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var colors = []Brush{
|
var (
|
||||||
NewBrush("1;36"), // Trace cyan
|
Red = NewBrush("1;31")
|
||||||
NewBrush("1;34"), // Debug blue
|
Purple = NewBrush("1;35")
|
||||||
NewBrush("1;32"), // Info green
|
Yellow = NewBrush("1;33")
|
||||||
NewBrush("1;33"), // Warn yellow
|
Green = NewBrush("1;32")
|
||||||
NewBrush("1;31"), // Error red
|
Blue = NewBrush("1;34")
|
||||||
NewBrush("1;35"), // Critical purple
|
Cyan = NewBrush("1;36")
|
||||||
NewBrush("1;31"), // Fatal red
|
|
||||||
}
|
colors = []Brush{
|
||||||
|
Cyan, // Trace cyan
|
||||||
|
Blue, // Debug blue
|
||||||
|
Green, // Info green
|
||||||
|
Yellow, // Warn yellow
|
||||||
|
Red, // Error red
|
||||||
|
Purple, // Critical purple
|
||||||
|
Red, // Fatal red
|
||||||
|
}
|
||||||
|
consoleWriter = &ConsoleWriter{lg: log.New(os.Stdout, "", 0),
|
||||||
|
Level: TRACE}
|
||||||
|
)
|
||||||
|
|
||||||
// ConsoleWriter implements LoggerInterface and writes messages to terminal.
|
// ConsoleWriter implements LoggerInterface and writes messages to terminal.
|
||||||
type ConsoleWriter struct {
|
type ConsoleWriter struct {
|
||||||
@@ -68,6 +80,76 @@ func (_ *ConsoleWriter) Flush() {
|
|||||||
func (_ *ConsoleWriter) Destroy() {
|
func (_ *ConsoleWriter) Destroy() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printConsole(level int, msg string) {
|
||||||
|
consoleWriter.WriteMsg(msg, 0, level)
|
||||||
|
}
|
||||||
|
|
||||||
|
func printfConsole(level int, format string, v ...interface{}) {
|
||||||
|
consoleWriter.WriteMsg(fmt.Sprintf(format, v...), 0, level)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleTrace prints to stdout using TRACE colors
|
||||||
|
func ConsoleTrace(s string) {
|
||||||
|
printConsole(TRACE, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleTracef prints a formatted string to stdout using TRACE colors
|
||||||
|
func ConsoleTracef(format string, v ...interface{}) {
|
||||||
|
printfConsole(TRACE, format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleDebug prints to stdout using DEBUG colors
|
||||||
|
func ConsoleDebug(s string) {
|
||||||
|
printConsole(DEBUG, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleDebugf prints a formatted string to stdout using DEBUG colors
|
||||||
|
func ConsoleDebugf(format string, v ...interface{}) {
|
||||||
|
printfConsole(DEBUG, format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleInfo prints to stdout using INFO colors
|
||||||
|
func ConsoleInfo(s string) {
|
||||||
|
printConsole(INFO, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleInfof prints a formatted string to stdout using INFO colors
|
||||||
|
func ConsoleInfof(format string, v ...interface{}) {
|
||||||
|
printfConsole(INFO, format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleWarn prints to stdout using WARN colors
|
||||||
|
func ConsoleWarn(s string) {
|
||||||
|
printConsole(WARN, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleWarnf prints a formatted string to stdout using WARN colors
|
||||||
|
func ConsoleWarnf(format string, v ...interface{}) {
|
||||||
|
printfConsole(WARN, format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleError prints to stdout using ERROR colors
|
||||||
|
func ConsoleError(s string) {
|
||||||
|
printConsole(ERROR, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleErrorf prints a formatted string to stdout using ERROR colors
|
||||||
|
func ConsoleErrorf(format string, v ...interface{}) {
|
||||||
|
printfConsole(ERROR, format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleFatal prints to stdout using FATAL colors
|
||||||
|
func ConsoleFatal(s string) {
|
||||||
|
printConsole(FATAL, s)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleFatalf prints a formatted string to stdout using FATAL colors
|
||||||
|
func ConsoleFatalf(format string, v ...interface{}) {
|
||||||
|
printfConsole(FATAL, format, v...)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Register("console", NewConsole)
|
Register("console", NewConsole)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user