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 ¶
- Variables
- type Bit
- type Logger
- func (l *Logger) Flags() Bit
- func (l *Logger) Log(bits Bit, msg any, fields ...any)
- func (l *Logger) Logf(bits Bit, format string, args ...any)
- func (l *Logger) SetFlags(bits Bit)
- func (l *Logger) SetOutput(v io.Writer)
- func (l *Logger) SetPrefix(v string)
- func (l *Logger) Sub(prefix string) *SubLogger
- func (l *Logger) WithStaticTime() *Logger
- type SubLogger
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. )
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) Log ¶
Log outputs the given message and fields using bit levels, e.g. INFO or DEBUG|2 for calldepth 2.
func (*Logger) Logf ¶
Logf formats the given message using bit flags, e.g. INFO or DEBUG|2 for calldepth 2.
func (*Logger) Sub ¶
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
Use only for testing log output
Click to show internal directories.
Click to hide internal directories.