clog

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2020 License: MIT Imports: 8 Imported by: 29

README

Build Status Go Report Card

clog (console-log)

All the fashionable loggers for Go tend to focus on structured logging, and that's perfectly fine: until you simply need a logger for a console application...

So here's yet another logger for Go:

  • unstructured logging
  • console logging in colour
  • file logging
  • simple to use
  • filter your logs with 4 levels of severity
  • redirect your logs to an io.Writer
  • get logs coming from an io.Writer
  • using the logger from the standard library under the hood
  • extensible (via handlers)

Have a look at the examples if you like the look of it

Here's a very simple one:

import "github.com/creativeprojects/clog"

func main() {
	log := clog.NewFilteredConsoleLogger(clog.LevelInfo)

	log.Debug("will be discarded")
	log.Info("will be displayed")
}

alt text

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseTestLog added in v0.1.0

func CloseTestLog()

CloseTestLog at the end of the test otherwise the logger will keep a reference on t. For a description on how to use it, see SetTestLog()

func Debug

func Debug(v ...interface{})

Debug sends debugging information

func Debugf

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

Debugf sends debugging information

func Error

func Error(v ...interface{})

Error sends error information to the console

func Errorf

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

Errorf sends error information to the console

func Info

func Info(v ...interface{})

Info logs some noticeable information

func Infof

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

Infof logs some noticeable information

func Log

func Log(level LogLevel, v ...interface{})

Log sends a log entry with the specified level

func Logf

func Logf(level LogLevel, format string, v ...interface{})

Logf sends a log entry with the specified level

func SetDefaultLogger

func SetDefaultLogger(logger *Logger)

SetDefaultLogger sets the logger used when using the package methods

func SetTestLog

func SetTestLog(t TestLogInterface)

SetTestLog install a test logger as the default logger. A test logger redirects all logs sent through the package methods to the Log/Logf methods of your test

IMPORTANT: don't forget to CloseTestLog() at the end of the test

Example:

func TestLog(t *testing.T) {
	SetTestLog(t)
	defer CloseTestLog()
	// These two calls are equivalent:
	clog.Debug("debug message")
	t.Log("debug message")
}

func Stderr added in v0.4.0

func Stderr() io.Writer

Stderr returns a thread-safe io.Writer to os.Stderr. Calling this function multiple times always returns the same instance of io.Writer.

func Stdout added in v0.4.0

func Stdout() io.Writer

Stdout returns a thread-safe io.Writer to os.Stdout. Calling this function multiple times always returns the same instance of io.Writer.

func Warning

func Warning(v ...interface{})

Warning send some important message to the console

func Warningf

func Warningf(format string, v ...interface{})

Warningf send some important message to the console

Types

type AferoHandler

type AferoHandler struct {
	*StandardLogHandler
	// contains filtered or unexported fields
}

AferoHandler logs messages to a file

func NewAferoHandler

func NewAferoHandler(fs afero.Fs, filename string, prefix string, flag int) (*AferoHandler, error)

NewAferoHandler creates a new file logger through afero Fs.

Remember to Close() the logger at the end

func (*AferoHandler) Close

func (l *AferoHandler) Close()

Close the logfile when no longer needed.

Please note this method reinstate the standard console output as default

type ConsoleHandler

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

ConsoleHandler logs messages to the console (in colour)

func NewConsoleHandler

func NewConsoleHandler(prefix string, flag int) *ConsoleHandler

NewConsoleHandler creates a new handler to send logs to the console

func (*ConsoleHandler) Colouring

func (l *ConsoleHandler) Colouring(colouring bool)

Colouring activate of deactivate displaying messages in colour in the console

func (*ConsoleHandler) LogEntry

func (l *ConsoleHandler) LogEntry(logEntry LogEntry) error

LogEntry sends a log entry with the specified level

func (*ConsoleHandler) SetTheme

func (l *ConsoleHandler) SetTheme(theme string)

SetTheme sets the dark or light theme

type DiscardHandler

type DiscardHandler struct{}

DiscardHandler forgets any log message

func NewDiscardHandler added in v0.4.0

func NewDiscardHandler() *DiscardHandler

NewDiscardHandler returns a handler that forgets all the logs you throw at it.

func (*DiscardHandler) LogEntry

func (l *DiscardHandler) LogEntry(LogEntry) error

LogEntry discards the LogEntry

type FileHandler

type FileHandler struct {
	*StandardLogHandler
	// contains filtered or unexported fields
}

FileHandler logs messages to a file

func NewFileHandler

func NewFileHandler(filename string, prefix string, flag int) (*FileHandler, error)

NewFileHandler creates a new file logger

Remember to Close() the logger at the end

func (*FileHandler) Close

func (l *FileHandler) Close()

Close the logfile when no longer needed

please note this method reinstate the standard console output as default

type Handler

type Handler interface {
	LogEntry(LogEntry) error
}

Handler for a logger.

The LogEntry method should return an error if the handler didn't manage to save the log (file, remote, etc.) It's up to the parent handler to take action on the error: the default Logger is always going to ignore it.

type LevelFilter

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

LevelFilter is a log middleware that is only passing log entries of level >= minimum level

func NewLevelFilter

func NewLevelFilter(minLevel LogLevel, destination Handler) *LevelFilter

NewLevelFilter creates a new LevelFilter handler passing log entries to destination if level >= minimum level

func (*LevelFilter) GetHandler

func (l *LevelFilter) GetHandler() Handler

GetHandler returns the current handler used by the filter

func (*LevelFilter) LogEntry

func (l *LevelFilter) LogEntry(logEntry LogEntry) error

LogEntry the LogEntry

func (*LevelFilter) SetHandler

func (l *LevelFilter) SetHandler(handler Handler)

SetHandler sets a new handler for the filter

func (*LevelFilter) SetLevel

func (l *LevelFilter) SetLevel(minLevel LogLevel)

SetLevel changes the minimum level the log entries are going to be sent to the destination logger

type LogEntry

type LogEntry struct {
	Calldepth int           // Calldepth is used to calculate the right place where we called the log method
	Level     LogLevel      // Debug, Info, Warning or Error
	Format    string        // Format for *printf (leave blank for *print)
	Values    []interface{} // Values for *print and *printf
}

LogEntry represents a log entry

func (LogEntry) GetMessage

func (l LogEntry) GetMessage() string

GetMessage returns the formatted message from Format & Values

func (LogEntry) GetMessageWithLevelPrefix

func (l LogEntry) GetMessageWithLevelPrefix() string

GetMessageWithLevelPrefix returns the formatted message from Format & Values prefixed with the level name

type LogLevel

type LogLevel int

LogLevel represents the importance of a log entry

const (
	LevelDebug LogLevel = iota
	LevelInfo
	LevelWarning
	LevelError
)

LogLevel

func (LogLevel) String

func (level LogLevel) String() string

String representation of a level (5 characters)

type Logger

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

Logger frontend

func GetDefaultLogger

func GetDefaultLogger() *Logger

GetDefaultLogger returns the logger used when using the package methods

func NewConsoleLogger

func NewConsoleLogger() *Logger

NewConsoleLogger is a shortcut to create a Logger with a ConsoleHandler

func NewFilteredConsoleLogger

func NewFilteredConsoleLogger(minLevel LogLevel) *Logger

NewFilteredConsoleLogger is a shortcut to create a Logger with a FilteredHandler sending to a ConsoleHandler

func NewLogger

func NewLogger(handler Handler) *Logger

NewLogger creates a new logger

func (*Logger) Debug

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

Debug sends debugging information

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

Debugf sends debugging information

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

Error sends error information to the console

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

Errorf sends error information to the console

func (*Logger) GetHandler

func (l *Logger) GetHandler() Handler

GetHandler returns the current handler used by the logger

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

Info logs some noticeable information

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

Infof logs some noticeable information

func (*Logger) Log

func (l *Logger) Log(level LogLevel, v ...interface{})

Log sends a log entry with the specified level

func (*Logger) LogEntry

func (l *Logger) LogEntry(logEntry LogEntry) error

LogEntry sends a LogEntry directly. Logger can also be used as a Handler

func (*Logger) Logf

func (l *Logger) Logf(level LogLevel, format string, v ...interface{})

Logf sends a log entry with the specified level

func (*Logger) SetHandler

func (l *Logger) SetHandler(handler Handler)

SetHandler sets a new handler for the logger

func (*Logger) Warning

func (l *Logger) Warning(v ...interface{})

Warning send some important message to the console

func (*Logger) Warningf

func (l *Logger) Warningf(format string, v ...interface{})

Warningf send some important message to the console

type MemoryHandler

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

MemoryHandler save messages in memory (useful for unit testing).

func NewMemoryHandler

func NewMemoryHandler() *MemoryHandler

NewMemoryHandler creates a new MemoryHandler that keeps logs in memory.

func (*MemoryHandler) LogEntry

func (l *MemoryHandler) LogEntry(logEntry LogEntry) error

LogEntry keep the message in memory.

type SafeHandler

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

SafeHandler sends logs to an alternate destination when the primary destination fails

func NewSafeHandler

func NewSafeHandler(primary, backup Handler) *SafeHandler

