log

package module
v1.8.8 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2025 License: GPL-3.0 Imports: 8 Imported by: 25

README

log

Yum

Go Report Card GitHub Workflow Status License

What is this?

A simple, extensible logger package. Maybe you want a logger that logs to a file and also logs to a websocket. Maybe you want to log everything to a file but only portions of certain messages to STDOUT. Maybe you don't want to log to STDOUT at all, but rather to a TUI. Using the CustomLogHandler functionality of this package, you can do anything you want.

How to install

Open a terminal and run the following:

$ go get -u github.com/mjwhitta/log

Usage

package main

import (
    "fmt"

    "github.com/mjwhitta/log"
)

var logger *log.Messenger

func main() {
    var e error

    // Default log functionality (stdout w/o timestamp)
    log.Debug("Debug message")
    log.Info("Info message")
    log.SubInfo("SubInfo message")
    log.Good("Good message")
    log.Warn("Warn message")
    log.Err("Error message")

    // Default log functionality + timestamp
    log.Timestamp = true
    log.Debug("Debug message")
    log.Info("Info message")
    log.SubInfo("SubInfo message")
    log.Good("Good message")
    log.Warn("Warn message")
    log.Err("Error message")

    // Will log to stdout (w/o timestamp)
    logger = log.NewMessenger()
    logger.Debug("Debug message")
    logger.Info("Info message")
    logger.SubInfo("SubInfo message")
    logger.Good("Good message")
    logger.Warn("Warn message")
    logger.Err("Error message")

    // Will now log to stdout (w/o timestamp) and file (w/ timestamp)
    if logger, e = log.NewFileMessenger("/tmp/test.log"); e != nil {
        panic(e)
    }

    logger.Debug("Debug message")
    logger.Info("Info message")
    logger.SubInfo("SubInfo message")
    logger.Good("Good message")
    logger.Warn("Warn message")
    logger.Err("Error message")

    // Will now log to stdout (w/ timestamp) and file (w/ timestamp)
    log.Prefixes[log.TypeDebug] = "[DEBU]"
    log.Prefixes[log.TypeErr] = "[ERRO]"
    log.Prefixes[log.TypeGood] = "[GOOD]"
    log.Prefixes[log.TypeInfo] = "[INFO]"
    log.Prefixes[log.TypeSubInfo] = "[SUBI]"
    log.Prefixes[log.TypeWarn] = "[WARN]"
    logger.Timestamp = true
    logger.Debug("Debug message")
    logger.Info("Info message")
    logger.SubInfo("SubInfo message")
    logger.Good("Good message")
    logger.Warn("Warn message")
    logger.Err("Error message")

    // Disable color on stdout
    logger.SetColor(false)
    logger.Debug("Debug message")
    logger.Info("Info message")
    logger.SubInfo("SubInfo message")
    logger.Good("Good message")
    logger.Warn("Warn message")
    logger.Err("Error message")

    // Custom MsgHandler
    logger.SetMsgHandler(
        func(msg *log.Message) error {
            switch msg.Type {
            case log.TypeDebug:
                fmt.Println("Custom 1 - debug")
            case log.TypeErr, log.TypeErrX:
                fmt.Println("Custom 1 - error")
            case log.TypeGood:
                fmt.Println("Custom 1 - good")
            case log.TypeInfo:
                fmt.Println("Custom 1 - info")
            case log.TypeMsg:
                fmt.Println("Custom 1 - message")
            case log.TypeSubInfo:
                fmt.Println("Custom 1 - additional info")
            case log.TypeWarn:
                fmt.Println("Custom 1 - warning")
            }
            return nil
        },
    )
    logger.AddMsgHandler(
        func(msg *log.Message) error {
            fmt.Println("Custom 2")
            return nil
        },
    )
    logger.Debug("Debug message")
    logger.Info("Info message")
    logger.SubInfo("SubInfo message")
    logger.Good("Good message")
    logger.Warn("Warn message")
    logger.Err("Error message")

    // Close logger
    logger.AddCloseHandler(
        func() error {
            fmt.Println("Closed")
            return nil
        },
    )
    if e = logger.Close(); e != nil {
        panic(e)
    }
}

Documentation

Index

Constants

View Source
const (
	TypeDebug uint64 = iota
	TypeErr
	TypeErrX // An error message that will also exit
	TypeGood
	TypeInfo
	TypeMsg // Generic message
	TypeSubInfo
	TypeWarn
)

Consts for log message types

View Source
const Version string = "1.8.8"

Version is the package version.

Variables

View Source
var (
	// Prefixes allows you to customize each log message prefix.
	Prefixes map[uint64]string = map[uint64]string{
		TypeDebug:   "[#]",
		TypeErr:     "[!]",
		TypeErrX:    "[!]",
		TypeGood:    "[+]",
		TypeInfo:    "[*]",
		TypeSubInfo: "[=]",
		TypeWarn:    "[-]",
	}

	// Timestamp is used to determine whether a timestamp is printed
	// to stdout with the message.
	Timestamp bool
)

Functions

func Debug

func Debug(msg string)

Debug will log a debug message.

func Debugf

func Debugf(format string, args ...any)

Debugf will log a debug message using a format string.

func Err

func Err(msg string)

Err will log an error message.

func ErrX

func ErrX(code int, msg string)

ErrX will log an error message and exit.

func ErrXf added in v1.7.0

func ErrXf(code int, format string, args ...any)

ErrXf will log an error message using a format string and exit.

func Errf

func Errf(format string, args ...any)

Errf will log an error message using a format string.

func Good

func Good(msg string)

Good will log a good message.

func Goodf

func Goodf(format string, args ...any)

Goodf will log a good message using a format string.

func Info

func Info(msg string)

Info will log an info message.

func Infof

func Infof(format string, args ...any)

Infof will log an info message using a format string.

func Log added in v1.8.0

func Log(msg *Message)

Log allows for logging of custom message types. If you aren't using a custom Messenger, you probably just want to use log.Msg(...) instead.

func Msg

func Msg(msg string)

Msg will log a message as is.

func Msgf

func Msgf(format string, args ...any)

Msgf will log a message as is using a format string.

func SetColor

func SetColor(enabled bool)

SetColor will disable/enable colors for stdout.

func SubInfo

func SubInfo(msg string)

SubInfo will log a subinfo message.

func SubInfof

func SubInfof(format string, args ...any)

SubInfof will log a subinfo message using a format string.

func Warn

func Warn(msg string)

Warn will log a warn message.

func Warnf

func Warnf(format string, args ...any)

Warnf will log a warn message using a format string.

Types

type CloseHandler

type CloseHandler func() error

CloseHandler is a function pointer. CloseHandlers are called when the Messengers is closed and allow for closing of files or sockets.

type Message

type Message struct {
	Discard bool
	Raw     string
	Type    uint64
	// contains filtered or unexported fields
}

Message is struct containing all message related data.

func NewMessage

func NewMessage(msgType uint64, msg string) *Message

NewMessage will return a new Message instance.

func (*Message) Preprocessed

func (m *Message) Preprocessed() string

Preprocessed will return the preprocessed message text.

func (*Message) RawString

func (m *Message) RawString() string

RawString will return a raw string representation of the Message.

func (*Message) String

func (m *Message) String() string

String will return a string representation of the Message.

func (*Message) Text

func (m *Message) Text() string

Text will return the processed message text (w/ no timestamp)

func (*Message) Timestamp

func (m *Message) Timestamp() string

Timestamp will return the timestamp of the message.

type Messenger

type Messenger struct {
	Stdout    bool
	Timestamp bool
	// contains filtered or unexported fields
}

Messenger will log to STDOUT as well as call a custom log handlers defined by the user. If Timestamp is true, then messages are prepended with an RFC3339 timestamp.

func NewFileMessenger

func NewFileMessenger(fn string, ts ...bool) (*Messenger, error)

