pkg/logutil: error when it can't find journal socket

Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
This commit is contained in:
Gyuho Lee 2018-05-16 14:11:08 -07:00
parent 65b29b0bd2
commit 1e953bd187
4 changed files with 58 additions and 3 deletions

View File

@ -24,6 +24,8 @@ import (
"os"
"path/filepath"
"github.com/coreos/etcd/pkg/systemd"
"github.com/coreos/go-systemd/journal"
"go.uber.org/zap/zapcore"
)
@ -33,8 +35,8 @@ import (
// back to writing to the original writer.
// The decode overhead is only <30µs per write.
// Reference: https://github.com/coreos/pkg/blob/master/capnslog/journald_formatter.go
func NewJournaldWriter(wr io.Writer) io.Writer {
return &journaldWriter{Writer: wr}
func NewJournaldWriter(wr io.Writer) (io.Writer, error) {
return &journaldWriter{Writer: wr}, systemd.DialJournal()
}
type journaldWriter struct {
@ -82,6 +84,8 @@ func (w *journaldWriter) Write(p []byte) (int, error) {
"SYSLOG_IDENTIFIER": filepath.Base(os.Args[0]),
})
if err != nil {
// "journal" also falls back to stderr
// "fmt.Fprintln(os.Stderr, s)"
return w.Writer.Write(p)
}
return 0, nil

View File

@ -26,7 +26,13 @@ import (
func TestNewJournaldWriter(t *testing.T) {
buf := bytes.NewBuffer(nil)
syncer := zapcore.AddSync(NewJournaldWriter(buf))
jw, err := NewJournaldWriter(buf)
if err != nil {
t.Skip(err)
}
syncer := zapcore.AddSync(jw)
cr := zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
syncer,

16
pkg/systemd/doc.go Normal file
View File

@ -0,0 +1,16 @@
// Copyright 2018 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package systemd provides utility functions for systemd.
package systemd

29
pkg/systemd/journal.go Normal file
View File

@ -0,0 +1,29 @@
// Copyright 2018 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package systemd
import "net"
// DialJournal returns no error if the process can dial journal socket.
// Returns an error if dial failed, whichi indicates journald is not available
// (e.g. run embedded etcd as docker daemon).
// Reference: https://github.com/coreos/go-systemd/blob/master/journal/journal.go.
func DialJournal() error {
conn, err := net.Dial("unixgram", "/run/systemd/journal/socket")
if conn != nil {
defer conn.Close()
}
return err
}