Browse Source

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 1 year ago
committed by GitHub
parent
commit
caede2e40b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 20 deletions
  1. +2
    -1
      config.json
  2. +10
    -14
      config/config.go
  3. +1
    -1
      gophish.go
  4. +22
    -4
      logger/logger.go
  5. +26
    -0
      logger/logger_test.go

+ 2
- 1
config.json View File

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

+ 10
- 14
config/config.go View File

@ -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

+ 1
- 1
gophish.go View File

@ -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)
}

+ 22
- 4
logger/logger.go View File

@ -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
- 0
logger/logger_test.go 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)
}
}
}

Loading…
Cancel
Save