![GoDoc](https://godoc.org/github.com/bobziuchkovski/cue?status.svg)
Cue
Overview
Cue implements contextual logging with "batteries included". It has
thorough test coverage and supports logging to stdout/stderr, file, syslog,
and network sockets, as well as hosted third-party logging and error/reporting
services such as Honeybadger, Loggly, Opbeat, Rollbar, and Sentry.
Cue uses atomic operations to compare logging calls to registered collector
thresholds. This ensures no-op calls are performed quickly and without lock
contention. On a 2015 MacBook Pro, no-op calls take about 16ns/call, meaning
tens of millions of calls may be dispatched per second. Uncollected log calls
are very cheap.
API Promise
Minor breaking changes may occur prior to the 1.0 release. After the 1.0
release, the API is guaranteed to remain backwards compatible.
Cue makes use of sync/atomic.Value and thus requires Go 1.4.x or later.
Basic Use
Please see the godocs for
additional information.
This example logs to both the terminal (stdout) and to file. If the program
receives SIGHUP, the file will be reopened. This is useful for log rotation.
Additional context is added via the .WithValue and .WithFields Logger methods.
The formatting may be changed by passing a different formatter to either collector.
See the cue/format godocs
for details. The context data may also be formatted as JSON for machine parsing
if desired. See cue/format.JSONMessage and cue/format.JSONContext.
package main
import (
"github.com/bobziuchkovski/cue"
"github.com/bobziuchkovski/cue/collector"
"os"
"syscall"
)
var log = cue.NewLogger("main")
func main() {
cue.Collect(cue.INFO, collector.Terminal{}.New())
cue.Collect(cue.INFO, collector.File{
Path: "app.log",
ReopenSignal: syscall.SIGHUP,
}.New())
log := cue.NewLogger("example")
log.Debug("Debug message -- a quick no-op since our collector is registered at INFO level")
log.Info("Info message")
log.Warn("Warn message")
log.WithValue("items", 2).Infof("This is an %s", "example")
log.WithFields(cue.Fields{
"user": "bob",
"authenticated": true,
}).Warn("Doing something important")
host, err := os.Hostname()
if err != nil {
log.Error(err, "Failed to retrieve hostname")
} else {
log.Infof("My hostname is %s", host)
}
}
Error Reporting
Please see the godocs for
additional information.
This example uses cue/hosted.Honeybadger to report error events to Honeybadger.
package main
import (
"github.com/bobziuchkovski/cue"
"github.com/bobziuchkovski/cue/hosted"
"os"
"time"
)
var log = cue.NewLogger("main")
func main() {
cue.CollectAsync(cue.ERROR, 10000, hosted.Honeybadger{
Key: os.Getenv("HONEYBADGER_KEY"),
Environment: os.Getenv("APP_ENV"),
}.New())
cue.SetFrames(1, 32)
defer cue.Close(5 * time.Second)
defer log.Recover("Recovered panic")
PanickingFunc()
}
func PanickingFunc() {
panic("This will be reported to Honeybadger")
}
Features
Please see the godocs for
additional information.
This example shows quite a few of the cue features: logging to a file that
reopens on SIGHUP (for log rotation), logging colored output to stdout,
logging to syslog with JSON context formatting, and reporting errors to
Honeybadger.
package main
import (
"github.com/bobziuchkovski/cue"
"github.com/bobziuchkovski/cue/collector"
"github.com/bobziuchkovski/cue/format"
"github.com/bobziuchkovski/cue/hosted"
"os"
"syscall"
"time"
)
var log = cue.NewLogger("main")
func main() {
defer cue.Close(5 * time.Second)
defer log.Recover("Recovered panic in main")
ConfigureLogging()
RunTheProgram()
}
func ConfigureLogging() {
cue.Collect(cue.DEBUG, collector.Terminal{
Formatter: format.HumanReadableColors,
}.New())
cue.Collect(cue.INFO, collector.File{
Path: "app.log",
ReopenSignal: syscall.SIGHUP,
}.New())
cue.Collect(cue.WARN, collector.Syslog{
App: "app",
Facility: collector.LOCAL7,
Formatter: format.JSONMessage,
}.New())
cue.CollectAsync(cue.ERROR, 10000, hosted.Honeybadger{
Key: os.Getenv("HONEYBADGER_KEY"),
Environment: os.Getenv("APP_ENV"),
}.New())
cue.SetFrames(1, 32)
}
func RunTheProgram() {
log.Info("Running the program!")
log.WithFields(cue.Fields{
"sad": true,
"length": 0,
}).Panic("No program", "Whoops, there's no program to run!")
}
Documentation
Please see the godocs for
additional details.
Authors
Bob Ziuchkovski (@bobziuchkovski)
License (MIT)
Copyright (c) 2016 Bob Ziuchkovski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.