Documentation
¶
Overview ¶
Example (UsingDefaultLogger) ¶
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
)
func main() {
// configure
bit.DefaultLogger.SetOutput(os.Stdout)
bit.DefaultLogger.SetFlags(bit.LEVEL | bit.TIME | bit.DEBUG)
// use
bit.Log(INFO, "first")
bit.Log(DEBUG, "second",
"color", "red",
"ok", true,
)
bit.Logf(ERROR, "%q", "third")
// reconfigure showing parent directory and file
bit.DefaultLogger.SetFlags(bit.LEVEL | bit.DIR | bit.FILE | bit.INFO)
A()
// format as text fields
bit.DefaultLogger.SetFlags(
bit.LEVEL | bit.FILE | bit.INFO | bit.FORMAT_TEXT_FIELDS,
)
bit.Log(INFO, "something", "year", 2023)
// TRACE is always shown regardless of level, good for development
bit.Log(TRACE, "wip...")
}
func A() {
// first three bits are reserved for calldepth, ie. 1..8 levels
bit.Log(INFO|1, "called by 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:32 called by example level="INFO" file="example_test.go:38" msg="something" year=2023 level="TRACE" file="example_test.go:41" msg="wip..."
Index ¶
- Constants
- Variables
- func Log(bits Bit, msg string, fields ...any)
- func Logf(bits Bit, format string, args ...any)
- type Bit
- type Logger
- func (l *Logger) Flags() Bit
- func (l *Logger) Log(bits Bit, msg string, 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) *Logger
Examples ¶
Constants ¶
View Source
const STAMP = DATE | TIME | MILLISEC
Variables ¶
View Source
var ( KeyStamp = "time" KeyLevel = "level" KeyFile = "file" KeyPrefix = "prefix" KeyMessage = "msg" )
View Source
var DefaultLogger = NewLogger()
Functions ¶
func Log ¶
Log uses DefaultLogger.Log
Example ¶
DefaultLogger.SetOutput(os.Stdout) Log(DEBUG, "debug package") // should not show Log(INFO, "package")
Output: 2006-01-02 15:04:05.123 package
Example (AsTextField) ¶
DefaultLogger.SetOutput(os.Stdout) DefaultLogger.SetFlags(LEVEL | INFO | STAMP | FORMAT_TEXT_FIELDS) Log(INFO, "package", "num%.2f", 1.12345, )
Output: time="2006-01-02 15:04:05.123" level="INFO" msg="package" num=1.12
Example (Dir) ¶
l := NewLogger() l.SetFlags(ERROR | LEVEL | DIR | FILE) l.SetOutput(os.Stdout) l.Log(ALERT, "something")
Output: ALERT bit/logger_test.go:22 something
Example (FieldArguments) ¶
DefaultLogger.SetOutput(os.Stdout)
DefaultLogger.SetFlags(LEVEL | INFO | STAMP)
type Account struct {
Name string
Age int
}
acc := Account{"John", 4}
Log(INFO, "package",
"open", true,
// if key contains a %, it's used as a format for the value
"account%+v", acc,
)
Output: 2006-01-02 15:04:05.123 INFO package open=true account={Name:John Age:4}
Example (UsingPrefix) ¶
DefaultLogger.SetOutput(os.Stdout)
DefaultLogger.SetFlags(LEVEL | INFO)
DefaultLogger.SetPrefix("candy")
Log(INFO, "hello")
Output: INFO candy hello
Example (UsingPrefixFields) ¶
DefaultLogger.SetOutput(os.Stdout)
DefaultLogger.SetFlags(LEVEL | INFO | FORMAT_TEXT_FIELDS)
DefaultLogger.SetPrefix("candy")
Log(INFO, "hello")
Output: level="INFO" prefix="candy" msg="hello"
Types ¶
type Bit ¶
type Bit int
const ( // Three first bits are used for the calldepth. Calldepth_ Bit = 1 << iota // 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. Changing parent settings, after sub logger createion, are not reflected in sub loggers. Sub loggers use the parent writer unless an explicit one has been set.
Example ¶
DefaultLogger.SetOutput(os.Stdout)
DefaultLogger.SetFlags(LEVEL | INFO)
s := DefaultLogger.Sub("car")
s.Log(INFO, "brm brm")
Output: INFO car brm brm
Click to show internal directories.
Click to hide internal directories.