logolang

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2019 License: MIT Imports: 7 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 "github.com/Miguel-Dorta/logolang"

func main() {
	log, err := logolang.NewLogger(true, "", logolang.LevelDebug, nil, nil, nil, nil)
	if err != nil {
		panic(err)
	}
	
	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
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.

Format

You can set the way your message is logged by setting a custom format. The format is defined by a string where the following sequences are given the following values:

  • %YYYY% = current year
  • %MM% = current month
  • %DD% = current day of the month
  • %hh% = current hour
  • %mm% = current minute
  • %ss% = current second
  • %ns% = current nanosecond
  • %LEVEL% = level name (DEBUG, INFO, ERROR or CRITICAL)
  • %MESSAGE% = message logged

The default format is:

  • DefaultFormat = "[%YYYY%-%MM%-%DD% %hh%:%mm%:%ss%] %LEVEL%: %MESSAGE%"
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 color when using NewLogger.

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

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.

Format

You can set the way your message is logged by setting a custom format. The format is defined by a string where the following sequences are given the following values:

%YYYY%    = current year
%MM%      = current month
%DD%      = current day of the month
%hh%      = current hour
%mm%      = current minute
%ss%      = current second
%ns%      = current nanosecond
%LEVEL%   = level name (DEBUG, INFO, ERROR or CRITICAL)
%MESSAGE% = message logged

The default format is:

DefaultFormat  = "[%YYYY%-%MM%-%DD% %hh%:%mm%:%ss%] %LEVEL%: %MESSAGE%"

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 color when using NewLogger.

Index

Constants

View Source
const (
	// Logger levels
	LevelNoLog = iota
	LevelCritical
	LevelError
	LevelInfo
	LevelDebug
)
View Source
const (
	// Default format for logolang
	DefaultFormat = "[%YYYY%-%MM%-%DD% %hh%:%mm%:%ss%] %LEVEL%: %MESSAGE%"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is the type used by logolang to perform logging operations. .

func NewLogger

func NewLogger(color bool, format string, level int, debug, info, error, critical io.Writer) (*Logger, error)

NewLogger creates a new Logger object. It's the function that lets you define every aspect of the new logger. It lets you define whether you want the output to be printed with colors, the format you want the messages to be logged, the logger level, and the writers where you want to print your log.

If the format provided is a empty string, it will be set to DefaultFormat. If any of the writers provided is nil, it will be assigned to its default value.

Default values:

debug:    os.Stdout
info:     os.Stdout
error:    os.Stderr
critical: os.Stderr

func NewLoggerStandard added in v0.4.0

func NewLoggerStandard() *Logger

NewLoggerStandard creates a new logger with the default values.

The default values are:

color:    true
format:   DefaultFormat
level:    LevelError
debug:    os.Stdout
info:     os.Stdout
error:    os.Stderr
critical: os.Stderr

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.

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