Categorygithub.com/pamburus/logftxt
repositorypackage
0.7.0
Repository: https://github.com/pamburus/logftxt.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

logftxt GoDoc Build Status Coverage Status

Package logftxt provides an alternative logf Appender and Encoder for colored text logs. It can be used as a more flexible replacement for logftext.

Features

  • Highlighting of log levels, messages, field names, delimiters, arrays, objects, strings, numbers, errors, durations, etc.
  • Built-in themes and external custom themes.
  • Built-in @default and @fancy themes have good support for both dark and light terminals.
  • Built-in configuration file that can easily be replaced with a custom configuration file.

Changing configuration

Built-in configuration file can be easily replaced with custom configuration file by

  • Copying it, editing it, and setting up an environment variable LOGFTXT_CONFIG pointing to the file
    • path/to/my-config.yml for a custom config relative to ~/.config/logftxt
    • ./path/to/my-config.yml for a custom config relative to the current directory
    • /home/root/path/to/my-config.yml for a custom config at an absolute path
  • Loading it manually with LoadConfig or ReadConfig and passing it as an optional parameter to NewAppender or NewEncoder function

Changing theme

Theme can be easily changed by

  • Setting up LOGFTXT_THEME environment variable to a value of
    • @default, @fancy or @legacy for a built-in theme
    • path/to/my-theme.yml for a custom theme relative to ~/.config/logftxt
    • ./path/to/my-theme.yml for a custom theme relative to the current directory
    • /home/root/path/to/my-theme.yml for a custom theme at an absolute path
  • Setting the theme name in the custom configuration file in the same format as the LOGFTXT_THEME environment variable
  • Providing an optional parameter ThemeRef to NewAppender or NewEncoder function containing the same value as the LOGFTXT_THEME environment variable
  • Loading it manually with LoadTheme or ReadTheme and passing it as an optional parameter to NewAppender or NewEncoder function

Example

The following example creates the new logf logger with the logftxt Appender constructed with the default Encoder. The source code can be found in examples/hello.

package main

import (
	"errors"
	"os"
	"runtime"
	"runtime/debug"

	"github.com/ssgreg/logf"

	"github.com/pamburus/logftxt"
)

func main() {
	// Create ChannelWriter with text Encoder with default settings.
	writer, writerClose := logf.NewChannelWriter(logf.ChannelWriterConfig{
		Appender: logftxt.NewAppender(os.Stdout),
	})
	defer writerClose()

	// Create Logger with ChannelWriter.
	logger := logf.NewLogger(logf.LevelDebug, writer).WithCaller().WithName("main")

	// Do some logging.
	logger.Info("runtime info", logf.Int("cpu-count", runtime.NumCPU()))

	if info, ok := debug.ReadBuildInfo(); ok {
		logger.Debug("build info", logf.String("go-version", info.GoVersion), logf.String("path", info.Path))
		for _, setting := range info.Settings {
			logger.Debug("build setting", logf.String("key", setting.Key), logf.String("value", setting.Value))
		}
		for _, dep := range info.Deps {
			logger.Debug("dependency", logf.String("path", dep.Path), logf.String("version", dep.Version))
		}
	} else {
		logger.Warn("couldn't get build info")
	}

	logger.Error("something bad happened", logf.Error(errors.New("failed to figure out what to do next")))
}

Example output

GitHub-Mark-Light GitHub-Mark-Dark

Used terminal color schemes

iTerm2

Alacritty

  • One Dark Neo
    • Note: It is recommended to use draw_bold_text_with_bright_colors: true setting
  • Light
    • Note: It is recommended to use draw_bold_text_with_bright_colors: false setting