logger

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2022 License: Apache-2.0 Imports: 4 Imported by: 68

README

Logger

Logger package implements logging methods with formatted output and more detailed logging information than provided in the standard logging package of golang. It is possible to specify the level of logging information to be written as output.

Usage

setLevel(level LogLevel)

sets the level of logging information written as output.

There are four possible values for argument level of type LogLevel in ascending info order:

  1. LEVEL_ERROR: Only Error logs are written to stderr.
  2. LEVEL_WARNING: Warnings and errors are written to stderr.
  3. LEVEL_INFO: Errors and warnings are written to stderr, infos to stdout.
  4. LEVEL_DEBUG: Errors and warnings are written to stderr, infos and debugs to stdout.

To print logging information you can choose one of the following *log.Logger variables chaining to common I/O writers:

Error
Warning
Info
Debug

The following Log flags are set:

log.Ldate: the date in the local time zone
log.Ltime: the time in the local time zone
log.Lshortfile: final file name element and line number
log.Lmsgprefix: move the "prefix" log level tag from the beginning of the line to before the message

Examples

logger.SetLevel(logger.LEVEL_WARNING)
suffix := 2
logger.Error.Printf("error %d", suffix)
logger.Warning.Printf("warning  %d", suffix)
logger.Info.Printf("info  %d", suffix)
logger.Debug.Printf("debug  %d", suffix)

has the following output:

2021/10/07 12:18:12 main.go:16: [ERROR]: error 2
2021/10/07 12:18:12 main.go:17: [WARNING]: warning  2
logger.SetLevel(logger.LEVEL_DEBUG)
suffix := 4
logger.Error.Printf("error %d", suffix)
logger.Warning.Printf("warning  %d", suffix)
logger.Info.Printf("info  %d", suffix)
logger.Debug.Printf("debug  %d", suffix)

prints all logging information:

2021/10/07 12:18:12 main.go:32: [ERROR]: error 4
2021/10/07 12:18:12 main.go:33: [WARNING]: warning  4
2021/10/07 12:18:12 main.go:34: [INFO]: info  4
2021/10/07 12:18:12 main.go:35: [DEBUG]: debug  4

Tests

To perform the unit tests, you can call from the root folder:

go test

To run the example:

go run example/main.go

License

Apache License 2.0

Documentation

Overview

Package logger implements logging methods with formatted output and more detailed logging information than provided in the standard logging package of golang. It is possible to specify the level of logging information to be written as output (Default: all information is written). Use the `*log.Logger` variables and chain them to common I/O writers for printing logging information.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Debug = getDebugLogger()
View Source
var Error = getErrorLogger()
View Source
var Info = getInfoLogger()
View Source
var Warning = getWarningLogger()

Functions

func SetLevel

func SetLevel(level LogLevel)

SetLevel sets the level of logging information written as output.

Example
package main

import (
	"github.com/coneno/logger"
)

func main() {
	logger.SetLevel(logger.LEVEL_DEBUG)
	logger.Error.Printf("error")
	logger.Warning.Printf("warning")
	logger.Debug.Printf("debug")
	logger.Info.Printf("info")
}
Output:

Types

type LogLevel

type LogLevel int

LogLevel indicates the level of logging information passed as argument to SetLevel function.

const (
	LEVEL_ERROR LogLevel = iota
	LEVEL_WARNING
	LEVEL_INFO
	LEVEL_DEBUG
)

There are four possible values for type `LogLevel`:

1. `LEVEL_ERROR`: Only Error logs are written to `stderr`.

2. `LEVEL_WARNING`: Warnings and errors are written to `stderr`.

3. `LEVEL_INFO`: Errors and warnings are written to `stderr`, infos to `stdout`.

4. `LEVEL_DEBUG`: Errors and warnings are written to `stderr`, infos and debugs to `stdout`.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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