bit

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2023 License: MIT Imports: 9 Imported by: 0

README

bit - Golang log package

Features

  • levels
  • calldepth
  • sub loggers
  • structured text output

Design

----------------------------------------

                filter
			
----------------------------------------

              formatter

----------------------------------------

              io.Writer

----------------------------------------

Documentation

Overview

Example (UsingLogger)
package main

import (
	"os"

	"github.com/gregoryv/bit"
)

// remove studdering, bit.Log(bit.INFO, ...), with local levels
const (
	TRACE = bit.TRACE
	DEBUG = bit.DEBUG
	INFO  = bit.INFO
	ERROR = bit.ERROR
)

var l = bit.NewLogger()

func main() {
	// configure
	l.SetOutput(os.Stdout)
	l.SetFlags(bit.LEVEL | bit.TIME | bit.DEBUG)

	// use
	l.Log(INFO, "first")
	l.Log(DEBUG, "second",
		"color", "red",
		"ok", true,
	)
	l.Logf(ERROR, "%q", "third")

	// reconfigure showing parent directory and file
	l.SetFlags(bit.LEVEL | bit.DIR | bit.FILE | bit.INFO)
	A()

	// format as text fields
	l.SetFlags(
		bit.LEVEL | bit.FILE | bit.INFO | bit.FORMAT_TEXT_FIELDS,
	)
	l.Log(INFO, "something", "year", 2023)

	// TRACE is always shown regardless of level, good for development
	l.Log(TRACE, "wip...")
}

func A() {
	// first four bits are reserved for calldepth, ie. 1..16 levels
	l.Log(INFO|1, "called by example")
}

func init() {
	l = l.WithStaticTime() // static time only for this example
}
Output:

15:04:05 INFO first
15:04:05 DEBUG second color=red ok=true
15:04:05 ERROR "third"
INFO bit/example_test.go:34 called by example
level="INFO" source="example_test.go:40" text="something" year=2023
level="TRACE" source="example_test.go:43" text="wip..."

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	KeyWhen   = "when"
	KeyLevel  = "level"
	KeySource = "source"
	KeyPrefix = "prefix"
	KeyText   = "text"
)

Functions

This section is empty.

Types

type Bit

type Bit int
const (
	// Four first bits are used for the calldepth.
	Calldepth_    Bit = 1 << iota // calldepth
	Calldepth__                   // calldepth
	Calldepth___                  // calldepth
	Calldepth____                 // calldepth

	// What information to include.
	// timestamp related
	DATE
	TIME
	MILLISEC

	LEVEL // include level text in capital letters

	FILE // include file and line number
	PATH // include full path to file
	DIR  // include parent directory of file only

	// Format
	FORMAT_TEXT_FIELDS // output format text fields

	// levels
	TRACE     // development print tracing.
	DEBUG     // messages helpful for debugging.
	INFO      // informational messages.
	NOTICE    // normal but significant conditions.
	WARN      // warning conditions.
	ERROR     // error conditions.
	CRITICAL  // critical conditions.
	ALERT     // immediate action required.
	EMERGENCY // system is unusable.
)
const STAMP Bit = DATE | TIME | MILLISEC

type Logger

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

func NewLogger

func NewLogger() *Logger

NewLogger returns a logger that outputs to os.Stderr using level INFO and including a timestamp.

func (*Logger) Flags added in v0.4.0

func (l *Logger) Flags() Bit

Flags returns current flags.

func (*Logger) Log

func (l *Logger) Log(bits Bit, msg any, fields ...any)

Log outputs the given message and fields using bit levels, e.g. INFO or DEBUG|2 for calldepth 2.

func (*Logger) Logf

func (l *Logger) Logf(bits Bit, format string, args ...any)

Logf formats the given message using bit flags, e.g. INFO or DEBUG|2 for calldepth 2.

func (*Logger) SetFlags

func (l *Logger) SetFlags(bits Bit)

SetFlags overrides current flags.

func (*Logger) SetOutput

func (l *Logger) SetOutput(v io.Writer)

SetOutput sets the destination writer for this logger.

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(v string)

SetPrefix sets the prefix to include, empty to skip.

func (*Logger) Sub

func (l *Logger) Sub(prefix string) *SubLogger

Sub creates a sub logger with a new prefix.

Example
l := NewLogger()
l.SetOutput(os.Stdout)
l.SetFlags(LEVEL | INFO)
s := l.Sub("car")
s.Log(INFO, "brm brm")
Output:

INFO car brm brm

func (*Logger) WithStaticTime added in v0.6.0

func (l *Logger) WithStaticTime() *Logger

Use only for testing log output

type SubLogger added in v0.5.0

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

func (*SubLogger) Log added in v0.5.0

func (s *SubLogger) Log(bits Bit, msg any, fields ...any)

func (*SubLogger) Logf added in v0.5.0

func (s *SubLogger) Logf(bits Bit, format string, args ...any)

Jump to

Keyboard shortcuts

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