log

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2021 License: MIT Imports: 6 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.

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

Log Levels

This library comes with the following log levels:

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

These are enumerated as LvlDebug, LvlInfo, 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.LvlInfo) // default: LvlError

	// 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.Info("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(format string, args ...interface{})

Debug writes formatted message to l.writer.

func Error

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

Error writes formatted error to l.writer.

func Fatal

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

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

func Info

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

Info writes formatted message to l.writer.

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(format string, args ...interface{})

Warn 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(format string, args ...interface{})

Debug writes formatted debug message to l.writer.

func (*Log) Error

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

Error writes formatted error to l.writer.

func (*Log) Fatal

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

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

func (*Log) Info

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

Info writes formatted info message to l.writer.

func (*Log) Warn

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

Warn 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 (
	LevelFatal Priority = iota
	LevelError
	LevelWarn
	LevelInfo
	LevelDebug
)

Log Priority levels are listed here as constants for convenience.

Directories

Path Synopsis
example
basic command
concurrent command
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