From 3c079dac2b8f8fc917d0f8e2c72d66ff8e4cc4f5 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Mon, 14 Nov 2016 23:34:22 +0530 Subject: [PATCH] Logging to syslog instead of stderr --- cmd/funnel/main.go | 9 ++++++++- config.go | 7 +++---- config_test.go | 17 +++++++++++------ consumer.go | 21 +++++++++++---------- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/cmd/funnel/main.go b/cmd/funnel/main.go index bc1929b..60349c3 100644 --- a/cmd/funnel/main.go +++ b/cmd/funnel/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "log/syslog" "os" "github.com/agnivade/funnel" @@ -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 { @@ -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) @@ -47,6 +53,7 @@ func main() { Config: cfg, LineProcessor: lp, ReloadChan: reloadChan, + Logger: logger, } c.Start(os.Stdin) } diff --git a/config.go b/config.go index d5185ed..db28d51 100644 --- a/config.go +++ b/config.go @@ -2,8 +2,7 @@ package funnel import ( "errors" - "fmt" - "os" + "log/syslog" "strconv" "strings" @@ -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 @@ -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) diff --git a/config_test.go b/config_test.go index 63335b1..f294ddc 100644 --- a/config_test.go +++ b/config_test.go @@ -1,6 +1,7 @@ package funnel import ( + "log/syslog" "os" "reflect" "testing" @@ -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 @@ -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") } @@ -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") } @@ -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) { @@ -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 diff --git a/consumer.go b/consumer.go index 12afe8d..401e81c 100644 --- a/consumer.go +++ b/consumer.go @@ -2,8 +2,8 @@ package funnel import ( "bufio" - "fmt" "io" + "log/syslog" "os" "os/signal" "path" @@ -17,6 +17,7 @@ import ( type Consumer struct { Config *Config LineProcessor LineProcessor + Logger *syslog.Writer // internal stuff currFile *os.File @@ -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 } @@ -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 @@ -120,7 +121,7 @@ outer: if err != nil { if err != io.EOF { - fmt.Fprintln(os.Stderr, err) + c.Logger.Err(err.Error()) } break outer } @@ -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 } } @@ -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()