-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package log | ||
|
||
import ( | ||
"math" | ||
"os" | ||
"strings" | ||
|
||
"github.com/charmbracelet/log" | ||
) | ||
|
||
// Run is the command-line interface for logging text. | ||
func (o Options) Run() error { | ||
l := log.New(os.Stderr) | ||
|
||
l.SetPrefix(o.Prefix) | ||
l.SetLevel(-math.MaxInt32) // log all levels | ||
l.SetReportTimestamp(o.Time) | ||
l.SetTimeFormat(o.TimeFormat) | ||
|
||
st := log.DefaultStyles() | ||
st.Levels[log.DebugLevel] = o.LevelDebugStyle.ToLipgloss(). | ||
Inline(true). | ||
SetString(strings.ToUpper(log.DebugLevel.String())) | ||
st.Levels[log.InfoLevel] = o.LevelInfoStyle.ToLipgloss(). | ||
Inline(true). | ||
SetString(strings.ToUpper(log.InfoLevel.String())) | ||
st.Levels[log.WarnLevel] = o.LevelWarnStyle.ToLipgloss(). | ||
Inline(true). | ||
SetString(strings.ToUpper(log.WarnLevel.String())) | ||
st.Levels[log.ErrorLevel] = o.LevelErrorStyle.ToLipgloss(). | ||
Inline(true). | ||
SetString(strings.ToUpper(log.ErrorLevel.String())) | ||
st.Levels[log.FatalLevel] = o.LevelFatalStyle.ToLipgloss(). | ||
Inline(true). | ||
SetString(strings.ToUpper(log.FatalLevel.String())) | ||
st.Timestamp = o.TimeStyle.ToLipgloss(). | ||
Inline(true) | ||
st.Prefix = o.PrefixStyle.ToLipgloss(). | ||
Inline(true) | ||
st.Message = o.MessageStyle.ToLipgloss(). | ||
Inline(true) | ||
st.Key = o.KeyStyle.ToLipgloss(). | ||
Inline(true) | ||
st.Value = o.ValueStyle.ToLipgloss(). | ||
Inline(true) | ||
st.Separator = o.SeparatorStyle.ToLipgloss(). | ||
Inline(true) | ||
|
||
l.SetStyles(st) | ||
|
||
if o.File != "" { | ||
f, err := os.OpenFile(o.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) | ||
Check failure on line 52 in log/command.go GitHub Actions / lint-soft
|
||
if err != nil { | ||
return err | ||
Check failure on line 54 in log/command.go GitHub Actions / lint-soft
|
||
} | ||
|
||
defer f.Close() // nolint: errcheck | ||
Check failure on line 57 in log/command.go GitHub Actions / lint-soft
|
||
l.SetOutput(f) | ||
} | ||
|
||
switch o.Formatter { | ||
case "json": | ||
l.SetFormatter(log.JSONFormatter) | ||
case "logfmt": | ||
l.SetFormatter(log.LogfmtFormatter) | ||
case "text": | ||
l.SetFormatter(log.TextFormatter) | ||
} | ||
|
||
var arg0 string | ||
var args []interface{} | ||
if len(o.Text) > 0 { | ||
arg0 = o.Text[0] | ||
} | ||
|
||
if len(o.Text) > 1 { | ||
args = make([]interface{}, len(o.Text[1:])) | ||
for i, arg := range o.Text[1:] { | ||
args[i] = arg | ||
} | ||
} | ||
|
||
switch o.Level { | ||
case "none": | ||
if o.Format { | ||
l.Printf(arg0, args...) | ||
} else if o.Structured { | ||
l.Print(arg0, args...) | ||
} else { | ||
l.Print(strings.Join(o.Text, " ")) | ||
} | ||
case "debug": | ||
if o.Format { | ||
l.Debugf(arg0, args...) | ||
} else if o.Structured { | ||
l.Debug(arg0, args...) | ||
} else { | ||
l.Debug(strings.Join(o.Text, " ")) | ||
} | ||
case "info": | ||
if o.Format { | ||
l.Infof(arg0, args...) | ||
} else if o.Structured { | ||
l.Info(arg0, args...) | ||
} else { | ||
l.Info(strings.Join(o.Text, " ")) | ||
} | ||
case "warn": | ||
if o.Format { | ||
l.Warnf(arg0, args...) | ||
} else if o.Structured { | ||
l.Warn(arg0, args...) | ||
} else { | ||
l.Warn(strings.Join(o.Text, " ")) | ||
} | ||
case "error": | ||
if o.Format { | ||
l.Errorf(arg0, args...) | ||
} else if o.Structured { | ||
l.Error(arg0, args...) | ||
} else { | ||
l.Error(strings.Join(o.Text, " ")) | ||
} | ||
case "fatal": | ||
if o.Format { | ||
l.Fatalf(arg0, args...) | ||
} else if o.Structured { | ||
l.Fatal(arg0, args...) | ||
} else { | ||
l.Fatal(strings.Join(o.Text, " ")) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package log | ||
|
||
import ( | ||
"github.com/alecthomas/kong" | ||
"github.com/charmbracelet/gum/style" | ||
) | ||
|
||
// Options is the set of options that can configure a join. | ||
type Options struct { | ||
Text []string `arg:"" help:"Text to log"` | ||
|
||
File string `short:"o" help:"Log to file"` | ||
Format bool `short:"f" help:"Format message using printf" xor:"format,structured"` | ||
Formatter string `help:"The log formatter to use" enum:"json,logfmt,text" default:"text"` | ||
Level string `help:"The log level to use" enum:"none,debug,info,warn,error,fatal" default:"none"` | ||
Prefix string `help:"Prefix to print before the message"` | ||
Structured bool `short:"s" help:"Use structured logging" xor:"format,structured"` | ||
Time bool `help:"Whether to print the time"` | ||
TimeFormat string `help:"The time format to use" default:"2006/01/02 15:04:05"` | ||
|
||
LevelDebugStyle style.Styles `embed:"" prefix:"level.debug." help:"The style of the debug level" set:"defaultBold=true" set:"defaultForeground=63" envprefix:"GUM_LOG_LEVEL_DEBUG_"` | ||
Check failure on line 21 in log/options.go GitHub Actions / lint
|
||
LevelInfoStyle style.Styles `embed:"" prefix:"level.info." help:"The style of the info level" set:"defaultBold=true" set:"defaultForeground=83" envprefix:"GUM_LOG_LEVEL_INFO_"` | ||
Check failure on line 22 in log/options.go GitHub Actions / lint
|
||
LevelWarnStyle style.Styles `embed:"" prefix:"level.warn." help:"The style of the warn level" set:"defaultBold=true" set:"defaultForeground=192" envprefix:"GUM_LOG_LEVEL_WARN_"` | ||
Check failure on line 23 in log/options.go GitHub Actions / lint
|
||
LevelErrorStyle style.Styles `embed:"" prefix:"level.error." help:"The style of the error level" set:"defaultBold=true" set:"defaultForeground=204" envprefix:"GUM_LOG_LEVEL_ERROR_"` | ||
Check failure on line 24 in log/options.go GitHub Actions / lint
|
||
LevelFatalStyle style.Styles `embed:"" prefix:"level.fatal." help:"The style of the fatal level" set:"defaultBold=true" set:"defaultForeground=134" envprefix:"GUM_LOG_LEVEL_FATAL_"` | ||
Check failure on line 25 in log/options.go GitHub Actions / lint
|
||
TimeStyle style.Styles `embed:"" prefix:"time." help:"The style of the time" envprefix:"GUM_LOG_TIME_"` | ||
PrefixStyle style.Styles `embed:"" prefix:"prefix." help:"The style of the prefix" set:"defaultBold=true" set:"defaultFaint=true" envprefix:"GUM_LOG_PREFIX_"` | ||
Check failure on line 27 in log/options.go GitHub Actions / lint
|
||
MessageStyle style.Styles `embed:"" prefix:"message." help:"The style of the message" envprefix:"GUM_LOG_MESSAGE_"` | ||
KeyStyle style.Styles `embed:"" prefix:"key." help:"The style of the key" set:"defaultFaint=true" envprefix:"GUM_LOG_KEY_"` | ||
ValueStyle style.Styles `embed:"" prefix:"value." help:"The style of the value" envprefix:"GUM_LOG_VALUE_"` | ||
SeparatorStyle style.Styles `embed:"" prefix:"separator." help:"The style of the separator" set:"defaultFaint=true" envprefix:"GUM_LOG_SEPARATOR_"` | ||
} | ||
|
||
// BeforeReset hook. Used to unclutter style flags. | ||
func (o Options) BeforeReset(ctx *kong.Context) error { | ||
style.HideFlags(ctx) | ||
return nil | ||
} |