gopherlogs

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

gopherlogs

go report card test status Apache-2.0 license Go.Dev reference

A simple, powerful, and extensible Go logging framework with batteries included 🔋.

gopherlogs is ideal for command line applications, combined logging to system files alongside terminal output, and much more.

Features:

  • Animated logging compatible with concurrent Go routines
  • Support for "emoji" style logs
  • Dynamic log line replacement
  • No external dependencies; what you see is what you get.

Usage

Install as a dependency in your Go module:

module example.com

go 1.19

require github.com/jpmcb/gopherlogs v0.1.0

And tidy up your modules and go.sum with:

$ go mod tidy

In your go code, you can now import gopherlogs:

package main

import (
        "github.com/jpmcb/gopherlogs"
)

func main() {
        // Creates a new logger with options
        l, err := gopherlogs.NewLogger(
            WithLogVerbosity(5),
        )

        // Handle errors from creating a new logger
        if err != nil {
            panic("Could not create new logger")
        }

        // Start logging!
        l.Info("Hello world")
}

When you run your Go program, it will look something like this:

$ go run main.go
Hello world

Why another Go logging framework?

This library is heavily inspired by the original logging library from Tanzu Community Edition's unmanaged-cluster CLI (which is no longer being maintained and is effectively abandoned).

I worked on that project for a few years, and we created the first iterations of this logging framework because we couldn't find anything suitable enough that was still very delightful to experience.

That experience is at the heart of this library; an amazing user experience for both the end user and the developer. In some ways, this logging library is a fork of some of the best pieces of Go code that came out of that project. And in the end, I believed it deserved to see abit more lite of day.

Shout out and huge kudos to the original authors: @stmcginnis, @joshrosso, and @jpmcb (me). Your work has inspired me and continues to bring me joy!

Documentation

Overview

package log provides logging mechanisms This file defines animator options to be used with AnimateProgressWithOptions

Package log provides logging mechanisms. It offers logging functionality that can include stylized logs, updating progress dots (...), and emojis. It respects a TTY parameter. When set to false, all stylization is removed.

Index

Constants

This section is empty.

Variables

View Source
var DefaultLogLevel = 0

DefaultLogLevel controls the default verbosity of log messages.

Functions

This section is empty.

Types

type AnimatorOption

type AnimatorOption interface {
	// contains filtered or unexported methods
}

AnimatorOption is an option to be passed to the AnimateProgressWithOptions logging method

func AnimatorWithContext

func AnimatorWithContext(ctx context.Context) AnimatorOption

AnimatorWithContext provides a context to the async call. That context can be canceled which stops the animation. Ex: AnimatorWithContext(myContext)

func AnimatorWithMaxLen

func AnimatorWithMaxLen(l int) AnimatorOption

AnimatorWithMaxLen sets the maximum number of dots to animate Ex:AnimatorWithMaxLen(3)

func AnimatorWithMessagef

func AnimatorWithMessagef(formatString string, formatArgs ...string) AnimatorOption

AnimatorWithMessagef sets the format string message and any format arguments to use Ex: AnimatorWithMessagef("Downloading to: %s", filePathLocation)

If no format arguments are provided, just the message will be displayed when animating Ex: AnimatorWithMessagef("Creating controller")

If a status channel is used, the first format argument in the template string _must_ be the status associated with the status channel Ex: AnimatorWithMessagef("controller status: %s")

func AnimatorWithStatusChan

func AnimatorWithStatusChan(s chan string) AnimatorOption

AnimatorWithStatusChan sets a string status channel that will be used to asynchronously inspect the status of an operation Ex: AnimatorWithStatusChan(myStatusChan)

type CMDLogger

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

CMDLogger is the logger implementation used for high-level command line logging.

func (*CMDLogger) AnimateProgressWithOptions

func (l *CMDLogger) AnimateProgressWithOptions(options ...AnimatorOption)

func (*CMDLogger) Error

func (l *CMDLogger) Error(message string)

func (*CMDLogger) Errorf

func (l *CMDLogger) Errorf(message string, args ...interface{})

func (*CMDLogger) Event

func (l *CMDLogger) Event(emoji, message string)

func (*CMDLogger) Eventf

func (l *CMDLogger) Eventf(emoji, message string, args ...interface{})

func (*CMDLogger) Info

func (l *CMDLogger) Info(message string)

func (*CMDLogger) Infof

func (l *CMDLogger) Infof(message string, args ...interface{})

func (*CMDLogger) ReplaceLinef

func (l *CMDLogger) ReplaceLinef(message string, args ...interface{})

