applog

package
v0.0.0-...-2eeca3e Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 License: MIT Imports: 12 Imported by: 0

README

applog -- The Application Logger

The applog library takes a UI-focused approach to logging for an application. It's primarily intended to be used for informing the application user of things like in-progress tasks, errors, and important messages.

The library is also intended to make supporting an application easier, by logging everything, even if it only displays a subset of information to the user.

Examples

package main

import (
    "time"

    "github.com/whamcloud/logging/applog"
)

func main() {
    applog.StartTask("Doing some long-running process")
    time.Sleep(10 * time.Second)
    applog.EndTask()    

    name := "Fred"
    if 2 + 2 == 5 {
        applog.Fail("Reality is broken, %s!", name)
    } else {
        applog.User("Things are fine, %s!", name)
    }
}

Will display something like (with a spinner until complete):

Doing some long-running process... Done.

Things are fine, Fred!

All but the final EndTask() call may be omitted if there is a sequential series of tasks started with StartTask().

A call to Fail() with an error object will prepend the text "ERROR: " to the Error() string before exiting, otherwise it just prints the arguments before exiting.

Documentation

Index

Constants

View Source
const (
	// DEBUG shows all
	DEBUG displayLevel = iota
	// TRACE shows application flow, suitable for support
	TRACE
	// USER shows user-appropriate messages
	USER
	// WARN shows warnings
	WARN
	// FAIL is bad
	FAIL
	// SILENT shows nothing
	SILENT
)

Variables

This section is empty.

Functions

func CompleteTask

func CompleteTask(v ...interface{})

CompleteTask stops the spinner and prints a newline

func Debug

func Debug(v ...interface{})

Debug logs the entry and prints to stdout if level <= DEBUG

func Fail

func Fail(v ...interface{})

Fail logs the entry and prints to stderr if level <= FAIL

func IsTerminal

func IsTerminal(fd int) bool

IsTerminal returns true if the given file descriptor is a terminal. Swiped from golang.org/x/crypto/ssh/terminal

func SetJournal

func SetJournal(w interface{})

SetJournal sets the standard logger's journal writer

func SetLevel

func SetLevel(d displayLevel)

SetLevel sets the standard logger's display level

func SetStandard

func SetStandard(l *AppLogger)

SetStandard sets the standard logger to the supplied logger

func StartTask

func StartTask(v ...interface{})

StartTask logs the entry at USER level and displays a spinner for long-running tasks

func Trace

func Trace(v ...interface{})

Trace logs the entry and prints to stdout if level <= TRACE

func User

func User(v ...interface{})

User logs the entry and prints to stdout if level <= USER

func Warn

func Warn(v ...interface{})

Warn logs the entry and prints to stderr if level <= WARN

func WriterIsTerminal

func WriterIsTerminal(writer io.Writer) bool

WriterIsTerminal returns true if the given io.Writer converts to an *os.File and the file's fd is a terminal.

Types

type AppLogger

type AppLogger struct {
	Level displayLevel
	// contains filtered or unexported fields
}

AppLogger is a logger with methods for displaying entries to the user after recording them to a journal.

func New

func New(options ...OptSetter) *AppLogger

New returns a new AppLogger

func StandardLogger

func StandardLogger() *AppLogger

StandardLogger returns the standard logger configured by the library

func (*AppLogger) CompleteTask

func (l *AppLogger) CompleteTask(v ...interface{})

CompleteTask stops the spinner and prints a newline

func (*AppLogger) Debug

func (l *AppLogger) Debug(v ...interface{})

Debug logs the entry and prints to stdout if level <= DEBUG

func (*AppLogger) DisplayLevel

func (l *AppLogger) DisplayLevel(level displayLevel)

DisplayLevel sets the logger's display level

func (*AppLogger) Fail

func (l *AppLogger) Fail(v ...interface{})

Fail logs the entry and prints to stderr if level <= FAIL

func (*AppLogger) JournalFile

func (l *AppLogger) JournalFile(w interface{})

JournalFile configures the logger's journaler

func (*AppLogger) StartTask

func (l *AppLogger) StartTask(v ...interface{})

StartTask logs the entry at USER level and displays a spinner for long-running tasks

func (*AppLogger) Trace

func (l *AppLogger) Trace(v ...interface{})

Trace logs the entry and prints to stdout if level <= TRACE

func (*AppLogger) User

func (l *AppLogger) User(v ...interface{})

User logs the entry and prints to stdout if level <= USER

func (*AppLogger) Warn

func (l *AppLogger) Warn(v ...interface{})

Warn logs the entry and prints to stderr if level <= WARN

func (*AppLogger) Writer

func (l *AppLogger) Writer() *LoggedWriter

Writer returns an io.Writer for injecting our logging into third-party libraries

type LoggedWriter

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

LoggedWriter implements io.Writer and is used to redirect logging from 3rd-party libraries to this library.

func Writer

func Writer() *LoggedWriter

Writer returns an io.Writer for injecting our logging into 3rd-party libraries

func (*LoggedWriter) Level

func (w *LoggedWriter) Level(level displayLevel) *LoggedWriter

Level optionally sets the LoggedWriter log level

func (*LoggedWriter) Prefix

func (w *LoggedWriter) Prefix(prefix string) *LoggedWriter

Prefix optionally sets the LoggedWriter prefix

func (*LoggedWriter) Write

func (w *LoggedWriter) Write(data []byte) (int, error)

Write logs the data at the specified loglevel

type OptSetter

type OptSetter func(*AppLogger)

OptSetter sets logger options

func DisplayLevel

func DisplayLevel(d displayLevel) OptSetter

DisplayLevel sets the logger's display level

func JournalFile

func JournalFile(w interface{}) OptSetter

JournalFile configures the logger's journaler

Jump to

Keyboard shortcuts

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