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

syslog writer #506

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 24 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ package zap

import (
"io/ioutil"
"log/syslog"
"os"
p "path"

"go.uber.org/zap/zapcore"

Expand Down Expand Up @@ -65,6 +67,28 @@ func open(paths []string) ([]zapcore.WriteSyncer, func(), error) {
writers = append(writers, os.Stderr)
// Don't close standard error.
continue
case "syslog":
// returns address for syslog to write on
syslogAddress, ok := os.LookupEnv("SYSLOG")
if !ok || (syslogAddress == "0") || (syslogAddress == "false") {
continue
}

// returns name of application that called logger to be used as value of tag in syslog connection
// or uses specific tag from env
syslogTag := p.Base(os.Args[0])
if tag, ok := os.LookupEnv("SYSLOG_TAG"); ok {
syslogTag = tag
}

// establishes connection via udp to specific address and returns writer
sys, err := syslog.Dial("udp", syslogAddress, syslog.LOG_LOCAL5, syslogTag)
if err != nil {
continue
}

writers = append(writers, AddSync(sys))
continue
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
openErr = multierr.Append(openErr, err)
Expand Down
20 changes: 20 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
package zap

import (
"fmt"
"io/ioutil"
"log/syslog"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

Expand Down Expand Up @@ -123,3 +126,20 @@ func TestCombineWriteSyncers(t *testing.T) {
w := CombineWriteSyncers(tw)
w.Write([]byte("test"))
}

func TestSyslogConnection(t *testing.T) {
sysLog, err := syslog.Dial("udp", "127.0.0.1:514", syslog.LOG_LOCAL5, "testtag")
if err != nil {
fmt.Println(err)
}
sysLog.Write([]byte("test"))
}

func TestSyslog(t *testing.T) {
// needs to be tested with env
config := zap.NewProductionConfig()
config.OutputPaths = "syslog"
logger, _ := config.Build()
defer logger.Sync()
logger.Info("msg")
}