Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Logentries handler. #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Package log implements a simple structured logging API inspired by Logrus, desig
- __level__ – level filter handler
- __logfmt__ – logfmt plain-text formatter
- __memory__ – in-memory handler for tests
- __logentries__ – Logentries.com handler
- __multi__ – fan-out to multiple handlers
- __papertrail__ – Papertrail handler
- __text__ – human-friendly colored output
Expand Down
31 changes: 31 additions & 0 deletions _examples/logentries/logentries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"os"
"time"

"github.com/apex/log"
"github.com/apex/log/handlers/logentries"
"github.com/apex/log/handlers/multi"
"github.com/apex/log/handlers/text"
)

func main() {
handler, _ := logentries.New("REPLACE_WITH_YOUR_TOKEN")

log.SetHandler(multi.New(
text.New(os.Stderr),
handler,
))

ctx := log.WithFields(log.Fields{
"file": "something.png",
"type": "image/png",
"user": "tobi",
})

for range time.Tick(time.Millisecond * 100) {
ctx.Info("upload")
ctx.Info("upload complete")
}
}
37 changes: 37 additions & 0 deletions handlers/logentries/logentries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Package LogEntries implements a LogEntries JSON handler.
package logentries

import (
"encoding/json"

"github.com/apex/log"
"github.com/bsphere/le_go"
)

type (
LogHandler struct {
logger *le_go.Logger
}
)

func New(token string) (*LogHandler, error) {
logger, err := le_go.Connect(token)

if err != nil {
return nil, err
}

return &LogHandler{
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't 100% sure here if I could nix the previous guard and return &LogHandler{logger: logger}, err here

Copy link

@jchv jchv Jan 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best to return nil on error. (in other words, I think the guard is good.)

logger: logger,
}, nil
}

func (handler *LogHandler) HandleLog(entry *log.Entry) error {
b, err := json.Marshal(entry)
if err != nil {
return err
}

_, writer_error := handler.logger.Write(b)
return writer_error
}