Skip to content

Commit

Permalink
Logging to syslog instead of stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
agnivade committed Nov 15, 2016
1 parent cdfa4a7 commit 3c079da
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
9 changes: 8 additions & 1 deletion cmd/funnel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log/syslog"
"os"

"github.com/agnivade/funnel"
Expand All @@ -14,6 +15,11 @@ const (
)

func main() {
logger, err := syslog.New(syslog.LOG_ERR, AppName)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Verifying whether the app has a piped stdin or not
fi, err := os.Stdin.Stat()
if err != nil {
Expand All @@ -33,7 +39,7 @@ func main() {
v.AddConfigPath(".")

// Read config
cfg, reloadChan, err := funnel.GetConfig(v)
cfg, reloadChan, err := funnel.GetConfig(v, logger)
if err != nil {
fmt.Println("Error in config file: ", err)
os.Exit(1)
Expand All @@ -47,6 +53,7 @@ func main() {
Config: cfg,
LineProcessor: lp,
ReloadChan: reloadChan,
Logger: logger,
}
c.Start(os.Stdin)
}
7 changes: 3 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package funnel

import (
"errors"
"fmt"
"os"
"log/syslog"
"strconv"
"strings"

Expand Down Expand Up @@ -67,7 +66,7 @@ func init() {

// GetConfig returns the config struct which is then passed
// to the consumer
func GetConfig(v *viper.Viper) (*Config, chan *Config, error) {
func GetConfig(v *viper.Viper, logger *syslog.Writer) (*Config, chan *Config, error) {
// Set default values. They are overridden by config file values, if provided
setDefaults(v)
// Create a chan to signal any config reload events
Expand All @@ -93,7 +92,7 @@ func GetConfig(v *viper.Viper) (*Config, chan *Config, error) {
v.OnConfigChange(func(e fsnotify.Event) {
if e.Op == fsnotify.Write {
if err := validateConfig(v); err != nil {
fmt.Fprintln(os.Stderr, err)
logger.Err(err.Error())
return
}
reloadChan <- getConfigStruct(v)
Expand Down
17 changes: 11 additions & 6 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package funnel

import (
"log/syslog"
"os"
"reflect"
"testing"
Expand All @@ -13,8 +14,9 @@ func TestSanity(t *testing.T) {
v := viper.New()
v.SetConfigName("goodconfig")
v.AddConfigPath("./testdata/")
logger, _ := syslog.New(syslog.LOG_ERR, "test")

cfg, _, err := GetConfig(v)
cfg, _, err := GetConfig(v, logger)
if err != nil {
t.Fatal(err)
return
Expand Down Expand Up @@ -48,8 +50,9 @@ func TestBadFile(t *testing.T) {
v := viper.New()
v.SetConfigName("badsyntaxconfig")
v.AddConfigPath("./testdata/")
logger, _ := syslog.New(syslog.LOG_ERR, "test")

_, _, err := GetConfig(v)
_, _, err := GetConfig(v, logger)
if err == nil {
t.Error("Expected error in config file, got none")
}
Expand All @@ -59,8 +62,9 @@ func TestInvalidConfigValue(t *testing.T) {
v := viper.New()
v.SetConfigName("invalidvalueconfig")
v.AddConfigPath("./testdata/")
logger, _ := syslog.New(syslog.LOG_ERR, "test")

_, _, err := GetConfig(v)
_, _, err := GetConfig(v, logger)
if err == nil {
t.Error("Expected error in config file, got none")
}
Expand All @@ -75,12 +79,12 @@ func TestNoConfigFile(t *testing.T) {
v := viper.New()
v.SetConfigName("noconfig")
v.AddConfigPath("./testdata/")
logger, _ := syslog.New(syslog.LOG_ERR, "test")

_, _, err := GetConfig(v)
_, _, err := GetConfig(v, logger)
if err != nil {
t.Error("Did not expect an error for config file not being present. Got - ", err)
}

}

func TestEnvVars(t *testing.T) {
Expand All @@ -89,8 +93,9 @@ func TestEnvVars(t *testing.T) {
v.AddConfigPath("./testdata/")
envValue := "env_var_value"
os.Setenv("LOGGING_DIRECTORY", envValue)
logger, _ := syslog.New(syslog.LOG_ERR, "test")

cfg, _, err := GetConfig(v)
cfg, _, err := GetConfig(v, logger)
if err != nil {
t.Fatal(err)
return
Expand Down
21 changes: 11 additions & 10 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package funnel

import (
"bufio"
"fmt"
"io"
"log/syslog"
"os"
"os/signal"
"path"
Expand All @@ -17,6 +17,7 @@ import (
type Consumer struct {
Config *Config
LineProcessor LineProcessor
Logger *syslog.Writer

// internal stuff
currFile *os.File
Expand Down Expand Up @@ -46,13 +47,13 @@ func (c *Consumer) Start(inputStream io.Reader) {

// Make the dir along with parents
if err := os.MkdirAll(c.Config.DirName, 0775); err != nil {
fmt.Fprintln(os.Stderr, err)
c.Logger.Err(err.Error())
return
}

// Create the file
if err := c.createNewFile(); err != nil {
fmt.Fprintln(os.Stderr, err)
c.Logger.Err(err.Error())
return
}

Expand Down Expand Up @@ -96,7 +97,7 @@ outer:
}
case err := <-c.errChan: // error channel to get any errors happening
// elsewhere. After printing to stderr, it breaks from the loop
fmt.Fprintln(os.Stderr, err)
c.Logger.Err(err.Error())
break outer
default:
// This will return a line until delimiter
Expand All @@ -120,7 +121,7 @@ outer:

if err != nil {
if err != io.EOF {
fmt.Fprintln(os.Stderr, err)
c.Logger.Err(err.Error())
}
break outer
}
Expand All @@ -138,24 +139,24 @@ func (c *Consumer) cleanUp() {
var err error
// Close file handle
if err = c.currFile.Sync(); err != nil {
fmt.Fprintln(os.Stderr, err)
c.Logger.Err(err.Error())
return
}

if err = c.currFile.Close(); err != nil {
fmt.Fprintln(os.Stderr, err)
c.Logger.Err(err.Error())
return
}

// Rename the currfile to a rolled up one
var fileName string
if fileName, err = c.rename(); err != nil {
fmt.Fprintln(os.Stderr, err)
c.Logger.Err(err.Error())
return
}

if err = c.compress(fileName); err != nil {
fmt.Fprintln(os.Stderr, err)
c.Logger.Err(err.Error())
return
}
}
Expand Down Expand Up @@ -260,7 +261,7 @@ func (c *Consumer) startFeed() {
case <-c.done: // Done signal received, close shop
ticker.Stop()
if err := c.writer.Flush(); err != nil {
fmt.Fprintln(os.Stderr, err)
c.Logger.Err(err.Error())
}
c.cleanUp()
c.wg.Done()
Expand Down

0 comments on commit 3c079da

Please sign in to comment.