log

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 8 Imported by: 0

README

A nicer command line logger for GO.

This library provides a nice and convenient way for GO application developers to handle log printing in their applications. It will try to print colorful logs if allowed by default, allowing you to disable this manually where it fails.

You install the library by:

go get github.com/echsylon/go-log

Key features

  • Option to log in all Unix Syslog categories (EMERGENCY, ALERT, CRITICAL, ERROR, WARNING, NOTICE, INFORMATIONAL, DEBUG) and additionally also TRACE.
  • Configurable log details (supports injecting custom details).
  • Color coded output.

A concrete example

Below example code:

package main

import (
	"fmt"
	"math/rand"

	"github.com/echsylon/go-log"
)

func main() {
	// Configure log environment
	log.SetLogLevel(log.LOG_LEVEL_TRACE)
	log.SetLogColumnSeparator(" : ")
	log.SetLogColumns(
		customLogColumn,
		log.LOG_COLUMN_DATETIME,
		log.LOG_COLUMN_SOURCE,
		log.LOG_COLUMN_LEVEL,
	)

	// Print standard Syslog categories
	log.Emergency("Emergency")
	log.Alert("Alert")
	log.Critical("Critical")
	log.Error("Error")
	log.Warning("Warning")
	log.Notice("Notice")
	log.Information("Information")
	log.Debug("Debug")

	// Print debug trace category.
	log.Trace("Trace")
}

func customLogColumn() string {
	// Produce custom log information on the form "X-nnn",
	// where "nnn" is a random integer between 0 and 1000.
	n := rand.Intn(1000)
	return fmt.Sprintf("X-%03d", n)
}

...would output something like this (Ubuntu 23.10):

Terminal output

Documentation

Index

Constants

View Source
const (
	LOG_LEVEL_EMERGENCY = iota
	LOG_LEVEL_ALERT
	LOG_LEVEL_CRITICAL
	LOG_LEVEL_ERROR
	LOG_LEVEL_WARNING
	LOG_LEVEL_NOTICE
	LOG_LEVEL_INFORMATIONAL
	LOG_LEVEL_DEBUG
	LOG_LEVEL_TRACE
)

The Unix Syslog log levels complemented with a custom "trace" level.

View Source
const (
	LOG_COLUMN_DATETIME = iota
	LOG_COLUMN_PID
	LOG_COLUMN_SOURCE
	LOG_COLUMN_LEVEL
)

The default types of information the caller can configure the logger to print along with each log message. See "SetLogColumns" for details.

Variables

This section is empty.

Functions

func Alert

func Alert(message string, args ...any)

Alert prints a log message for situations where an action must be taken immediately, say, for example, due to a corrup database. This function will not "panic()", allowing the process to do additional logging.

func Critical

func Critical(message string, args ...any)

Critical prints a log message for critical errors, such as hardware device errors. This function will not "panic()", allowing the process to do additional logging.

func Debug

func Debug(message string, args ...any)

Debug prints a log message which contains information that is normally useful only when debugging a program, for example an intermediate value of local variable.

func DisableColorOutput

func DisableColorOutput()

DisableColorOutput prevents the logs from sending color control characters to the output stream.

Color output is usually enabled by default, unless it is actively disabled by FORCE_COLOR or NO_COLOR environment variables. This logger honors said variables without the caller having to explicitly call this method.

Furthermore, this logger will also disable color output when log output is redirected (through any of the ">" directives) to a different destination than the terminal.

func Emergency

func Emergency(message string, args ...any)

Emergency prints a panic mode log message. These logs are usually printed when the system is unusable due to a panic situation. This function will not actually "panic()", allowing the process to do additional logging or tombstone aggregation.

func Error

func Error(message string, args ...any)

Error prints a log message for serious errors that may cause a degrade in functionality or even data loss without having to halt execution, for example a full disk. This function will not "panic()", allowing the process to do additional logging.

func Information

func Information(message string, args ...any)

Information prints a log message that confirms expected execution, for example when data has been successfully saved.

func Notice

func Notice(message string, args ...any)

Notice prints a log message for situations that are normal but needs special handling, for example when requesting a network resource that is temporarily unavailable.

func SetLogColumnSeparator

func SetLogColumnSeparator(separator string)

SetLogColumnSeparator defines how the log info columns are separated. If the logs are to be machine processed at a later time, this function enables producing, say, a CSV file by setting the separator ";" or "\t".

func SetLogColumns

func SetLogColumns(columns ...any)

SetLogColumns will alter the information being printed before each log message. This function takes either a "LOG_COLUMN_..." constant that describes one of the standard log parameters, or a function that takes no arguments but produces a string "func() string" in order to log custom information.

func SetLogLevel

func SetLogLevel(level int)

SetLogLevel constrains which logs to print. All log categories associated with a log level lower than or equal to the value being defined here will be sent to the output streams.

func Trace

func Trace(message string, args ...any)

Trace prints a debug log message that gives information of the execution flow during debugging. This is useful when trying to debug exactly which logical branch the logic is following.

func Warning

func Warning(message string, args ...any)

Warning prints a log message for issues that are not necessarily errors in them selves, but indicates other logical issues, for example when trying to remove an item from an empty list.

Types

This section is empty.

Jump to

Keyboard shortcuts

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