forked from bobziuchkovski/cue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_features_test.go
67 lines (57 loc) · 1.89 KB
/
example_features_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2016 Bob Ziuchkovski. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package cue_test
import (
"os"
"syscall"
"time"
"github.com/remerge/cue"
"github.com/remerge/cue/collector"
"github.com/remerge/cue/format"
"github.com/remerge/cue/hosted"
)
var log = cue.NewLogger("example")
// 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, and reporting errors to Honeybadger.
func Example_features() {
// defer cue.Close before log.Recover so that Close flushes any events
// triggers by panic recovery
defer cue.Close(5 * time.Second)
defer log.Recover("Recovered panic in main")
ConfigureLogging()
RunTheProgram()
}
func ConfigureLogging() {
// Collect logs to stdout in color! :)
cue.Collect(cue.DEBUG, collector.Terminal{
Formatter: format.HumanReadableColors,
}.New())
// Collect to app.log and reopen the handle if we receive SIGHUP
cue.Collect(cue.INFO, collector.File{
Path: "app.log",
ReopenSignal: syscall.SIGHUP,
}.New())
// Collect to syslog, formatting the context data as JSON for indexing.
cue.Collect(cue.WARN, collector.Syslog{
App: "app",
Facility: collector.LOCAL7,
Formatter: format.JSONMessage,
}.New())
// Report errors asynchronously to Honeybadger. If HONEYBADGER_KEY is
// unset, Honeybadger.New will return nil and cue.CollectAsync will
// ignore it. This works great for development.
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!")
}