Skip to content

Commit

Permalink
[FEATURE] Made syslog integration OS specific
Browse files Browse the repository at this point in the history
Windows doesn't have log/syslog package. Abstract syslog interface in OS specific
go files. Panic under Windows if syslog requested.

* Testing done:
* Bugs:
* Bugs closed:
* Severity: trivial|bugfix|feature
* Review URL: (recommended)
* Reviewed by: (recommended)
* Approved by:
* Commit template: 2014-01-07
  • Loading branch information
HeikoKoehler committed May 8, 2017
1 parent c7bc94f commit e32b306
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 13 deletions.
21 changes: 8 additions & 13 deletions glog.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ import (
"fmt"
"io"
stdLog "log"
"log/syslog"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -162,6 +161,11 @@ func severityByName(s string) (severity, bool) {
return 0, false
}

// internal interface for optional syslog facility
type sysLogger interface {
log(s severity, msg string)
}

// OutputStats tracks the number of output lines and bytes written.
type OutputStats struct {
lines int64
Expand Down Expand Up @@ -430,7 +434,7 @@ type loggingT struct {
stderrThreshold severity // The -stderrthreshold flag.

// syslog logger
sysLogger *syslog.Writer
sysLogger sysLogger

// freeList is a list of byte buffers, maintained under freeListMu.
freeList *buffer
Expand Down Expand Up @@ -683,7 +687,7 @@ func (l *loggingT) printWithFileLine(s severity, file string, line int, alsoToSt
func (l *loggingT) output(s severity, buf *buffer, file string, line int, alsoToStderr bool) {
l.mu.Lock()
if l.toSyslog && l.sysLogger == nil {
sysLogger, err := syslog.Dial("", "", syslog.LOG_DEBUG, program)
sysLogger, err := NewSysLogger(program)
if err != nil {
l.mu.Unlock()
panic(err)
Expand All @@ -703,16 +707,7 @@ func (l *loggingT) output(s severity, buf *buffer, file string, line int, alsoTo
} else if l.toStderr {
os.Stderr.Write(data)
} else if l.toSyslog {
switch s {
case fatalLog:
l.sysLogger.Emerg(string(data))
case errorLog:
l.sysLogger.Err(string(data))
case warningLog:
l.sysLogger.Warning(string(data))
case infoLog:
l.sysLogger.Info(string(data))
}
l.sysLogger.log(s, string(data))
} else {
if alsoToStderr || l.alsoToStderr || s >= l.stderrThreshold.get() {
os.Stderr.Write(data)
Expand Down
53 changes: 53 additions & 0 deletions glog_syslog_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Go support for leveled logs, analogous to https://code.google.com/p/google-glog/
//
// Copyright 2017 Nutanix Inc. All Rights Reserved.
//
// 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.

// Syslog IO for logs
// +build linux darwin

package glog

import (
"log/syslog"
)

type unixSysLogger struct {
sysLogger *syslog.Writer
}

// implement sysLogger interface
func (l *unixSysLogger) log(s severity, msg string) {
switch s {
case fatalLog:
l.sysLogger.Emerg(msg)
case errorLog:
l.sysLogger.Err(msg)
case warningLog:
l.sysLogger.Warning(msg)
case infoLog:
l.sysLogger.Info(msg)
}
}

func NewSysLogger(tag string) (sysLogger, error) {
sysLogger, err := syslog.Dial("", "", syslog.LOG_DEBUG, tag)
if err != nil {
return nil, err
} else {
return &unixSysLogger{
sysLogger: sysLogger,
}, nil
}
}
24 changes: 24 additions & 0 deletions glog_syslog_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Go support for leveled logs, analogous to https://code.google.com/p/google-glog/
//
// Copyright 2017 Nutanix Inc. All Rights Reserved.
//
// 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.

// Syslog IO for logs
// +build windows

package glog

func NewSysLogger(tag string) (sysLogger, error) {
panic("Not implemented")
}

0 comments on commit e32b306

Please sign in to comment.