Add support for "syslog" logging

pull/902/head
Malthe Borch 2014-09-03 21:41:56 +02:00
parent fd0457c1ff
commit 8b369aed2b
6 changed files with 103 additions and 6 deletions

View File

@ -1,5 +1,7 @@
## v0.8.1 [unreleased]
- [Issue #896](https://github.com/influxdb/influxdb/issues/896). Allow logging to syslog. Thanks @malthe
### Bugfixes
## v0.8.0 [2014-08-22]

View File

@ -18,7 +18,7 @@ reporting-disabled = false
[logging]
# logging level can be one of "debug", "info", "warn" or "error"
level = "info"
file = "influxdb.log" # stdout to log to standard out
file = "influxdb.log" # stdout to log to standard out, or syslog
# Configure the admin server
[admin]

View File

@ -31,11 +31,18 @@ func setupLogging(loggingLevel, logFile string) {
log.Global = make(map[string]*log.Filter)
if logFile == "stdout" {
switch logFile {
case "stdout":
flw := log.NewConsoleLogWriter()
log.AddFilter("stdout", level, flw)
} else {
case "syslog":
flw, err := NewSysLogWriter(3)
if err != nil {
fmt.Fprintf(os.Stderr, "NewSysLogWriter: %s\n", err.Error())
return
}
log.AddFilter("syslog", level, flw)
default:
logFileDir := filepath.Dir(logFile)
os.MkdirAll(logFileDir, 0744)
@ -62,6 +69,7 @@ func main() {
pidFile := flag.String("pidfile", "", "the pid file")
repairLeveldb := flag.Bool("repair-ldb", false, "set to true to repair the leveldb files")
stdout := flag.Bool("stdout", false, "Log to stdout overriding the configuration")
syslog := flag.Bool("syslog", false, "Log to syslog overriding the configuration")
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
@ -96,6 +104,11 @@ func main() {
if *stdout {
config.LogFile = "stdout"
}
if *syslog {
config.LogFile = "syslog"
}
setupLogging(config.LogLevel, config.LogFile)
if *repairLeveldb {

82
daemon/syslog.go Normal file
View File

@ -0,0 +1,82 @@
package main
import (
"errors"
"log/syslog"
log "code.google.com/p/log4go"
)
// This log writer sends output to a socket
type SysLogWriter chan *log.LogRecord
// This is the SocketLogWriter's output method
func (w SysLogWriter) LogWrite(rec *log.LogRecord) {
w <- rec
}
func (w SysLogWriter) Close() {
close(w)
}
func GetWriter(writer *syslog.Writer, level string) func(string) error {
switch level {
case "DEBG", "TRAC", "FINE", "FNST":
return writer.Debug
case "INFO":
return writer.Info
case "WARN":
return writer.Warning
case "EROR":
return writer.Err
default:
return writer.Crit
}
}
func Log(writer *syslog.Writer, level string, message string) {
m := GetWriter(writer, level)
m(message)
}
func connectSyslogDaemon() (writer *syslog.Writer, err error) {
logTypes := []string{"unixgram", "unix"}
logPaths := []string{"/dev/log", "/var/run/syslog"}
var raddr string
for _, network := range logTypes {
for _, path := range logPaths {
raddr = path
writer, err = syslog.Dial(network, raddr, syslog.LOG_SYSLOG, "influxdb")
if err != nil {
continue
} else {
return
}
}
}
if err != nil {
err = errors.New("cannot connect to Syslog Daemon")
}
return
}
func NewSysLogWriter(facility int) (w SysLogWriter, err error) {
writer, err := connectSyslogDaemon()
if err != nil {
return
}
w = SysLogWriter(make(chan *log.LogRecord, log.LogBufferLength))
go func() {
defer func() {
if w != nil {
w.Close()
}
}()
for rec := range w {
m := log.FormatLogRecord("(%S) %M", rec)
Log(writer, rec.Level.String(), m)
}
}()
return
}

View File

@ -18,7 +18,7 @@ reporting-disabled = true
[logging]
# logging level can be one of "debug", "info", "warn" or "error"
level = "info"
file = "stdout" # stdout to log to standard out
file = "stdout" # stdout to log to standard out, or syslog
# Configure the admin server
[admin]

View File

@ -18,7 +18,7 @@ reporting-disabled = true
[logging]
# logging level can be one of "debug", "info", "warn" or "error"
level = "info"
file = "stdout" # stdout to log to standard out
file = "stdout" # stdout to log to standard out, or syslog
# Configure the admin server
[admin]