log

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2022 License: MIT Imports: 7 Imported by: 9

README

log-go

Log-go is a logging library developed with simplicity and thread-safety in mind. It doesn't support any extra-fancy features. Just like Go itself.

If you wish to improve upon this, you are welcome to send me a Pull Request.

Example with Colors

Demo

Features

  • Different log levels like Fatal, Error, Info, etc.
  • Configurable buffered writer (os.Stdout, *File, etc.)
  • Thread safety
  • Prefixes to separate logs from everything else
  • Extremely simple setup
  • Optionally coloured terminal output
  • Well-tested

Log Levels

This library comes with the following log levels:

  1. Panic
  2. Fatal
  3. Error
  4. Warn
  5. Info
  6. Debug

These are enumerated as LevelDebug, LevelInfo, etc., so that you won't have to memorise them by numbers.

Each Log instance has five methods that are named precisely after the levels. As well as their formatted counterparts Debugf, Infof, Errorf, etc. Use them like so:

logger := log.Default()
logger.Warn("this is a warning")
logger.Infof("this is an information message #%d", 42)

Basic Setup

package main

import (
	"os"
	"github.com/sharpvik/log-go"
)

func init() {
	// Change log level.
	log.SetLevel(log.LevelInfo) // default: LevelError

	// Change log writer.
	file, _ := os.Create("server.log")
	log.SetWriter(file) // default: os.Stdout
}

func main() {
	// Computations ...
	x := 40 + 2

	// Print log with priority level Info.
	log.Infof("x = %d", x)
	// * 11/01/2021 23:07:08 INFO  x = 42
}

Examples

You are welcome to look at some examples too!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug writes formatted debug message to l.writer.

func Debugf

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

Debugf writes formatted message to l.writer.

func Error

func Error(args ...interface{})

Error writes formatted error to l.writer.

func Errorf

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

Errorf writes formatted error to l.writer.

func Fatal

func Fatal(args ...interface{})

Fatal is equivalent to Error and os.Exit(1).

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf is equivalent to Error and os.Exit(1).

func Info

func Info(args ...interface{})

Info writes formatted info message to l.writer.

func Infof

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

Infof writes formatted message to l.writer.

func Panic

func Panic(args ...interface{})

Panic is equivalent to panic(formattedMessage).

func Panicf

func Panicf(format string, args ...interface{})

Panicf is equivalent to panic(formattedMessage).

func SetColor

func SetColor(color bool)

SetColor changes std's color mode.

func SetLevel

func SetLevel(level Priority)

SetLevel changes std's priority level.

func SetWriter

func SetWriter(writer io.Writer)

SetWriter changes std's writer.

func Warn

func Warn(args ...interface{})

Warn writes formatted warning to l.writer.

func Warnf

func Warnf(format string, args ...interface{})

Warnf writes formatted warning to l.writer.

Types

type Log

type Log struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Log is the central logger struct.

func Default

func Default() *Log

Default returns a default logger instance used withing this package and available globally.

func New

func New(level Priority, writer io.Writer, prefix string, color bool) *Log

New returns a new custom logger instance.

func (*Log) Debug

func (l *Log) Debug(args ...interface{})

Debug writes formatted debug message to l.writer.

func (*Log) Debugf

func (l *Log) Debugf(format string, args ...interface{})

Debugf writes formatted debug message to l.writer.

func (*Log) Error

func (l *Log) Error(args ...interface{})

Error writes formatted error to l.writer.

func (*Log) Errorf

func (l *Log) Errorf(format string, args ...interface{})

Errorf writes formatted error to l.writer.

func (*Log) Fatal

func (l *Log) Fatal(args ...interface{})

Fatal is equivalent to Error and os.Exit(1).

func (*Log) Fatalf

func (l *Log) Fatalf(format string, args ...interface{})

Fatalf is equivalent to Error and os.Exit(1).

func (*Log) Info

func (l *Log) Info(args ...interface{})

Info writes formatted info message to l.writer.

func (*Log) Infof

func (l *Log) Infof(format string, args ...interface{})

Infof writes formatted info message to l.writer.

func (*Log) Panic

func (l *Log) Panic(args ...interface{})

Panic is equivalent to panic(formattedMessage).

func (*Log) Panicf

func (l *Log) Panicf(format string, args ...interface{})

Panicf is equivalent to panic(formattedMessage).

func (*Log) Warn

func (l *Log) Warn(args ...interface{})

Warn writes formatted warning to l.writer.

func (*Log) Warnf

func (l *Log) Warnf(format string, args ...interface{})

Warnf writes formatted warning to l.writer.

func (*Log) WithColor

func (l *Log) WithColor() *Log

WithColor returns a new logger instance with color mode turned on.

func (*Log) WithLevel

func (l *Log) WithLevel(level Priority) *Log

WithLevel returns a new logger instance with the specified level.

func (*Log) WithPrefix

func (l *Log) WithPrefix(prefix string) *Log

WithPrefix returns a new logger instance with the specified prefix.

func (*Log) WithWriter

func (l *Log) WithWriter(writer io.Writer) *Log

WithWriter returns a new logger instance with the specified writer.

func (*Log) WithoutColor

func (l *Log) WithoutColor() *Log

WithoutColor returns a new logger instance with color mode turned off.

type Priority

type Priority uint8

Priority is a type alias for logging priority levels.

const (
	LevelPanic Priority = iota
	LevelFatal
	LevelError
	LevelWarn
	LevelInfo
	LevelDebug
)

Log Priority levels are listed here as constants for convenience.

Directories

Path Synopsis
examples
colors
This example shows you all colours of the rainbow! :)
This example shows you all colours of the rainbow! :)
concurrent
This example demonstates that the log-go package is thread-safe.
This example demonstates that the log-go package is thread-safe.

Jump to

Keyboard shortcuts

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