cli

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2024 License: MIT Imports: 16 Imported by: 16

README

cli

I consistently needed a simple output printer for my console projects that did not require a lot of setup. Since I couldn't find a library to fill this role, I just wrote this project quickly for myself.

Features include:

  • cross-platform colored output
  • automatic tty detection
  • leveled printing

This library is not meant to be a comprehensive logging library. If you need more out of your logging library, I recommend Logrus.

Usage

example.go
package main

import "github.com/gesquive/cli"

func main() {
	cli.SetPrintLevel(cli.LevelInfo)
	cli.Debug("debug")
	cli.Info("info")
	cli.Warn("warn")
	cli.Error("error")
}
debug
info
warn
error

License

This library is made available under an MIT-style license. See LICENSE.

Contributing

PRs are always welcome!

Documentation

Index

Constants

View Source
const (
	LevelDebug = iota
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
)

Print levels go in order: Debug, Info, Warn, Error, Fatal

Variables

Blue creates a blue string

Green creates a green string

Red creates a red string

View Source
var SprintfBlue = color.New(color.FgHiBlue).SprintfFunc()

SprintfBlue creates a blue formatted string

View Source
var SprintfGreen = color.New(color.FgHiGreen).SprintfFunc()

SprintfGreen creates a green formatted string

View Source
var SprintfRed = color.New(color.FgHiRed).SprintfFunc()

SprintfRed creates a red formatted string

View Source
var SprintfYellow = color.New(color.FgHiYellow).SprintfFunc()

SprintfYellow creates a yellow formatted string

View Source
var Yellow = SprintfYellow

Yellow creates a yellow string

Functions

func Debug

func Debug(format string, a ...interface{})

Debug prints a formatted debug level message with a newline appended

func Debugf

func Debugf(format string, a ...interface{})

Debugf prints a formatted debug level message

func Debugln

func Debugln(a ...interface{})

Debugln prints a debug level message with a newline appended

func Error

func Error(format string, a ...interface{})

Error prints a formatted error level message with a newline appended

func Errorf

func Errorf(format string, a ...interface{})

Errorf prints a formatted error level message

func Errorln

func Errorln(a ...interface{})

Errorln prints an error level message with a newline appended

func Fatal

func Fatal(format string, a ...interface{})

Fatal prints a formatted fatal level message with a newline appended and calls os.Exit(1)

func Fatalf

func Fatalf(format string, a ...interface{})

Fatalf prints a formatted fatal level message and calls os.Exit(1)

func Fatalln

func Fatalln(a ...interface{})

Fatalln prints a fatal level message with a newline appended and calls os.Exit(1)

func Info

func Info(format string, a ...interface{})

Info prints a formatted info level message with a newline appended

func Infof

func Infof(format string, a ...interface{})

Infof prints a formatted info level message

func Infoln

func Infoln(a ...interface{})

Infoln prints an info level message with a newline appended

func NewHandler added in v0.3.0

func NewHandler(w io.Writer, opts *HandlerOptions) slog.Handler

func SetAsDefault added in v0.3.0

func SetAsDefault(w io.Writer, opts *HandlerOptions)

func SetColor added in v0.1.1

func SetColor(colorOn bool)

SetColor sets the color status. True for color, False for no color

func SetErrorWriter

func SetErrorWriter(w io.Writer)

SetErrorWriter allows you to set the output writer for error and fatal messages

func SetOutputWriter

func SetOutputWriter(w io.Writer)

SetOutputWriter allows you to set the output file for debug, info, and warn messges

func SetPrintLevel added in v0.2.0

func SetPrintLevel(level int)

SetPrintLevel allows you to set the level to print, by default LevelInfo is set

func Warn

func Warn(format string, a ...interface{})

Warn prints a formatted warning level message with a newline appended

func Warnf

func Warnf(format string, a ...interface{})

Warnf prints a formatted warning level message

func Warnln

func Warnln(a ...interface{})

Warnln prints a warning level message with a newline appended

Types

type Handler added in v0.3.0

type Handler struct {
	// contains filtered or unexported fields
}

func (*Handler) Enabled added in v0.3.0

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

func (*Handler) Handle added in v0.3.0

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

func (*Handler) SetLogLoggerLevel added in v0.3.0

func (h *Handler) SetLogLoggerLevel(level slog.Level)

func (*Handler) WithAttrs added in v0.3.0

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Handler) WithGroup added in v0.3.0

func (h *Handler) WithGroup(name string) slog.Handler

type HandlerOptions added in v0.3.0

type HandlerOptions struct {
	// AddSource causes the handler to compute the source code position
	// of the log statement and add a SourceKey attribute to the output.
	AddSource bool

	// Level reports the minimum record level that will be logged.
	// The handler discards records with lower levels.
	// If Level is nil, the handler assumes LevelInfo.
	// The handler calls Level.Level for each record processed;
	// to adjust the minimum level dynamically, use a LevelVar.
	Level slog.Leveler

	// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
	// The attribute's value has been resolved (see [Value.Resolve]).
	// If ReplaceAttr returns a zero Attr, the attribute is discarded.
	//
	// The built-in attributes with keys "time", "level", "source", and "msg"
	// are passed to this function, except that time is omitted
	// if zero, and source is omitted if AddSource is false.
	//
	// The first argument is a list of currently open groups that contain the
	// Attr. It must not be retained or modified. ReplaceAttr is never called
	// for Group attributes, only their contents. For example, the attribute
	// list
	//
	//     Int("a", 1), Group("g", Int("b", 2)), Int("c", 3)
	//
	// results in consecutive calls to ReplaceAttr with the following arguments:
	//
	//     nil, Int("a", 1)
	//     []string{"g"}, Int("b", 2)
	//     nil, Int("c", 3)
	//
	// ReplaceAttr can be used to change the default keys of the built-in
	// attributes, convert types (for example, to replace a `time.Time` with the
	// integer seconds since the Unix epoch), sanitize personal information, or
	// remove attributes from the output.
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr

	// Time format (Default: time.DateTime)
	TimeFormat string

	// Disable color (Default: false)
	NoColor bool
}

HandlerOptions is a drop in replacement for slog.HandlerOptions

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL