log

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2022 License: LGPL-3.0 Imports: 12 Imported by: 12

README

Golang Logging Library

The colorful and simple logging library for Golang.

What is this

Opinionated logging library that able to output to io.Reader with file descriptors (os.Stdout, os.Stderr, regular file, etc.) with automatic terminal color support.

The log serverity and behavior can be described as follows:

Severity Description Caller Info
Fatal Unrecoverable error and automatically exit after logging Yes
Error Recoverable error but need attention Yes
Warn Minor error and does not output the caller info No
Info Informational message No
Debug Debug message, only shown when debug enabled Yes
Trace Trace message, only shown when debug enabled No

Getting started

Add the go-log package using

go get github.com/withmandala/go-log

And import it to your package by

import (
    "github.com/withmandala/go-log"
)

Use the go-log package with

logger := log.New(os.Stderr)
logger.Info("Hi, this is your logger")

Write to a log file

f, err := os.Create("app.log")
if err != nil {
	fmt.Println(err)
	return
}
logger := log.New(f)

Color support

The library will try to automatically detect the io.Reader file descriptor when calling log.New() for color support. But, if you insist to use or not to use color, you can add .WithColor() or .WithoutColor() respectively.

// With color
logger := log.New(os.Stderr).WithColor()

// Without color
logger := log.New(os.Stderr).WithoutColor()

Debug output

The log library will suppress the .Debug() and .Trace() output by default. To enable or disable the debug output, call (Logger).WithDebug() or (Logger).WithoutDebug() respectively.

// Enable debugging
logger := log.New(os.Stderr).WithDebug()
// Print debug output
logger.Debug("Test debug output")
// Disable debug output
logger.WithoutDebug()
logger.Debug("Test debug output") // This message will not be printed

Be Quiet

If somehow the log is annoying to you, just shush it by calling (Logger).Quiet() and ALL log output will be disappear, although .Fatal() will silently quit the program with error. To re-enable the log output use (Logger).NoQuiet().

Documentation

Overview

The colorful and simple logging library

Index

Constants

This section is empty.

Variables

View Source
var (
	PanicPrefix = Prefix{
		Plain: []byte("[PANIC] "),
		Color: append(append([]byte{'['}, colorful.Red([]byte("PANIC"))...), []byte("] ")...),
		File:  false,
	}

	// FatalPrefix show fatal prefix
	FatalPrefix = Prefix{
		Plain: []byte("[FATAL] "),
		Color: append(append([]byte{'['}, colorful.Red([]byte("FATAL"))...), []byte("] ")...),
		File:  true,
	}

	// ErrorPrefix show error prefix
	ErrorPrefix = Prefix{
		Plain: []byte("[ERROR] "),
		Color: append(append([]byte{'['}, colorful.Red([]byte("ERROR"))...), []byte("] ")...),
		File:  true,
	}

	// WarnPrefix show warn prefix
	WarnPrefix = Prefix{
		Plain: []byte("[WARN]  "),
		Color: append(append([]byte{'['}, colorful.Orange([]byte("WARN"))...), []byte("]  ")...),
	}

	// InfoPrefix show info prefix
	InfoPrefix = Prefix{
		Plain: []byte("[INFO]  "),
		Color: append(append([]byte{'['}, colorful.Green([]byte("INFO"))...), []byte("]  ")...),
	}

	// DebugPrefix show info prefix
	DebugPrefix = Prefix{
		Plain: []byte("[DEBUG] "),
		Color: append(append([]byte{'['}, colorful.Purple([]byte("DEBUG"))...), []byte("] ")...),
		File:  true,
	}

	// TracePrefix show info prefix
	TracePrefix = Prefix{
		Plain: []byte("[TRACE] "),
		Color: append(append([]byte{'['}, colorful.Cyan([]byte("TRACE"))...), []byte("] ")...),
	}
)

Functions

This section is empty.

Types

type FdWriter

type FdWriter interface {
	io.Writer
	Fd() uintptr
}

FdWriter interface extends existing io.Writer with file descriptor function support

type IOStream

type IOStream chan<- interface{}

Mainly this just looks cool.

Logger IOStreams are syntactically similar to C++ iostream to stdout: `std::cout << "Words\n"` becomes `logger.Info() <- "Words"`

This may be cool for CGO with C++ (if you're crazy)!

type Logger

type Logger struct {
	Name string

	Color bool

	DebugOut  bool
	Timestamp bool
	Quiet     bool
	// contains filtered or unexported fields
}

Logger defines the underlying storage for single logger.

IOStreams are cached on first call to their functions.

func New

func New(out io.Writer, name string) *Logger

New returns new Logger instance with predefined writer output and automatically detect terminal coloring support

func (*Logger) Debug

func (l *Logger) Debug() IOStream

func (*Logger) Debugf

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

Debugf print formatted debug message to output if debug output enabled

func (*Logger) Error

func (l *Logger) Error() IOStream

func (*Logger) Errorf

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

Errorf print formatted error message to output

func (*Logger) Fatal

func (l *Logger) Fatal() IOStream

func (*Logger) Fatalf

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

Fatalf print formatted fatal message to output and quit the application with status 1

func (*Logger) Info

func (l *Logger) Info() IOStream

func (*Logger) Infof

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

Infof print formatted informational message to output

func (*Logger) Output

func (l *Logger) Output(depth int, prefix Prefix, data string, caller string) error

Output print the actual value

func (*Logger) Recover

func (l *Logger) Recover() func(exit bool)

A function that recovers and writes pretty panic messages. Usage: `defer Recover()(true)` to exit, false argument to continue running.

func (*Logger) Trace

func (l *Logger) Trace() IOStream

func (*Logger) Tracef

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

Tracef print formatted trace message to output if debug output enabled

func (*Logger) Warn

func (l *Logger) Warn() IOStream

func (*Logger) Warnf

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

Warnf print formatted warning message to output

type Prefix

type Prefix struct {
	Plain []byte
	Color []byte
	File  bool
}

Prefix struct define plain and color byte

Directories

Path Synopsis
Buffer-like byte slice
Buffer-like byte slice
The color engine for the go-log library
The color engine for the go-log library

Jump to

Keyboard shortcuts

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