logger

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: MIT Imports: 3 Imported by: 2

README

slogger: simple go logger

This is a small go package that provides a few simple logging functions to aid in development. This package allows you to register a new logger very similar to how you would a standard go log.Logger, with the added ability to pass paremeters to configure a log color (using ANSI color codes) and some log prefixese.

pkg.go.dev docs page

install in project

Using go modules, from your project directory you can run:

go get github.com/jesse-rb/slogger-go 

example usage

package main

import (
	slogger "github.com/jesse-rb/slogger-go"
    log
    os
)

func main() {
    // Declare some loggers
    infoLogger := slogger.New(os.Stdout, slogger.ANSIBlue, "info", log.Lshortfile+log.Ldate);
    errorLogger := slogger.New(os.Stderr, slogger.ANSIRed, "error", log.Lshortfile+log.Ldate);

    // Log some things
    infoLogger.Log("main", "Something worth noting happened", 2+4)
    errorLogger.Log("main", "Some horrible error happened", []int{3, 5, 7})
}

preview logs

preview image A

References

Referenced 3 bit 8 colours from Haoyi's Programming Blog as most terminals should supprot this

const (
    ANSIReset    = "\u001b[0m"
    ANSIRed      = "\u001b[31m"
    ANSIBlue     = "\u001b[34m"
    ANSIBlack    = "\u001b[30m"
    ANSIGreen    = "\u001b[32m"
    ANSIYellow   = "\u001b[33m"
    ANSIMagenta  = "\u001b[35m"
    ANSICyan     = "\u001b[36m"
    ANSIWhite    = "\u001b[37m"
)

Documentation

Index

Constants

View Source
const (
	ANSIReset   = "\u001b[0m"
	ANSIRed     = "\u001b[31m"
	ANSIBlue    = "\u001b[34m"
	ANSIBlack   = "\u001b[30m"
	ANSIGreen   = "\u001b[32m"
	ANSIYellow  = "\u001b[33m"
	ANSIMagenta = "\u001b[35m"
	ANSICyan    = "\u001b[36m"
	ANSIWhite   = "\u001b[37m"
)

3 bit color ansi color codes for colored logging output srouce: https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html#8-colors

Variables

View Source
var GlobalPrefix = "project"

Init logger global prefix

Functions

This section is empty.

Types

type Logger added in v1.0.0

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

Contains a standard golang log.logger for this package to exectute it's functions

func New added in v1.0.0

func New(out io.Writer, color string, localPrefix string, flag int) *Logger

Create a new logger similr to how you would create a default go log.Logger with log.New()

func (*Logger) Log added in v1.0.0

func (l *Logger) Log(tag string, msg string, data interface{})

Log something, you can provide: a tag e.g. function name, a msg e.g. info about something that happened, a data interface, e.g. []int{3, 5, 6} OR 4+2

func (*Logger) SetColor added in v1.0.1

func (l *Logger) SetColor(color string)

Set color of logger

func (*Logger) SetLocalPrefix added in v1.0.1

func (l *Logger) SetLocalPrefix(localPrefix string)

Set local prefix of logger

Jump to

Keyboard shortcuts

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