asklog

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2024 License: MIT Imports: 10 Imported by: 1

README

asklog

Log config / utils for CLI tool building with github.com/protolambda/ask.

This uses the github.com/ethereum/go-ethereum/log package.

Example

See asklog_example_test.go.

License

MIT, see LICENSE file.

Documentation

Overview

Example
package main

import (
	"context"

	"github.com/protolambda/ask"
	"github.com/protolambda/asklog"
)

type MainCmd struct {
	LogConfig asklog.Config `ask:"."`

	Foobar string `ask:"--foobar" help:"Some other flag"`
}

func (m *MainCmd) Default() {
	m.LogConfig.Default()
}

func (m *MainCmd) Run(ctx context.Context, args ...string) error {
	logger := m.LogConfig.New()
	logger.Info("Hello world!", "foobar", m.Foobar)
	// ...
	return nil
}

func main() {
	ask.Run(&MainCmd{})
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Out to write log data to. If nil, os.Stdout will be used.
	Out io.Writer `ask:"-"`

	Level  Level  `ask:"--log.level" help:"Log level: 'trace', 'debug', 'info', 'warn', 'error', 'crit'. Aliases and mixed-case are excepted."`
	Format Format `ask:"--log.format" help:"Log format: 'text', 'terminal', 'logfmt', 'json'."`
	Color  bool   `ask:"--log.color" help:"Enable log coloring (terminal format only)"`
}

func (*Config) Default

func (c *Config) Default()

func (*Config) New

func (c *Config) New() log.Logger

type DynamicLogHandler

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

DynamicLogHandler allow runtime-configuration of the log handler.

func NewDynamicLogHandler

func NewDynamicLogHandler(lvl slog.Level, h slog.Handler) *DynamicLogHandler

func (*DynamicLogHandler) Enabled

func (d *DynamicLogHandler) Enabled(ctx context.Context, lvl slog.Level) bool

func (*DynamicLogHandler) Handle

func (d *DynamicLogHandler) Handle(ctx context.Context, r slog.Record) error

func (*DynamicLogHandler) SetLogLevel

func (d *DynamicLogHandler) SetLogLevel(lvl slog.Level)

func (*DynamicLogHandler) WithAttrs

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

func (*DynamicLogHandler) WithGroup

func (d *DynamicLogHandler) WithGroup(name string) slog.Handler

type Format

type Format string
const (
	FormatTerminal Format = "terminal"
	FormatLogFmt   Format = "logfmt"
	FormatJSON     Format = "json"
)

func (Format) Handler

func (f Format) Handler(color bool) func(writer io.Writer) slog.Handler

func (*Format) Set

func (f *Format) Set(v string) error

func (*Format) String

func (f *Format) String() string

func (*Format) Type

func (f *Format) Type() string

type Level

type Level slog.Level

func (Level) Level

func (lvl Level) Level() slog.Level

func (*Level) Set

func (lvl *Level) Set(v string) error

func (Level) String

func (lvl Level) String() string

func (Level) Type

func (lvl Level) Type() string

type LvlSetter

type LvlSetter interface {
	SetLogLevel(lvl slog.Level)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