logger

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

README

simplelog

simplelog provides the logger package, a simple logger with level and time information.

Usage

package main

import "git.brokenmouse.studio/bms/go-simplelog"

func main() {
	logger.Info("0x%02X", 10)
	logger.Debug("Test")
	logger.Warn("This is weird...")
	logger.Error("Something went wrong!")
	logger.Fatal("Can't continue, everything is broken!")
}

Result:

[12:00:00/INFO] 0x0A
[12:00:00/DEBUG] Test
[12:00:00/WARN] This is weird...
[12:00:00/ERROR] Something went wrong!
[12:00:00/FATAL] Can't continue, everything is broken!
exit status 1
Debug Messages

Debug messages are only logged when LogDebug is true (the default).

package main

import (
	"flag"

	"git.brokenmouse.studio/bms/go-simplelog"
)

func main() {
	verboseFlag := flag.Bool("v", false, "Increase verbosity.")
	flag.Parse()
	logger.LogDebug = *verboseFlag

	logger.Debug("Building index ...")
}

Result:

The message is only logged when the -v flag is specified.

[12:00:00/DEBUG] Building index ...
Fatal Errors

When logging a fatal error, the program will exit with status code 1.

logger.Fatal("Failed to initialize!")

Result:

[12:00:00/FATAL] Failed to initialize!
exit status 1
Writing to files

Messages can additionally be logged to a file or another io.StringWriter by setting LogWriter.

package main

import (
	"os"

	"git.brokenmouse.studio/bms/go-simplelog"
)

func main() {
	logger.LogWriter, _ = os.OpenFile("simplelog.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0660)

	logger.Info("Read me later...")
}

Result:

The message will appear in standard output and a new file named simplelog.log.

[12:00:00/INFO] Read me later...

Documentation

Overview

Simple logger with level and time information. Example: "[12:00:00/INFO] simplelog!"

Index

Constants

This section is empty.

Variables

View Source
var LogDebug = true

Whether to log debug messages.

View Source
var LogToStd = true

Whether to log messages to standard output and standard error.

View Source
var LogWriter io.StringWriter

A StringWriter to log all messages to. When this is nil, messages will only be written to standard output and standard error.

Functions

func Debug

func Debug(format string, a ...any)

Logs a debug message if LogDebug is true.

func Error

func Error(format string, a ...any)

Logs an error message.

func Fatal

func Fatal(format string, a ...any)

Logs a fatal error message and exits with status code 1.

func Info

func Info(format string, a ...any)

Logs an informational message.

func Log

func Log(w io.Writer, level string, format string, a []any)

Logs a message to io.Writer *w* with the current time and the level *level*.

func Warn

func Warn(format string, a ...any)

Logs a warning message.

Types

This section is empty.

Jump to

Keyboard shortcuts

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