package log
import (
"fmt"
"math"
)
type fieldType int
const (
stringType fieldType = iota
boolType
intType
int32Type
uint32Type
int64Type
uint64Type
float32Type
float64Type
errorType
objectType
lazyLoggerType
noopType
)
type Field struct {
key string
fieldType fieldType
numericVal int64
stringVal string
interfaceVal interface {}
}
func String (key , val string ) Field {
return Field {
key : key ,
fieldType : stringType ,
stringVal : val ,
}
}
func Bool (key string , val bool ) Field {
var numericVal int64
if val {
numericVal = 1
}
return Field {
key : key ,
fieldType : boolType ,
numericVal : numericVal ,
}
}
func Int (key string , val int ) Field {
return Field {
key : key ,
fieldType : intType ,
numericVal : int64 (val ),
}
}
func Int32 (key string , val int32 ) Field {
return Field {
key : key ,
fieldType : int32Type ,
numericVal : int64 (val ),
}
}
func Int64 (key string , val int64 ) Field {
return Field {
key : key ,
fieldType : int64Type ,
numericVal : val ,
}
}
func Uint32 (key string , val uint32 ) Field {
return Field {
key : key ,
fieldType : uint32Type ,
numericVal : int64 (val ),
}
}
func Uint64 (key string , val uint64 ) Field {
return Field {
key : key ,
fieldType : uint64Type ,
numericVal : int64 (val ),
}
}
func Float32 (key string , val float32 ) Field {
return Field {
key : key ,
fieldType : float32Type ,
numericVal : int64 (math .Float32bits (val )),
}
}
func Float64 (key string , val float64 ) Field {
return Field {
key : key ,
fieldType : float64Type ,
numericVal : int64 (math .Float64bits (val )),
}
}
func Error (err error ) Field {
return Field {
key : "error.object" ,
fieldType : errorType ,
interfaceVal : err ,
}
}
func Object (key string , obj interface {}) Field {
return Field {
key : key ,
fieldType : objectType ,
interfaceVal : obj ,
}
}
func Event (val string ) Field {
return String ("event" , val )
}
func Message (val string ) Field {
return String ("message" , val )
}
type LazyLogger func (fv Encoder )
func Lazy (ll LazyLogger ) Field {
return Field {
fieldType : lazyLoggerType ,
interfaceVal : ll ,
}
}
func Noop () Field {
return Field {
fieldType : noopType ,
}
}
type Encoder interface {
EmitString (key, value string )
EmitBool (key string , value bool )
EmitInt (key string , value int )
EmitInt32 (key string , value int32 )
EmitInt64 (key string , value int64 )
EmitUint32 (key string , value uint32 )
EmitUint64 (key string , value uint64 )
EmitFloat32 (key string , value float32 )
EmitFloat64 (key string , value float64 )
EmitObject (key string , value interface {})
EmitLazyLogger (value LazyLogger )
}
func (lf Field ) Marshal (visitor Encoder ) {
switch lf .fieldType {
case stringType :
visitor .EmitString (lf .key , lf .stringVal )
case boolType :
visitor .EmitBool (lf .key , lf .numericVal != 0 )
case intType :
visitor .EmitInt (lf .key , int (lf .numericVal ))
case int32Type :
visitor .EmitInt32 (lf .key , int32 (lf .numericVal ))
case int64Type :
visitor .EmitInt64 (lf .key , int64 (lf .numericVal ))
case uint32Type :
visitor .EmitUint32 (lf .key , uint32 (lf .numericVal ))
case uint64Type :
visitor .EmitUint64 (lf .key , uint64 (lf .numericVal ))
case float32Type :
visitor .EmitFloat32 (lf .key , math .Float32frombits (uint32 (lf .numericVal )))
case float64Type :
visitor .EmitFloat64 (lf .key , math .Float64frombits (uint64 (lf .numericVal )))
case errorType :
if err , ok := lf .interfaceVal .(error ); ok {
visitor .EmitString (lf .key , err .Error())
} else {
visitor .EmitString (lf .key , "<nil>" )
}
case objectType :
visitor .EmitObject (lf .key , lf .interfaceVal )
case lazyLoggerType :
visitor .EmitLazyLogger (lf .interfaceVal .(LazyLogger ))
case noopType :
}
}
func (lf Field ) Key () string {
return lf .key
}
func (lf Field ) Value () interface {} {
switch lf .fieldType {
case stringType :
return lf .stringVal
case boolType :
return lf .numericVal != 0
case intType :
return int (lf .numericVal )
case int32Type :
return int32 (lf .numericVal )
case int64Type :
return int64 (lf .numericVal )
case uint32Type :
return uint32 (lf .numericVal )
case uint64Type :
return uint64 (lf .numericVal )
case float32Type :
return math .Float32frombits (uint32 (lf .numericVal ))
case float64Type :
return math .Float64frombits (uint64 (lf .numericVal ))
case errorType , objectType , lazyLoggerType :
return lf .interfaceVal
case noopType :
return nil
default :
return nil
}
}
func (lf Field ) String () string {
return fmt .Sprint (lf .key , ":" , lf .Value ())
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .