Skip to content

Commit

Permalink
Add config / flag for color.
Browse files Browse the repository at this point in the history
Signed-off-by: SuperQ <superq@gmail.com>
  • Loading branch information
SuperQ committed Feb 25, 2023
1 parent bd29a9c commit 9227990
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
10 changes: 10 additions & 0 deletions promlog/flag/flag.go
Expand Up @@ -18,6 +18,12 @@ import (
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

// ColorFlagName is the canonical flag name to enable color logging.
const ColorFlagName = "log.color"

// ColorFlagHelp is the help description for the log.color flag.
const ColorFlagHelp = "Enable colors in the console log. Use --no-log.color to disable."

// LevelFlagName is the canonical flag name to configure the allowed log level
// within Prometheus projects.
const LevelFlagName = "log.level"
Expand All @@ -35,6 +41,10 @@ const FormatFlagHelp = "Output format of log messages. One of: [logfmt, json]"
// AddFlags adds the flags used by this package to the Kingpin application.
// To use the default Kingpin application, call AddFlags(kingpin.CommandLine)
func AddFlags(a *kingpin.Application, config *promlog.Config) {
config.Color = &promlog.Color{}
a.Flag(ColorFlagName, ColorFlagHelp).
Default("true").SetValue(config.Color)

config.Level = &promlog.AllowedLevel{}
a.Flag(LevelFlagName, LevelFlagHelp).
Default("info").SetValue(config.Level)
Expand Down
59 changes: 54 additions & 5 deletions promlog/log.go
Expand Up @@ -19,6 +19,7 @@ package promlog
import (
"fmt"
"os"
"strconv"
"sync"
"time"

Expand All @@ -37,6 +38,33 @@ var (
)
)

// Color is a settable boolean for controlling color output.
type Color struct {
s string
enabled bool
}

func (c *Color) Set(s string) error {
switch s {
case "true":
c.enabled = true
case "false":
c.enabled = false
default:
return fmt.Errorf("unrecognized boolean %q", s)
}
c.s = s
return nil
}

func (c *Color) Enabled() bool {
return c.enabled
}

func (c *Color) String() string {
return strconv.FormatBool(c.enabled)
}

// AllowedLevel is a settable identifier for the minimum level a log entry
// must be have.
type AllowedLevel struct {
Expand Down Expand Up @@ -124,21 +152,30 @@ func (f *AllowedFormat) Set(s string) error {

// Config is a struct containing configurable settings for the logger
type Config struct {
Color *Color
Level *AllowedLevel
Format *AllowedFormat
}

// New returns a new leveled oklog logger. Each logged line will be annotated
// with a timestamp. The output always goes to stderr.
func New(config *Config) log.Logger {
if config.Color == nil {
config.Color = &Color{}
config.Color.Set("true")
}
var l log.Logger
syncWriter := log.NewSyncWriter(os.Stderr)
if config.Format != nil && config.Format.s == "json" {
l = log.NewJSONLogger(syncWriter)
} else {
// Returns a new logger with color logging capabilites if we're in a terminal, otherwise we
// just get a standard go-kit logger.
l = term.NewLogger(syncWriter, log.NewLogfmtLogger, colorFn)
if config.Color.Enabled() {
// Returns a new logger with color logging capabilites if we're in a terminal, otherwise we
// just get a standard go-kit logger.
l = term.NewLogger(syncWriter, log.NewLogfmtLogger, colorFn)
} else {
l = log.NewJSONLogger(syncWriter)
}
}

if config.Level != nil {
Expand All @@ -154,11 +191,23 @@ func New(config *Config) log.Logger {
// with a timestamp. The output always goes to stderr. Some properties can be
// changed, like the level.
func NewDynamic(config *Config) *logger {
if config.Color == nil {
config.Color = &Color{}
config.Color.Set("true")
}
var l log.Logger
syncWriter := log.NewSyncWriter(os.Stderr)

if config.Format != nil && config.Format.s == "json" {
l = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
l = log.NewJSONLogger(syncWriter)
} else {
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
if config.Color.Enabled() {
// Returns a new logger with color logging capabilites if we're in a terminal, otherwise we
// just get a standard go-kit logger.
l = term.NewLogger(syncWriter, log.NewLogfmtLogger, colorFn)
} else {
l = log.NewJSONLogger(syncWriter)
}
}

lo := &logger{
Expand Down

0 comments on commit 9227990

Please sign in to comment.