NewSafeHandler creates a handler that redirects logs to a backup handler when the primary fails

func (*SafeHandler) LogEntry

func (l *SafeHandler) LogEntry(logEntry LogEntry) error

LogEntry send messages to primaryHandler first, then to backupHandler if it had fail

type SafeWriter added in v0.4.0

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

SafeWriter is a thread safe io.Writer

func NewSafeWriter added in v0.4.0

func NewSafeWriter(writer io.Writer) *SafeWriter

NewSafeWriter creates a thread-safe io.Writer

func (*SafeWriter) Write added in v0.4.0

func (w *SafeWriter) Write(p []byte) (n int, err error)

Write data (thread safe)

type StandardLogHandler

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

StandardLogHandler send messages to a io.Writer using the standard logger.

func NewStandardLogHandler

func NewStandardLogHandler(out io.Writer, prefix string, flag int) *StandardLogHandler

NewStandardLogHandler creates a handler to send the logs to io.Writer through a standard logger.

func (*StandardLogHandler) LogEntry

func (l *StandardLogHandler) LogEntry(logEntry LogEntry) error

LogEntry sends a log entry with the specified level.

func (*StandardLogHandler) SetOutput

func (l *StandardLogHandler) SetOutput(output io.Writer)

SetOutput sets the output destination for the logger.

type StandardLogger added in v0.2.0

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

StandardLogger can be used when you need to plug-in a standard library logger (via an interface)

func NewStandardLogger added in v0.2.0

func NewStandardLogger(level LogLevel, handler Handler) *StandardLogger

NewStandardLogger creates a new logger that can be used in place of a standard library logger (via an interface)

func (*StandardLogger) Fatal added in v0.2.0

func (l *StandardLogger) Fatal(v ...interface{})

Fatal is equivalent to l.Print() followed by a call to os.Exit(1).

func (*StandardLogger) Fatalf added in v0.2.0

func (l *StandardLogger) Fatalf(format string, v ...interface{})

Fatalf is equivalent to l.Printf() followed by a call to os.Exit(1).

func (*StandardLogger) Fatalln added in v0.2.0

func (l *StandardLogger) Fatalln(v ...interface{})

Fatalln is equivalent to l.Println() followed by a call to os.Exit(1).

func (*StandardLogger) Panic added in v0.2.0

func (l *StandardLogger) Panic(v ...interface{})

Panic is equivalent to l.Print() followed by a call to panic().

func (*StandardLogger) Panicf added in v0.2.0

func (l *StandardLogger) Panicf(format string, v ...interface{})

Panicf is equivalent to l.Printf() followed by a call to panic().

func (*StandardLogger) Panicln added in v0.2.0

func (l *StandardLogger) Panicln(v ...interface{})

Panicln is equivalent to l.Println() followed by a call to panic().

func (*StandardLogger) Print added in v0.2.0

func (l *StandardLogger) Print(v ...interface{})

Print writes the output for a logging event. Arguments are handled in the manner of fmt.Print. A newline is appended if the last character of s is not already a newline.

func (*StandardLogger) Printf added in v0.2.0

func (l *StandardLogger) Printf(format string, v ...interface{})

Printf writes the output for a logging event. Arguments are handled in the manner of fmt.Printf. A newline is appended if the last character of s is not already a newline.

func (*StandardLogger) Println added in v0.2.0

func (l *StandardLogger) Println(v ...interface{})

Println writes the output for a logging event. Arguments are handled in the manner of fmt.Println.

type TestHandler

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

TestHandler redirects all the logs to the testing framework logger

func NewTestHandler

func NewTestHandler(t TestLogInterface) *TestHandler

NewTestHandler instantiates a new logger redirecting to the test framework logger or any other implementation of TestLogInterface for that matter

func (*TestHandler) LogEntry

func (l *TestHandler) LogEntry(logEntry LogEntry) error

LogEntry sends a log entry with the specified level

type TestLogInterface

type TestLogInterface interface {
	Log(args ...interface{})
	Logf(format string, args ...interface{})
}

TestLogInterface for use with testing.B or testing.T

type Writer added in v0.3.0

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

Writer is an io.Writer that writes into a Handler. This can be used to redirect a log.Logger into a handler.

func NewWriter added in v0.3.0

func NewWriter(level LogLevel, handler Handler) *Writer

NewWriter creates a new Writer to a Handler

func (*Writer) Write added in v0.3.0

func (w *Writer) Write(p []byte) (n int, err error)

Write bytes into the logger

Directories

Path Synopsis
examples
console command
file command
filter command
stdlog command

Jump to

Keyboard shortcuts

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