Logging and Loggers in Cookoo

Virtually every application needs logging and Go already tries to make logging easy. In cookoo there is some helper functionality enabling the use of multiple loggers and provide access to logging in commands without needing to know how logging is implemented.

Making Loggers Available

Loggers are registered as part of the setup code. The plural use is intentional as multiple loggers can be registered. For example,

registry, router, context := cookoo.Cookoo()

logger := NewMyLogger()
logger2 := NewMyOtherLogger()
context.AddLogger("foo", logger)
context.AddLogger("bar", logger2)

Loggers for cookoo simply implement the io.Writer interface.

Logging

Logging is then available from the context passed into each command.

func Foo(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
    c.Log("info", "This is a test")

    return true, nil
}

The two logging functions available from the Context are Log() and Logf(). The first argument is the prefix to use when the message is logged. When one of these logging functions is called the log is sent to each of the registered loggers.

Managing Loggers

Some helper functions exist to manage the registered loggers.

To retrieve a logger that has been registered use the Logger() function on the context.

logger, exists := context.Logger("foo")

Removing a logger can be done via RemoveLogger().

context.RemoveLogger("foo")