log

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2021 License: MIT Imports: 7 Imported by: 3

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
  • 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. Use them like so:

logger := log.Default()
logger.Warn("this is a warning")

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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

Infof writes formatted message to l.writer.

func Panic added in v0.3.0

func Panic(args ...interface{})

Panic is equivalent to panic(formattedMessage).

func Panicf added in v0.4.0

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

Panicf is equivalent to panic(formattedMessage).

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 added in v0.4.0

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

Warnf writes formatted warning to l.writer.

Types

type Log

type Log struct {
	// 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) 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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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

Infof writes formatted info message to l.writer.

func (*Log) Panic added in v0.3.0

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

Panic is equivalent to panic(formattedMessage).

func (*Log) Panicf added in v0.4.0

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 added in v0.4.0

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

Warnf writes formatted warning to l.writer.

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.

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