Skip to content

Commit

Permalink
updated syslog to use github.com/RackSec/srslog
Browse files Browse the repository at this point in the history
- std lib syslog will no longer be maintained as per comment golang/go#13449 (comment)
- TLS support is now available via new syslog package
- Simplified Syslog New() function
- eliminated some data races IN THE TESTS, NOT THE LIBRARY ITSELF
  • Loading branch information
joeybloggs authored and joeybloggs committed Jul 21, 2016
1 parent e40ca21 commit fe02c9f
Show file tree
Hide file tree
Showing 20 changed files with 997 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## log
<img align="right" src="https://raw.githubusercontent.com/go-playground/log/master/logo.png">
![Project status](https://img.shields.io/badge/version-3.1.1-green.svg)
![Project status](https://img.shields.io/badge/version-4.0.0-green.svg)
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/log/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/log)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/log/badge.svg?branch=master)](https://coveralls.io/github/go-playground/log?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/log)](https://goreportcard.com/report/github.com/go-playground/log)
Expand Down Expand Up @@ -188,7 +188,7 @@ Pull requests for new handlers are welcome, please provide test coverage is all
| Handler | Description | Docs |
| ------- | ---- | ----------- |
| console | Allows for log messages to be sent to a any writer, default os.Stderr | [![GoDoc](https://godoc.org/github.com/go-playground/log/handlers/console?status.svg)](https://godoc.org/github.com/go-playground/log/handlers/console) |
| syslog | Allows for log messages to be sent via syslog. | [![GoDoc](https://godoc.org/github.com/go-playground/log/handlers/syslog?status.svg)](https://godoc.org/github.com/go-playground/log/handlers/syslog) |
| syslog | Allows for log messages to be sent via syslog, includes TLS support. | [![GoDoc](https://godoc.org/github.com/go-playground/log/handlers/syslog?status.svg)](https://godoc.org/github.com/go-playground/log/handlers/syslog) |
| http | Allows for log messages to be sent via http. Can use the HTTP handler as a base for creating other handlers requiring http transmission. | [![GoDoc](https://godoc.org/github.com/go-playground/log/handlers/http?status.svg)](https://godoc.org/github.com/go-playground/log/handlers/http) |
| email | Allows for log messages to be sent via email. | [![GoDoc](https://godoc.org/github.com/go-playground/log/handlers/email?status.svg)](https://godoc.org/github.com/go-playground/log/handlers/email) |
| hipchat | Allows for log messages to be sent to a hipchat room. | [![GoDoc](https://godoc.org/github.com/go-playground/log/handlers/http/hipchat?status.svg)](https://godoc.org/github.com/go-playground/log/handlers/http/hipchat) |
Expand Down
1 change: 0 additions & 1 deletion handlers/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ func (c *Console) handleLog(entries <-chan *log.Entry) {

b = formatter(e)
c.writer.Write(b)
// b.WriteTo(c.writer)

e.Consumed()
}
Expand Down
25 changes: 20 additions & 5 deletions handlers/console/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
stdlog "log"
"strings"
"sync"
"testing"
"time"

"github.com/go-playground/log"
)
Expand All @@ -13,15 +15,13 @@ import (
// - Run "go test" to run tests
// - Run "gocov test | gocov report" to report on test converage by file
// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called

//
// or

// -- may be a good idea to change to output path to somewherelike /tmp
// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html

func TestConsoleLogger(t *testing.T) {
tests := getConsoleLoggerTests()

buff := new(bytes.Buffer)

cLog := New()
Expand Down Expand Up @@ -276,21 +276,36 @@ func TestSetFilenameColor(t *testing.T) {

func TestConsoleSTDLogCapturing(t *testing.T) {

var m sync.Mutex
buff := new(bytes.Buffer)

cLog := New()
cLog.SetWriter(buff)
cLog.SetDisplayColor(false)
cLog.SetBuffersAndWorkers(3, 3)
cLog.SetTimestampFormat("MST")
cLog.RedirectSTDLogOutput(true)
cLog.SetFormatFunc(func(c *Console) Formatter {
return func(e *log.Entry) []byte {
m.Lock()
defer m.Unlock()
buff.Write([]byte(e.Message))

return buff.Bytes()
}
})

log.RegisterHandler(cLog, log.AllLevels...)

stdlog.Println("STD LOG message")

time.Sleep(500 * time.Millisecond)

m.Lock()
s := buff.String()
m.Unlock()

expected := "STD LOG message stdlog=true"
// expected := "STD LOG message stdlog=true"
expected := "STD LOG message"

if !strings.Contains(s, expected) {
t.Errorf("Expected '%s' Got '%s'", expected, s)
Expand Down
13 changes: 12 additions & 1 deletion handlers/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"html/template"
stdlog "log"
"os"
"sync"
"time"

"gopkg.in/gomail.v2"
Expand Down Expand Up @@ -59,6 +60,7 @@ type Email struct {
from string
to []string
keepalive time.Duration
m sync.Mutex
}

// New returns a new instance of the email logger
Expand Down Expand Up @@ -192,10 +194,19 @@ func (email *Email) Run() chan<- *log.Entry {
func defaultFormatFunc(email *Email) Formatter {
var err error
b := new(bytes.Buffer)

// apparently there is a race condition when I was using
// email.to... below in the SetHeader for whatever reason
// so copying the "to" values solves the issue
// I wonder if it's a flase positive in the race detector.....
to := make([]string, len(email.to), len(email.to))
copy(to, email.to)

template := email.Template()
message := gomail.NewMessage()

message.SetHeader("From", email.from)
message.SetHeader("To", email.to...)
message.SetHeader("To", to...)

return func(e *log.Entry) *gomail.Message {
b.Reset()
Expand Down
22 changes: 17 additions & 5 deletions handlers/email/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,15 @@ func TestEmailHandler(t *testing.T) {

defer server.Close()

proceed := make(chan struct{})
defer close(proceed)

go func() {
for {
conn, err := server.Accept()
if err != nil {
t.Errorf("Expected <nil> Got '%s'", err)
msg = ""
break
}

if conn == nil {
Expand All @@ -138,11 +142,15 @@ func TestEmailHandler(t *testing.T) {
}

msg = handleClient(c, false)

proceed <- struct{}{}
}
}()

log.Debug("debug")

<-proceed

for i, tt := range tests {
if !strings.Contains(msg, tt.expected) {
t.Errorf("Index %d Expected '%s' Got '%s'", i, tt.expected, msg)
Expand All @@ -155,6 +163,8 @@ func TestEmailHandler(t *testing.T) {

log.Debug("debug")

<-proceed

for i, tt := range tests {
if !strings.Contains(msg, tt.expected) {
t.Errorf("Index %d Expected '%s' Got '%s'", i, tt.expected, msg)
Expand Down Expand Up @@ -195,7 +205,7 @@ func TestBadSend(t *testing.T) {
for {
conn, err := server.Accept()
if err != nil {
t.Errorf("Expected <nil> Got '%s'", err)
break
}

if conn == nil {
Expand All @@ -220,12 +230,14 @@ func TestBadSend(t *testing.T) {
func testFormatFunc(email *Email) Formatter {
var err error
b := new(bytes.Buffer)
message := gomail.NewMessage()
message.SetHeader("From", email.From())
message.SetHeader("To", email.To()...)

return func(e *log.Entry) *gomail.Message {
b.Reset()

message := gomail.NewMessage()
message.SetHeader("From", email.From())
message.SetHeader("To", email.To()...)

if err = email.template.ExecuteTemplate(b, "email", e); err != nil {
log.WithFields(log.F("error", err)).Error("Error parsing Email handler template")
}
Expand Down
4 changes: 3 additions & 1 deletion handlers/syslog/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Package syslog allows for log messages to be sent via syslog.
Example
NOTE: currently syslog uses the std library's syslog
NOTE: syslog uses github.com/RackSec/srslog as the stdlib syslog
is no longer being maintained or added to as of this discussion
https://github.com/golang/go/issues/13449#issuecomment-161204716
package main
Expand Down
19 changes: 15 additions & 4 deletions handlers/syslog/syslog.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package syslog

import (
"crypto/tls"
"fmt"
stdlog "log"
"log/syslog"
"os"
"strconv"

syslog "github.com/RackSec/srslog"

"github.com/go-playground/log"
)

Expand Down Expand Up @@ -56,8 +58,10 @@ var (
)

// New returns a new instance of the syslog logger
// example: syslog.New("udp", "localhost:514", syslog.LOG_DEBUG, "")
func New(network string, raddr string, priority syslog.Priority, tag string) (*Syslog, error) {
// example: syslog.New("udp", "localhost:514", syslog.LOG_DEBUG, "", nil)
// NOTE: tlsConfig param is optional and only applies when networks in "tcp+tls"
// see TestSyslogTLS func tion int syslog_test.go for an example usage of tlsConfig parameter
func New(network string, raddr string, tag string, tlsConfig *tls.Config) (*Syslog, error) {

var err error

Expand All @@ -71,7 +75,14 @@ func New(network string, raddr string, priority syslog.Priority, tag string) (*S
formatFunc: defaultFormatFunc,
}

if s.writer, err = syslog.Dial(network, raddr, priority, tag); err != nil {
// if non-TLS
if tlsConfig == nil {
s.writer, err = syslog.Dial(network, raddr, syslog.LOG_INFO, tag)
} else {
s.writer, err = syslog.DialWithTLSConfig(network, raddr, syslog.LOG_INFO, tag, tlsConfig)
}

if err != nil {
return nil, err
}

Expand Down
Loading

0 comments on commit fe02c9f

Please sign in to comment.