Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Configure ¶
Configure initializes a root logger from with all subsequent logging events are derived, provided there are no previous writes to the log. If there are any log writes before configuration, then all writes will go to os.Stderr by default. So, it's best to call this function as early as possible in your application.
Configure will set up a prototype logger to write to w, include version metadata and may optionally write to moreSinks. The output destination(s) cannot be changed once this function is called.
The version parameter may be empty, but it's recommended to put some metadata here so you can associate an event with the source code version.
Example ¶
package main import ( "os" "github.com/rafaelespinoza/logg" ) func main() { logg.Configure( os.Stderr, map[string]string{"branch_name": "main", "build_time": "20060102T150415", "commit_hash": "deadbeef"}, ) }
Output:
Example (MultipleSinks) ¶
package main import ( "io" "os" "github.com/rafaelespinoza/logg" ) func main() { // Write to standard error as well as some file. var file io.Writer logg.Configure( os.Stderr, map[string]string{"branch_name": "main", "build_time": "20060102T150415", "commit_hash": "1337d00d"}, file, ) }
Output:
Example (PossiblyUnintendedConfiguration) ¶
package main import ( "io" "github.com/rafaelespinoza/logg" ) func main() { // This example shows how the first usage of logg, that is not Configure, // may unintentionally set up your logger. logg.Infof("these writes go") logg.Infof("to standard error") logg.Infof("by default") // Then your code attempts to configure the logger to write to someSocket. var someSocket io.Writer logg.Configure( someSocket, map[string]string{"branch_name": "main", "build_time": "20060102T150415", "commit_hash": "feedface"}, ) // Whoops, these logging event will continue to go to standard error. This // may not be what you want. The solution would be to call Configure before // emitting any kind of event. logg.Infof("hello, is there") logg.Infof("anybody out there?") }
Output:
func CtxWithID ¶
CtxWithID returns a new context with an ID. If the ID already existed in the context, then the new context has the same ID as before.
Example ¶
package main import ( "context" "github.com/rafaelespinoza/logg" ) func main() { // Create a new context to ensure the same tracing ID on each subsequent // tracing event. ctxA := logg.CtxWithID(context.Background()) alfa := logg.New(map[string]interface{}{"a": "A"}).WithID(ctxA) alfa.Infof("altoona") alfa.Infof("athletic") // Attempting to create a logger using the same context would yield the same // tracing ID on each event. ctxB := logg.CtxWithID(ctxA) bravo := logg.New(map[string]interface{}{"b": "B"}).WithID(ctxB) bravo.Infof("boston") bravo.Infof("boisterous") // If you need another tracing ID, then use a brand-new context as the // parent context create create another context (using a // brand-new context as the parent) ctxC := logg.CtxWithID(context.Background()) charlie := logg.New(map[string]interface{}{"c": "C"}).WithID(ctxC) charlie.Infof("chicago") charlie.Infof("chewbacca") }
Output:
Types ¶
type Emitter ¶
type Emitter interface { Infof(msg string, args ...interface{}) Errorf(err error, msg string, args ...interface{}) WithID(ctx context.Context) Emitter WithData(fields map[string]interface{}) Emitter }
An Emitter emitter writes to the log at info or error levels.
func New ¶
New initializes a logger Emitter type and configures it so each event emission outputs fields at the data key. If sinks is empty or the first sink is nil, then it writes to the same destination as the root logger. If sinks is non-empty then it duplicates the root logger and writes to sinks.
Example ¶
package main import ( "github.com/rafaelespinoza/logg" ) func main() { // Create a logger without any data fields. logger := logg.New(nil) // do stuff ... logger.Infof("no data fields here") }
Output:
Example (MultipleSinks) ¶
package main import ( "io" "time" "github.com/rafaelespinoza/logg" ) func main() { // This logger will emit events to multiple destinations. var file, socket io.Writer dataFields := map[string]interface{}{ "bravo": true, "delta": 234 * time.Millisecond, "foxtrot": float64(1.23), "india": 10, } logger := logg.New(dataFields, file, socket) // do stuff ... logger.Infof("hello") logger.Infof("world") }
Output:
Example (WithData) ¶
package main import ( "time" "github.com/rafaelespinoza/logg" ) func main() { // Initialize the logger with data fields. logger := logg.New(map[string]interface{}{ "bravo": true, "delta": 234 * time.Millisecond, "foxtrot": float64(1.23), "india": 10, }) // do stuff ... logger.Infof("hello") logger.Infof("world") }
Output:
Example (WithID) ¶
package main import ( "context" "github.com/rafaelespinoza/logg" ) func main() { // Set up a logger a tracing ID. logger := logg.New(nil).WithID(context.Background()) // do stuff ... logger.Infof("hello") logger.Infof("world") }
Output: