log

package module
v1.6.12 Latest Latest
Warning

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

Go to latest
Published: May 5, 2025 License: GPL-3.0 Imports: 5 Imported by: 24

README

log

Yum

Go Report Card 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 (
    hl "github.com/mjwhitta/hilighter"
    "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.Good("Good message")
    log.Err("Error message")

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

    // Will log to stdout (w/o timestamp)
    logger = log.NewMessenger()
    logger.Info("Info message")
    logger.Good("Good 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.Info("Info message")
    logger.Good("Good message")
    logger.Err("Error message")

    // Will now log to stdout (w/ timestamp) and file (w/ timestamp)
    logger.Timestamp = true
    logger.Info("Info message")
    logger.Good("Good message")
    logger.Err("Error message")

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

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

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

Documentation

Index

Constants

View Source
const (
	TypeDebug   = iota // TypeDebug is a debug message
	TypeErr            // TypeErr is an error message
	TypeErrX           // TypeErrX is an error message that will exit
	TypeGood           // TypeGood is a success message
	TypeInfo           // TypeInfo is an informative message
	TypeMsg            // TypeMsg is a generic/plain message
	TypeSubInfo        // TypeSubInfo is an additional info message
	TypeWarn           // TypeWarn is a warning message
)

Consts for log message types

View Source
const Version string = "1.6.12"

Version is the package version.

Variables

View Source
var Timestamp bool

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

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 Errf

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

Errf will log an error message using a format string.

func ErrfX

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

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

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 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 uint8
	// contains filtered or unexported fields
}

Message is struct containing all message related data.

func NewMessage

func NewMessage(msgType uint8, msg string) (m *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) Errf

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

Errf will log an error message using a format string.

func (*Messenger) ErrfX

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

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

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) 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 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