mirror of
https://github.com/gophish/gophish
synced 2024-11-12 23:37:11 +00:00
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>
This commit is contained in:
parent
546da4ee7d
commit
caede2e40b
5 changed files with 61 additions and 20 deletions
|
@ -16,6 +16,7 @@
|
|||
"migrations_prefix": "db/db_",
|
||||
"contact_address": "",
|
||||
"logging": {
|
||||
"filename": ""
|
||||
"filename": "",
|
||||
"level": ""
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ package config
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
log "github.com/gophish/gophish/logger"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
|
@ -21,22 +22,17 @@ type PhishServer struct {
|
|||
KeyPath string `json:"key_path"`
|
||||
}
|
||||
|
||||
// LoggingConfig represents configuration details for Gophish logging.
|
||||
type LoggingConfig struct {
|
||||
Filename string `json:"filename"`
|
||||
}
|
||||
|
||||
// Config represents the configuration information.
|
||||
type Config struct {
|
||||
AdminConf AdminServer `json:"admin_server"`
|
||||
PhishConf PhishServer `json:"phish_server"`
|
||||
DBName string `json:"db_name"`
|
||||
DBPath string `json:"db_path"`
|
||||
DBSSLCaPath string `json:"db_sslca_path"`
|
||||
MigrationsPath string `json:"migrations_prefix"`
|
||||
TestFlag bool `json:"test_flag"`
|
||||
ContactAddress string `json:"contact_address"`
|
||||
Logging LoggingConfig `json:"logging"`
|
||||
AdminConf AdminServer `json:"admin_server"`
|
||||
PhishConf PhishServer `json:"phish_server"`
|
||||
DBName string `json:"db_name"`
|
||||
DBPath string `json:"db_path"`
|
||||
DBSSLCaPath string `json:"db_sslca_path"`
|
||||
MigrationsPath string `json:"migrations_prefix"`
|
||||
TestFlag bool `json:"test_flag"`
|
||||
ContactAddress string `json:"contact_address"`
|
||||
Logging *log.Config `json:"logging"`
|
||||
}
|
||||
|
||||
// Version contains the current gophish version
|
||||
|
|
|
@ -69,7 +69,7 @@ func main() {
|
|||
}
|
||||
config.Version = string(version)
|
||||
|
||||
err = log.Setup(conf)
|
||||
err = log.Setup(conf.Logging)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/gophish/gophish/config"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
|
@ -12,16 +12,34 @@ import (
|
|||
// It is exported here for use with gorm.
|
||||
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() {
|
||||
Logger = logrus.New()
|
||||
Logger.Formatter = &logrus.TextFormatter{DisableColors: true}
|
||||
}
|
||||
|
||||
// Setup configures the logger based on options in the config.json.
|
||||
func Setup(conf *config.Config) error {
|
||||
Logger.SetLevel(logrus.InfoLevel)
|
||||
func Setup(config *Config) error {
|
||||
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
|
||||
logFile := conf.Logging.Filename
|
||||
logFile := config.Filename
|
||||
if logFile != "" {
|
||||
f, err := os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
|
|
26
logger/logger_test.go
Normal file
26
logger/logger_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue