logolang

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2020 License: MIT Imports: 5 Imported by: 4 modules, 7 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

logolang

Package logolang is a simple and thread-safe library for logging operations.

Example

package main

import (
	log "github.com/Miguel-Dorta/logolang"
)

func main() {
	log.DefaultLogger.Level = log.LevelDebug

	log.Debug("debug test")
	log.Info("info test")
	log.Error("error test")
	log.Critical("critical test")
}

Output

Example of logolang

Documentation

Logger levels

The logger object from logolang have an internal logger level that is used to determine which log messages should log. It will log the messages whose log level is equal or less than the defined level. This levels are:

  • 0: no log
  • 1: critical
  • 2: error
  • 3: info
  • 4: debug
Formatter

The standard behaviour of logolang is to print log message with the following format:

  • [YYYY-MM-DD hh:mm:ss.nssssssss] LEVEL: MESSAGE

You may not want this, so you can set a custom formatter function. This function will receive the name of the level logged (levelName) and the message to display (msg). This function will return just a string with the formatted text.

Colors

The standard behaviour of logolang is to print log messages in the terminal coloring the level name of the logger. This uses special characters that the terminal will understand as colors, but text files and other things could not identify it as that. For that reason, you can disable it.

Writers

If you want to use a different writers for logging operations, it MUST be safe for concurrent use. You can get a thread-safe writer for an unsafe one by wrapping it in a SafeWriter. If you're not sure if your writer is safe or not, you should probably wrap it.

The custom writers MUST also be reliable for writing, because a logging operation that founds an error while writing will end up in panic.

You can read the entire documentation in https://godoc.org/github.com/Miguel-Dorta/logolang

Documentation

Overview

Package logolang is a simple and thread-safe library for logging operations.

Logger levels

The logger object from logolang have an internal logger level that is used to determine which log messages should log. It will log the messages whose log level is equal or less than the defined level. This levels are:

0: no log
1: critical
2: error
3: info
4: debug

Formatter

The standard behaviour of logolang is to print log message with the following format:

[YYYY-MM-DD hh:mm:ss.nssssssss] LEVEL: MESSAGE

You may not want this, so you can set a custom formatter function. This function will receive the name of the level logged (levelName) and the message to display (msg). This function will return just a string with the formatted text.

Colors

The standard behaviour of logolang is to print log messages in the terminal coloring the level name of the logger. This uses special characters that the terminal will understand as colors, but text files and other things could not identify it as that. For that reason, you can disable it.

Writers

If you want to use a different writers for logging operations, it MUST be safe for concurrent use. You can get a thread-safe writer for an unsafe one by wrapping it in a SafeWriter. If you're not sure if your writer is safe or not, you should probably wrap it.

The custom writers MUST also be reliable for writing, because a logging operation that founds an error while writing will end up in panic.

Index

Examples

Constants

View Source
const (
	// Logger levels
	LevelNoLog = iota
	LevelCritical
	LevelError
	LevelInfo
	LevelDebug
)

Variables

View Source
var (
	// DefaultFormatter is the default function for formatting the logging messages.
	// It will format them as "[YYYY-MM-DD hh:mm:ss.nssssssss] LEVEL: MESSAGE"
	DefaultFormatter = func(levelName, msg string) string {
		now := time.Now()
		return fmt.Sprintf(
			"[%04d-%02d-%02d %02d:%02d:%02d.%09d] %s: %s",
			now.Year(), now.Month(), now.Day(),
			now.Hour(), now.Minute(), now.Second(), now.Nanosecond(),
			levelName, msg,
		)
	}

	// DefaultLogger is the default logger of the package, it's the one used when the package logging functions
	// (are used.
	DefaultLogger = NewLogger()
)

Functions

func Critical added in v1.0.0

func Critical(message string)

Critical logs a critical message using DefaultLogger when DefaultLogger.Level >= LevelCritical.

func Criticalf added in v1.0.0

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

Criticalf logs a critical message using DefaultLogger when DefaultLogger.Level >= LevelCritical. Arguments are handled in the manner of fmt.Printf.

func Debug added in v1.0.0

func Debug(message string)

Debug logs a debug message using DefaultLogger when DefaultLogger.Level >= LevelDebug.

func Debugf added in v1.0.0

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

Debug logs a debug message using DefaultLogger when DefaultLogger.Level >= LevelDebug. Arguments are handled in the manner of fmt.Printf.

func Error added in v1.0.0

func Error(message string)

Error logs an error message using DefaultLogger when DefaultLogger.Level >= LevelError.

func Errorf added in v1.0.0

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

Error logs an error message using DefaultLogger when DefaultLogger.Level >= LevelError. Arguments are handled in the manner of fmt.Printf.

func Info added in v1.0.0

func Info(message string)

Info logs an info message using DefaultLogger when DefaultLogger.Level >= LevelInfo.

func Infof added in v1.0.0

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

Info logs an info message using DefaultLogger when DefaultLogger.Level >= LevelInfo. Arguments are handled in the manner of fmt.Printf.

Types

type Logger

type Logger struct {
	// Will the logger print the level name with color?
	Color bool

	// Function for formatting the output.
	Formatter func(levelName, msg string) string

	// Logger level. It will log the levels with equal or less value.
	Level int
	// contains filtered or unexported fields
}

Logger is the type used by logolang to perform logging operations. This type is NOT thread-safe. Do not modify its content concurrently.

Example (Color)
package main

import (
	"github.com/Miguel-Dorta/logolang"
)

func main() {
	log := logolang.NewLogger()
	log.Level = logolang.LevelDebug

	// Printed with color
	log.Debug("debug test")
	log.Info("info test")
	log.Error("error test")
	log.Critical("critical test")

	log.Color = false

	// Printed with no color
	log.Debug("debug test")
	log.Info("info test")
	log.Error("error test")
	log.Critical("critical test")
}
Example (Formatter)
package main

import (
	"fmt"
	"github.com/Miguel-Dorta/logolang"
)

func main() {
	log := logolang.NewLogger()
	log.Level = logolang.LevelDebug

	// Set Formatter to be "LEVEL: MESSAGE"
	log.Formatter = func(levelName, msg string) string {
		return fmt.Sprintf("%s: %s", levelName, msg)
	}

	log.Debug("debug test")
	log.Info("info test")
	log.Error("error test")
	log.Critical("critical test")
}
Example (Level)
package main

import (
	"github.com/Miguel-Dorta/logolang"
)

func main() {
	log := logolang.NewLogger()
	log.Level = logolang.LevelInfo // Print info or lower

	log.Debug("debug test")       // Will not be printed
	log.Info("info test")         // Will be printed
	log.Error("error test")       // Will be printed
	log.Critical("critical test") // Will be printed
}

func NewLogger

func NewLogger() *Logger

NewLogger creates a new logger with the default values.

The default values are:

Color:     true
Formatter: DefaultFormatter
Level:     LevelError

Debug(f) and Info(f) will log to the os.Stdout interface. Error(f) and Critical(f) will log to the os.Stderr interface.

func NewLoggerWriters added in v0.5.0

func NewLoggerWriters(debug, info, error, critical io.Writer) *Logger

NewLoggerWriters creates a new Logger object. This function will let you define the writer for each log level. If any of the writers is set to nil, it will be assigned to its default value. It will use standard values of NewLogger.

Example
package main

import (
	"github.com/Miguel-Dorta/logolang"
	"os"
)

func main() {
	// Create output files
	outFile, err := os.Create("/tmp/out.log")
	if err != nil {
		panic(err)
	}
	defer outFile.Close()

	errFile, err := os.Create("/tmp/err.log")
	if err != nil {
		panic(err)
	}
	defer errFile.Close()

	// Create safe writers
	outSafe := &logolang.SafeWriter{W: outFile}
	errSafe := &logolang.SafeWriter{W: errFile}

	// Assign safe writers to new Logger
	log := logolang.NewLoggerWriters(outSafe, outSafe, errSafe, errSafe)
	log.Color = false // Disable color (read documentation)
	log.Level = logolang.LevelDebug

	log.Debug("debug test")
	log.Info("info test")
	log.Error("error test")
	log.Critical("critical test")
}

func (*Logger) Critical

func (l *Logger) Critical(message string)

Critical logs a critical message in the critical interface when logger Level >= LevelCritical.

func (*Logger) Criticalf

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

Criticalf logs a critical message in the critical interface when logger Level >= LevelCritical. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Debug

func (l *Logger) Debug(message string)

Debug logs a debug message in the debug interface when logger Level >= LevelDebug.

func (*Logger) Debugf

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

Debug logs a debug message in the debug interface when logger Level >= LevelDebug. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Error

func (l *Logger) Error(message string)

Error logs an error message in the error interface when logger Level >= LevelError.

func (*Logger) Errorf

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

Error logs an error message in the error interface when logger Level >= LevelError. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Info

func (l *Logger) Info(message string)

Info logs an info message in the info interface when logger Level >= LevelInfo.

func (*Logger) Infof

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

Info logs an info message in the info interface when logger Level >= LevelInfo. Arguments are handled in the manner of fmt.Printf.

type SafeWriter added in v0.4.0

type SafeWriter struct {
	W io.Writer
	// contains filtered or unexported fields
}

SafeWriter is a simple wrapper for making a writer safe for concurrent access.

Example
package main

import (
	"github.com/Miguel-Dorta/logolang"
	"os"
)

func main() {
	// Create output files
	outFile, err := os.Create("/tmp/out.log")
	if err != nil {
		panic(err)
	}
	defer outFile.Close()

	errFile, err := os.Create("/tmp/err.log")
	if err != nil {
		panic(err)
	}
	defer errFile.Close()

	// Create safe writers
	outSafe := &logolang.SafeWriter{W: outFile}
	errSafe := &logolang.SafeWriter{W: errFile}

	// Assign safe writers to new Logger
	log := logolang.NewLoggerWriters(outSafe, outSafe, errSafe, errSafe)
	log.Color = false // Disable color (read documentation)
	log.Level = logolang.LevelDebug

	log.Debug("debug test")
	log.Info("info test")
	log.Error("error test")
	log.Critical("critical test")
}

func (*SafeWriter) Write added in v0.4.0

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

Write follows the implementation of the io.Writer interface.

Jump to

Keyboard shortcuts

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