Skip to content

Commit

Permalink
structured logging with zap (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
furusiyya authored and glaslos committed Jun 6, 2017
1 parent 27bf45d commit fad46eb
Show file tree
Hide file tree
Showing 20 changed files with 231 additions and 184 deletions.
25 changes: 4 additions & 21 deletions app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ package main
import (
"flag"
"fmt"
"io"
"os"
"os/signal"
"sync"

log "github.com/Sirupsen/logrus"
"github.com/mushorg/glutton"
"github.com/mushorg/glutton/config"
)

func onErrorExit(err error) {
if err != nil {
log.Fatalf("[glutton ] %+v", err)
fmt.Println("[glutton ] %+v", err)
os.Exit(0)
}
}

Expand Down Expand Up @@ -46,22 +44,7 @@ func main() {
enableDebug := flag.Bool("debug", false, "Set to enable debug log")
flag.Parse()

// Setting up the logger
logger := log.New()
// Write log to file and stdout
f, err := os.OpenFile(*logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
onErrorExit(err)
logger.Out = io.MultiWriter(f, os.Stdout)
if *enableDebug == true {
logger.Level = log.DebugLevel
}
logger.Formatter = &log.TextFormatter{ForceColors: true}

// Loading the congiguration
logger.Info("[glutton ] Loading configurations from: config/conf.yaml")
conf := config.Init(*confPath, logger)

gtn, err := glutton.New(*iface, conf, logger)
gtn, err := glutton.New(iface, confPath, logPath, enableDebug)
onErrorExit(err)

err = gtn.Init()
Expand All @@ -73,7 +56,7 @@ func main() {
fmt.Fprintln(os.Stderr, recover())
exitMtx.Lock()
println() // make it look nice after the ^C
logger.Info("[glutton ] shutting down...")
fmt.Println("[glutton ] shutting down...")

// TODO
// Close connections on shutdown.
Expand Down
4 changes: 2 additions & 2 deletions config/conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ ports:

rules_path: rules/rules.yaml

# TODO: Provide flag to active or deactive producer
# Glutton will produce events at the following address
gollum: http://gollum:gollum@localhost:9000
enableGollum: false
gollumAddress: http://gollum:gollum@localhost:9000

# Address of SSH server behind proxy
# Example:
Expand Down
35 changes: 18 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
package config

import (
"fmt"
"strconv"

log "github.com/Sirupsen/logrus"
"github.com/spf13/viper"
"go.uber.org/zap"
)

// Init initializes the configuration
func Init(confPath string, logger *log.Logger) (v *viper.Viper) {
func Init(confPath *string, logger *zap.Logger) (v *viper.Viper) {

v = viper.New()

// Loading config file
v.SetConfigName("conf")
v.AddConfigPath(confPath)
v.AddConfigPath(*confPath)
err := v.ReadInConfig()
if err != nil {
logger.Errorf("[glutton ] No configuration file loaded - using defaults: %s", err)
logger.Warn(fmt.Sprintf("[glutton ] no configuration file loaded - using defaults, error: %v", err))
}
validate(logger, v)

// If no config is found, use the defaults
v.SetDefault("glutton_server", 5000)
v.SetDefault("proxy_tcp", 6000)
v.SetDefault("rules_path", "rules/rules.yaml")
v.SetDefault("gollum", "http://gollum:gollum@localhost:9000")
v.SetDefault("gollumAddress", "http://gollum:gollum@localhost:9000")
v.SetDefault("enableGollum", false)
v.SetDefault("proxy_ssh", "tcp://localhost:22")

logger.Debug("[glutton ] Configuration file loaded successfully")
logger.Info("[glutton ] configuration loaded successfully")
return
}

func validate(logger *log.Logger, v *viper.Viper) {
func validate(logger *zap.Logger, v *viper.Viper) {

ports := v.GetStringMapString("ports")
if ports == nil {
Expand All @@ -41,11 +43,11 @@ func validate(logger *log.Logger, v *viper.Viper) {

for key, value := range ports {
if key != "glutton_server" && key != "proxy_tcp" {
logger.Errorf("[glutton ] Invalid key found: %s", key)
logger.Error(fmt.Sprintf("[glutton ] invalid key found. key: %s", key))
continue
}
if port, err := strconv.Atoi(value); err != nil {
logger.Debugf("[glutton ] Using default value for ports:%s", key)
logger.Debug(fmt.Sprintf("[glutton ] using default value for port: %s", key))
} else {
v.Set(key, port)
}
Expand All @@ -55,16 +57,15 @@ func validate(logger *log.Logger, v *viper.Viper) {
p := sshProxy.([]interface{})
v.Set("proxy_ssh", p[0].(string))
} else {
logger.Debug("[glutton ] Using default value for proxy_ssh")
}

if v.GetString("rules_path") == "" {
logger.Debug("[glutton ] Using default value for rules_path")
logger.Debug("[glutton ] using default value for proxy_ssh")
}
if v.GetString("log_path") == "" {
logger.Debug("[glutton ] Using default value for log_path")
logger.Debug("[glutton ] using default value for log_path")
}
if v.GetString("gollum") == "" {
logger.Debug("[glutton ] Using default value for gollum")
if v.GetBool("enableGollum") == true {
if v.GetString("gollumAddress") == "" {
logger.Debug("[glutton ] using default value for gollum address")
}
}

}
7 changes: 4 additions & 3 deletions ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package glutton

import (
"bufio"
"fmt"
"net"
"strings"
)

func readFTP(conn net.Conn, g *Glutton) (msg string, err error) {
msg, err = bufio.NewReader(conn).ReadString('\n')
if err != nil {
g.logger.Errorf("[ftp ] error: %v", err)
g.logger.Error(fmt.Sprintf("[ftp ] error: %v", err))
}
g.logger.Infof("[ftp ] recv: %q", msg)
g.logger.Info(fmt.Sprintf("[ftp ] recv: %q", msg))
return
}

Expand All @@ -20,7 +21,7 @@ func (g *Glutton) HandleFTP(conn net.Conn) (err error) {
defer func() {
err = conn.Close()
if err != nil {
g.logger.Errorf("[ftp ] %v", err)
g.logger.Error(fmt.Sprintf("[ftp ] error: %v", err))
}
}()

Expand Down
21 changes: 15 additions & 6 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import:
- server
- sipnet
- package: github.com/kung-foo/freki
version: ^1.1.0
- package: github.com/Sirupsen/logrus
version: ^0.11.0
version: b4042f6dd12c8d141636194fa79c028226ac3ec8
- package: go.uber.org/zap
version: ^1.4.0
- package: gopkg.in/yaml.v2
- package: github.com/lunixbochs/vtclean
version: d14193dfc626125c831501c1c42340b4248e1f5a
- package: github.com/spf13/viper
Loading

0 comments on commit fad46eb

Please sign in to comment.