msg

package module
v0.0.0-...-63d8b38 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2022 License: BSD-3-Clause Imports: 8 Imported by: 2

README

EXAMPLE
-------

package main

import (
        "codeberg.org/ac/msg"
)

func main() {
        m := make(map[msg.Level]string)
        m[msg.Verbose] = "TestPrefix: "

        c := msg.New(msg.V, m, nil) // create a config and receive a pointer to it

        //c.Println(msg.E, m) // usage almost as simple as fmt.Println()
        // You save all the boilerplate eg. if debug ... and so on ...

        c.Println("essential")             // will be printed since it's included in the msg.Verbose Level that was passed to New
        c.Debug().Println("debug")         // will NOT be printed since msg.Debug is on a higher Level than msg.Verbose
        c.Verbose().Println("verbose")     // is printed as it matches the set Level

        c.E().Println("error")             // is printed to stderr, since Essential is included in the set level

        debug1(*c)
        c.D().Println("debug1")        // will NOT be printed since msg.Debug is on a higher Level than msg.Verbose

        debug2(c)                      // if you pass the pointer ¹
        c.D().Println("debug2")        // will be printed since c.Setting is = msg.Debug now
}

func debug1(c msg.Config) {
        // modifies a copy
        c.Setting = msg.Debug
        c.D().Println("debug") // will be printed
}

func debug2(c *msg.Config) {
        // modifies the Setting through the pointer and it persists
        c.Setting = msg.Debug
        c.D().Println("debug") // will be printed
}

OUTPUT
------

essential
TestPrefix: verbose
[Error] error
[Debug] debug
[Debug] debug
[Debug] debug2

Documentation

Overview

msg - Is a library addition to the fmt package and for application output purposes. It enables you to categorize your output as well as modify it by category through prefix and suffix additions on a per Level basis.

Different levels (Common/Verbose/Debug) are set by default and can be extended and customized.

It's exclusive focus is on printing messages to io.Writer's (mainly stdout/stderr).

c := msg.New(msg.V, nil, nil)
c.Error().Printf("%v\t\t%v\n", "hello", "world!")

D(), V() and E() are shortcuts for Debug(), Verbose() and Error() Levels respectively.

For comparision:

c := msg.New(msg.Common, nil, nil)

c.Println("hello world")
fmt.Println("hello world") // equivalent

c.E().Println("error world")
fmt.Fprintln(os.Stderr, "error world") // equivalent

The greatest strength is to enable or disable verbosity up to debug levels or even shut it down with only one setting at all times.

c.Setting = msg.Debug

All you have to do is pass that returning pointer of New(Level) around to your functions. This enables you to write debug or verbose messages whereever you want.

Index

Constants

View Source
const (
	// a special Level - no output at all
	Silent Level = iota
	// Common Level / default
	Common
	// dedicated Levels
	Verbose = 128
	Debug   = 254
	// print only errors, but all errors
	ErrorsOnly = 255
)
View Source
const (
	D = Debug
	E = ErrorsOnly
	C = Common
	S = Silent
	V = Verbose
)

Variables

View Source
var (
	Stdout      = os.Stdout
	Stderr      = os.Stderr
	PrefixDebug = "[Debug] "
	PrefixError = "[Error] "
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Setting Level
	// contains filtered or unexported fields
}

func New

func New(setting Level) *Config

New provides a configuration element for printing messages with regard to the set Level.

func (*Config) Custom

func (c *Config) Custom(prefixes, suffixes map[Level]string)

Custom provides a method to add pre-/suffixes.

func (*Config) D

func (c *Config) D() *Config

D is a shortcut for Debug().

func (*Config) Debug

func (c *Config) Debug() *Config

Debug sets the runlevel to Level Debug (255)

func (*Config) E

func (c *Config) E() *Config

E is a shortcut for Err().

func (*Config) Error

func (c *Config) Error() *Config

Err enables a shift to write to c.err / set standard error.

func (*Config) L

func (c *Config) L(runlevel Level) *Config

L is a shortcut for c.Level(runlevel).

func (*Config) Level

func (c *Config) Level(runlevel Level) *Config

Level takes a Level as runlevel to set output situations by the given Level.

func (*Config) Logger

func (c *Config) Logger(level Level, name string, err bool) *log.Logger

Logger enables you to handle the logged output through the msg.Config type. You have to pre-decide if it's handled like an error or normal log value and on which Level.

func (*Config) Print

func (c *Config) Print(msg ...interface{}) (n int, err error)

Print writes to c.out or c.err depending on c.shift, if the given Level is set.

func (*Config) Printf

func (c *Config) Printf(format string, msg ...interface{}) (n int, err error)

Printf formats according to a format specifier and writes to c.out or c.err depending on c.shift, if the given Level is set.

func (*Config) Println

func (c *Config) Println(msg ...interface{}) (n int, err error)

Println writes to c.out or c.err depending on c.shift and appends a newline on msg, if the given Level is set.

func (*Config) TimestampDisable

func (c *Config) TimestampDisable()

TimestampDisable deactivates all timestamps.

func (*Config) TimestampEnable

func (c *Config) TimestampEnable(format string)

TimestampEnable activates timestamps for all output including logging to the format or defaults to "2006-01-02|15:04:05|-0700[MST]|".

func (*Config) V

func (c *Config) V() *Config

V is a shortcut for Verbose().

func (*Config) Verbose

func (c *Config) Verbose() *Config

Verbose sets the runlevel to Level Verbose (128).

type Level

type Level uint8

type Output

type Output func(w io.Writer, format string, msg ...interface{}) (n int, err error)
var DefaultOutput Output = p

Jump to

Keyboard shortcuts

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