NewFileMessenger will return a new Messenger instance for logging to a file. The log file will always show the timestamp, but STDOUT will only show the timestamp if Timestamp is true.

func NewMessenger

func NewMessenger(ts ...bool) *Messenger

NewMessenger will return a new Messenger instance for logging.

func (*Messenger) AddCloseHandler

func (m *Messenger) AddCloseHandler(handler CloseHandler)

AddCloseHandler will add a handler for custom actions when the Messenger instance is closed.

func (*Messenger) AddMsgHandler

func (m *Messenger) AddMsgHandler(handler MsgHandler)

AddMsgHandler will add a handler for custom actions when the Messenger logs a message.

func (*Messenger) Close

func (m *Messenger) Close() error

Close will call the close handler.

func (*Messenger) Debug

func (m *Messenger) Debug(msg string) error

Debug will log a debug message.

func (*Messenger) Debugf

func (m *Messenger) Debugf(format string, args ...any) error

Debugf will log a debug message using a format string.

func (*Messenger) Err

func (m *Messenger) Err(msg string) error

Err will log an error message.

func (*Messenger) ErrX

func (m *Messenger) ErrX(code int, msg string)

ErrX will log an error message and exit.

func (*Messenger) ErrXf added in v1.7.0

func (m *Messenger) ErrXf(code int, format string, args ...any)

ErrXf will log an error message using a format string and exit.

func (*Messenger) Errf

func (m *Messenger) Errf(format string, args ...any) error

Errf will log an error message using a format string.

func (*Messenger) Good

func (m *Messenger) Good(msg string) error

Good will log a good message.

func (*Messenger) Goodf

func (m *Messenger) Goodf(format string, args ...any) error

Goodf will log a good message using a format string.

func (*Messenger) Info

func (m *Messenger) Info(msg string) error

Info will log an info message.

func (*Messenger) Infof

func (m *Messenger) Infof(format string, args ...any) error

Infof will log an info message using a format string.

func (*Messenger) Log added in v1.8.0

func (m *Messenger) Log(msg *Message) error

Log allows for logging of custom message types.

func (*Messenger) Msg

func (m *Messenger) Msg(msg string) error

Msg will log a message as is.

func (*Messenger) Msgf

func (m *Messenger) Msgf(format string, args ...any) error

Msgf will log a message as is using a format string.

func (*Messenger) SetCloseHandler

func (m *Messenger) SetCloseHandler(handler CloseHandler)

SetCloseHandler will set the handler for custom actions when the Messenger instance is closed.

func (*Messenger) SetColor

func (m *Messenger) SetColor(enabled bool)

SetColor will disable/enable colors for STDOUT.

func (*Messenger) SetMsgHandler

func (m *Messenger) SetMsgHandler(handler MsgHandler)

SetMsgHandler will set the handler for custom actions when the Messenger logs a message.

func (*Messenger) SetPreprocessor

func (m *Messenger) SetPreprocessor(handler Preprocessor)

SetPreprocessor will set the handler for preprocessing messages when the Messenger logs a message.

func (*Messenger) SubInfo

func (m *Messenger) SubInfo(msg string) error

SubInfo will log a subinfo message.

func (*Messenger) SubInfof

func (m *Messenger) SubInfof(format string, args ...any) error

SubInfof will log a subinfo message using a format string.

func (*Messenger) Warn

func (m *Messenger) Warn(msg string) error

Warn will log a warn message.

func (*Messenger) Warnf

func (m *Messenger) Warnf(format string, args ...any) error

Warnf will log a warn message using a format string.

type MsgHandler

type MsgHandler func(msg *Message) error

MsgHandler is a function pointer. MsgHandlers are called when a message is logged and allow for custom actions like writing to a file or a socket.

type Preprocessor

type Preprocessor func(msg *Message)

Preprocessor is a function pointer. The Preprocessor is called before the message is logged and allows for reformatting of messages such as JSON. Set the Message's Discard field to true to drop messages.

Jump to

Keyboard shortcuts

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