func (*CMDLogger) Style

func (l *CMDLogger) Style(indent int, c colors.Attribute) Logger

func (*CMDLogger) V

func (l *CMDLogger) V(level int) Logger

func (*CMDLogger) Warn

func (l *CMDLogger) Warn(message string)

func (*CMDLogger) Warnf

func (l *CMDLogger) Warnf(message string, args ...interface{})

type Logger

type Logger interface {
	// Event takes an emoji codepoint (e.g. "\U0001F609") and presents a log message on it's own line.
	// This package provides several standard emoji codepoints as string constants. I.e: logger.HammerEmoji
	// Warning: Emojis may have variable width and this method assumes 2 width emojis, adding a space between the emoji and message.
	// Emojis provided in this package as string consts have 2 width and work with this method.
	// If you wish for additional space, add it at the beginning of the message (string) argument.
	Event(emoji, message string)
	// Eventf takes an emoji codepoint (e.g. "\U0001F609"), a format string, arguments for the format string.
	// This package provides several standard emoji codepoints as string constants. I.e: logger.HammerEmoji
	// Warning: Emojis may have variable width and this method assumes 2 width emojis, adding a space between the emoji and message.
	// Emojis provided in this package as string consts have 2 width and work with this method.
	// If you wish for additional space, add it at the beginning of the message (string) argument.
	Eventf(emoji, message string, args ...interface{})
	// Info prints a standard log message.
	// Line breaks are automatically added to the end.
	Info(message string)
	// Infof takes a format string, arguments, and prints it as a standard log message.
	// Line breaks are not automatically added to the end.
	Infof(message string, args ...interface{})
	// Warn prints a warning message. When TTY is enabled (default), it will be stylized as yellow.
	// Line breaks are automatically added to the end.
	Warn(message string)
	// Warnf takes a format string, arguments, and prints it as a warning message.
	// When TTY is enabled (default), it will be stylized as yellow.
	// Line breaks are not automatically added to the end.
	Warnf(message string, args ...interface{})
	// Error prints an error message. When TTY is enabled (default), it will be stylized as red.
	// Line breaks are automatically added to the end.
	Error(message string)
	// Errorf takes a format string, arguments, and prints it as an error message.
	// When TTY is enabled (default), it will be stylized as yellow.
	// Line breaks are not automatically added to the end.
	Errorf(message string, args ...interface{})
	// ReplaceLinef takes a template string message
	// and any optional format arguments
	// and replaces the current line.
	// This is useful after canceling AnimateProgressWithOptions and needing to print a final "success" message
	// Ex: ReplaceLinef("Finished reconciling controller: %s", controllerStatus)
	ReplaceLinef(message string, args ...interface{})
	// AnimateProgressWithOptions takes any number of AnimatorOptions
	// and is used to async animate a number of dots.
	// See the AnimatorOptions for further documentation
	// Ex: AnimateProgressWithOptions(AnimatorWithMaxLen(5))
	AnimateProgressWithOptions(options ...AnimatorOption)
	// V sets the level of the log message based on an integer. The logger implementation will hold a configured
	// log level, which this V level is assessed against to determine whether the log message should be output.
	V(level int) Logger
	// Style provides indentation and colorization of log messages. The indent argument specifies the amount of " "
	// characters to prepend to the message. The color should be specified using color constants in this package.
	Style(indent int, c colors.Attribute) Logger
}

Logger provides the logging interaction for the application.

func NewLogger

func NewLogger(options ...LoggerOptions) (Logger, error)

NewLogger returns an instance of Logger, implemented via CMDLogger.

type LoggerOptions

type LoggerOptions interface {
	// contains filtered or unexported methods
}

LoggerOptions an option to be passed to the NewLogger constructor

func WithColor added in v0.2.0

func WithColor(color colors.Attribute) LoggerOptions

func WithLeftPadIndent added in v0.2.0

func WithLeftPadIndent(indent int) LoggerOptions

func WithLogFile added in v0.2.0

func WithLogFile(filePath string) LoggerOptions

func WithLogLevel added in v0.2.0

func WithLogLevel(logLevel int) LoggerOptions

func WithLogVerbosity added in v0.2.0

func WithLogVerbosity(verbosity int) LoggerOptions

func WithOutputWriter added in v0.2.0

func WithOutputWriter(writer io.Writer) LoggerOptions

func WithTermFileDescriptor added in v0.2.0

func WithTermFileDescriptor(termFd int) LoggerOptions

func WithTty added in v0.2.0

func WithTty(isTty bool) LoggerOptions

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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