Refactoring Logging (#1722)

* Added ParseLevel to set log level (#1671)
* Moved logger config into the logger package for better decoupling. Added logging tests.

Co-authored-by: Amal Alkhamees <Amalkh5@users.noreply.github.com>
pull/1749/head
Jordan Wright 2020-01-16 22:21:58 -06:00 committed by GitHub
parent 546da4ee7d
commit caede2e40b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 20 deletions

View File

@ -16,6 +16,7 @@
"migrations_prefix": "db/db_", "migrations_prefix": "db/db_",
"contact_address": "", "contact_address": "",
"logging": { "logging": {
"filename": "" "filename": "",
"level": ""
} }
} }

View File

@ -2,6 +2,7 @@ package config
import ( import (
"encoding/json" "encoding/json"
log "github.com/gophish/gophish/logger"
"io/ioutil" "io/ioutil"
) )
@ -21,22 +22,17 @@ type PhishServer struct {
KeyPath string `json:"key_path"` KeyPath string `json:"key_path"`
} }
// LoggingConfig represents configuration details for Gophish logging.
type LoggingConfig struct {
Filename string `json:"filename"`
}
// Config represents the configuration information. // Config represents the configuration information.
type Config struct { type Config struct {
AdminConf AdminServer `json:"admin_server"` AdminConf AdminServer `json:"admin_server"`
PhishConf PhishServer `json:"phish_server"` PhishConf PhishServer `json:"phish_server"`
DBName string `json:"db_name"` DBName string `json:"db_name"`
DBPath string `json:"db_path"` DBPath string `json:"db_path"`
DBSSLCaPath string `json:"db_sslca_path"` DBSSLCaPath string `json:"db_sslca_path"`
MigrationsPath string `json:"migrations_prefix"` MigrationsPath string `json:"migrations_prefix"`
TestFlag bool `json:"test_flag"` TestFlag bool `json:"test_flag"`
ContactAddress string `json:"contact_address"` ContactAddress string `json:"contact_address"`
Logging LoggingConfig `json:"logging"` Logging *log.Config `json:"logging"`
} }
// Version contains the current gophish version // Version contains the current gophish version

View File

@ -69,7 +69,7 @@ func main() {
} }
config.Version = string(version) config.Version = string(version)
err = log.Setup(conf) err = log.Setup(conf.Logging)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -1,10 +1,10 @@
package logger package logger
import ( import (
"errors"
"io" "io"
"os" "os"
"github.com/gophish/gophish/config"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -12,16 +12,34 @@ import (
// It is exported here for use with gorm. // It is exported here for use with gorm.
var Logger *logrus.Logger var Logger *logrus.Logger
// ErrInvalidLevel is returned when an invalid log level is given in the config
var ErrInvalidLevel = errors.New("invalid log level")
// Config represents configuration details for logging.
type Config struct {
Filename string `json:"filename"`
Level string `json:"level"`
}
func init() { func init() {
Logger = logrus.New() Logger = logrus.New()
Logger.Formatter = &logrus.TextFormatter{DisableColors: true} Logger.Formatter = &logrus.TextFormatter{DisableColors: true}
} }
// Setup configures the logger based on options in the config.json. // Setup configures the logger based on options in the config.json.
func Setup(conf *config.Config) error { func Setup(config *Config) error {
Logger.SetLevel(logrus.InfoLevel) var err error
// Set up logging level
level := logrus.InfoLevel
if config.Level != "" {
level, err = logrus.ParseLevel(config.Level)
if err != nil {
return err
}
}
Logger.SetLevel(level)
// Set up logging to a file if specified in the config // Set up logging to a file if specified in the config
logFile := conf.Logging.Filename logFile := config.Filename
if logFile != "" { if logFile != "" {
f, err := os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) f, err := os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil { if err != nil {

26
logger/logger_test.go Normal file
View File

@ -0,0 +1,26 @@
package logger
import "testing"
import "github.com/sirupsen/logrus"
func TestLogLevel(t *testing.T) {
tests := map[string]logrus.Level{
"": logrus.InfoLevel,
"debug": logrus.DebugLevel,
"info": logrus.InfoLevel,
"error": logrus.ErrorLevel,
"fatal": logrus.FatalLevel,
}
config := &Config{}
for level, expected := range tests {
config.Level = level
err := Setup(config)
if err != nil {
t.Fatalf("error setting logging level %v", err)
}
if Logger.Level != expected {
t.Fatalf("invalid logging level. expected %v got %v", expected, Logger.Level)
}
}
}