2022-03-20 19:19:42 +00:00
//go:build !binary_log
2018-03-28 18:49:41 +00:00
// +build !binary_log
2018-02-09 05:00:09 +00:00
package log_test
import (
"errors"
"flag"
"os"
"time"
2022-03-20 19:19:42 +00:00
"git.tuxpa.in/a/zlog"
"git.tuxpa.in/a/zlog/log"
2018-02-09 05:00:09 +00:00
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup ( ) {
// UNIX Time is faster and smaller than most timestamps
2022-03-20 19:19:42 +00:00
// If you set zlog.TimeFieldFormat to an empty string,
2018-02-09 05:00:09 +00:00
// logs will write with UNIX time
2022-03-20 19:19:42 +00:00
zlog . TimeFieldFormat = ""
2018-02-09 05:00:09 +00:00
// In order to always output a static time to stdout for these
2022-03-20 19:19:42 +00:00
// examples to pass, we need to override zlog.TimestampFunc
2018-02-09 05:00:09 +00:00
// and log.Logger globals -- you would not normally need to do this
2022-03-20 19:19:42 +00:00
zlog . TimestampFunc = func ( ) time . Time {
2018-02-09 05:00:09 +00:00
return time . Date ( 2008 , 1 , 8 , 17 , 5 , 05 , 0 , time . UTC )
}
2022-03-20 19:19:42 +00:00
log . Logger = zlog . New ( os . Stdout ) . With ( ) . Timestamp ( ) . Logger ( )
2018-02-09 05:00:09 +00:00
}
// Simple logging example using the Print function in the log package
// Note that both Print and Printf are at the debug log level by default
func ExamplePrint ( ) {
setup ( )
log . Print ( "hello world" )
2018-02-09 05:29:16 +00:00
// Output: {"level":"debug","time":1199811905,"message":"hello world"}
2018-02-09 05:00:09 +00:00
}
// Simple logging example using the Printf function in the log package
func ExamplePrintf ( ) {
setup ( )
log . Printf ( "hello %s" , "world" )
2018-02-09 05:29:16 +00:00
// Output: {"level":"debug","time":1199811905,"message":"hello world"}
2018-02-09 05:00:09 +00:00
}
// Example of a log with no particular "level"
func ExampleLog ( ) {
setup ( )
log . Log ( ) . Msg ( "hello world" )
// Output: {"time":1199811905,"message":"hello world"}
}
2019-11-19 00:22:50 +00:00
// Example of a conditional level based on the presence of an error.
func ExampleErr ( ) {
setup ( )
err := errors . New ( "some error" )
log . Err ( err ) . Msg ( "hello world" )
log . Err ( nil ) . Msg ( "hello world" )
// Output: {"level":"error","error":"some error","time":1199811905,"message":"hello world"}
// {"level":"info","time":1199811905,"message":"hello world"}
}
2019-11-04 19:39:22 +00:00
// Example of a log at a particular "level" (in this case, "trace")
func ExampleTrace ( ) {
setup ( )
log . Trace ( ) . Msg ( "hello world" )
// Output: {"level":"trace","time":1199811905,"message":"hello world"}
}
2018-02-09 05:00:09 +00:00
// Example of a log at a particular "level" (in this case, "debug")
func ExampleDebug ( ) {
setup ( )
log . Debug ( ) . Msg ( "hello world" )
2018-02-09 05:29:16 +00:00
// Output: {"level":"debug","time":1199811905,"message":"hello world"}
2018-02-09 05:00:09 +00:00
}
// Example of a log at a particular "level" (in this case, "info")
func ExampleInfo ( ) {
setup ( )
log . Info ( ) . Msg ( "hello world" )
2018-02-09 05:29:16 +00:00
// Output: {"level":"info","time":1199811905,"message":"hello world"}
2018-02-09 05:00:09 +00:00
}
// Example of a log at a particular "level" (in this case, "warn")
func ExampleWarn ( ) {
setup ( )
log . Warn ( ) . Msg ( "hello world" )
2018-02-09 05:29:16 +00:00
// Output: {"level":"warn","time":1199811905,"message":"hello world"}
2018-02-09 05:00:09 +00:00
}
// Example of a log at a particular "level" (in this case, "error")
func ExampleError ( ) {
setup ( )
log . Error ( ) . Msg ( "hello world" )
2018-02-09 05:29:16 +00:00
// Output: {"level":"error","time":1199811905,"message":"hello world"}
2018-02-09 05:00:09 +00:00
}
// Example of a log at a particular "level" (in this case, "fatal")
func ExampleFatal ( ) {
setup ( )
err := errors . New ( "A repo man spends his life getting into tense situations" )
service := "myservice"
log . Fatal ( ) .
Err ( err ) .
Str ( "service" , service ) .
Msgf ( "Cannot start %s" , service )
2018-02-09 05:29:16 +00:00
// Outputs: {"level":"fatal","time":1199811905,"error":"A repo man spends his life getting into tense situations","service":"myservice","message":"Cannot start myservice"}
2018-02-09 05:00:09 +00:00
}
// TODO: Panic
// This example uses command-line flags to demonstrate various outputs
// depending on the chosen log level.
2018-03-15 17:50:30 +00:00
func Example ( ) {
2018-02-09 05:00:09 +00:00
setup ( )
debug := flag . Bool ( "debug" , false , "sets log level to debug" )
flag . Parse ( )
// Default level for this example is info, unless debug flag is present
2022-03-20 19:19:42 +00:00
zlog . SetGlobalLevel ( zlog . InfoLevel )
2018-02-09 05:00:09 +00:00
if * debug {
2022-03-20 19:19:42 +00:00
zlog . SetGlobalLevel ( zlog . DebugLevel )
2018-02-09 05:00:09 +00:00
}
log . Debug ( ) . Msg ( "This message appears only when log level set to Debug" )
log . Info ( ) . Msg ( "This message appears when log level set to Debug or Info" )
if e := log . Debug ( ) ; e . Enabled ( ) {
// Compute log output only if enabled.
value := "bar"
e . Str ( "foo" , value ) . Msg ( "some debug message" )
}
2018-02-09 05:29:16 +00:00
// Output: {"level":"info","time":1199811905,"message":"This message appears when log level set to Debug or Info"}
2018-02-09 05:00:09 +00:00
}
// TODO: Output
// TODO: With
// TODO: Level
// TODO: Sample
// TODO: Hook
// TODO: WithLevel
// TODO: Ctx