-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46328de
commit ef3120c
Showing
6 changed files
with
204 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
if o.File != "" { | ||
f, err := os.OpenFile(o.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) | ||
Check failure on line 16 in log/command.go GitHub Actions / lint-soft
|
||
if err != nil { | ||
return err | ||
Check failure on line 18 in log/command.go GitHub Actions / lint-soft
|
||
} | ||
|
||
defer f.Close() // nolint: errcheck | ||
Check failure on line 21 in log/command.go GitHub Actions / lint-soft
|
||
l.SetOutput(f) | ||
} | ||
|
||
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) | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_"` //nolint:staticcheck | ||
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_"` //nolint:staticcheck | ||
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_"` //nolint:staticcheck | ||
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_"` //nolint:staticcheck | ||
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_"` //nolint:staticcheck | ||
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_"` //nolint:staticcheck | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters