Skip to content
This repository was archived by the owner on Mar 16, 2021. It is now read-only.

Commit fbf2344

Browse files
memorymxschmitt
authored andcommitted
Add config option to disable log coloration (#103)
* Add config option to disable log coloration Color logs are great in person, but not so awesome when you're trying to view them in a log aggregation services e.g. splunk, stackdriver, ELK, etc. * dump running config to log on startup * init config before testing ...and use a temporary directory for config_test, lest the presence of a local one cause confusing test results.
1 parent b92f3df commit fbf2344

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

build/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ RedisHost: localhost:6379 # If using the redis backend, a host:port combination
55
RedisPassword: replace me # if using the redis backend, a conneciton password.
66
DataDir: ./data # Contains: the database and the private key
77
EnableDebugMode: true # Activates more detailed logging
8+
EnableColorLogs: true # Enables/disables ANSI color sequences in log output; default is true
89
ShortedIDLength: 10 # Length of the random generated ID which is used for new shortened URLs
910
AuthBackend: oauth # Can be 'oauth' or 'proxy'
1011
Google: # only relevant when using the oauth authbackend

main.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@ import (
1414

1515
func main() {
1616
stop := make(chan os.Signal, 1)
17+
if err := initConfig(); err != nil {
18+
logrus.Fatal(err)
19+
}
1720
signal.Notify(stop, os.Interrupt)
1821
logrus.SetFormatter(&logrus.TextFormatter{
19-
ForceColors: true,
22+
ForceColors: util.GetConfig().EnableColorLogs,
23+
DisableColors: !util.GetConfig().EnableColorLogs,
2024
})
21-
logrus.SetOutput(ansicolor.NewAnsiColorWriter(os.Stdout))
25+
if util.GetConfig().EnableColorLogs == true {
26+
logrus.SetOutput(ansicolor.NewAnsiColorWriter(os.Stdout))
27+
} else {
28+
logrus.SetOutput(os.Stdout)
29+
}
2230
close, err := initShortener()
2331
if err != nil {
2432
logrus.Fatalf("could not init shortener: %v", err)
@@ -28,10 +36,14 @@ func main() {
2836
close()
2937
}
3038

31-
func initShortener() (func(), error) {
39+
func initConfig() error {
3240
if err := util.ReadInConfig(); err != nil {
33-
return nil, errors.Wrap(err, "could not reload config file")
41+
return errors.Wrap(err, "could not load config")
3442
}
43+
return nil
44+
}
45+
46+
func initShortener() (func(), error) {
3547
if util.GetConfig().EnableDebugMode {
3648
logrus.SetLevel(logrus.DebugLevel)
3749
}

main_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"io/ioutil"
45
"net"
56
"testing"
67
"time"
@@ -9,6 +10,14 @@ import (
910
)
1011

1112
func TestInitShortener(t *testing.T) {
13+
tmpdir, err := ioutil.TempDir("", "shorten")
14+
if err != nil {
15+
t.Fatal(err)
16+
}
17+
util.Config.DataDir = tmpdir
18+
if err = initConfig(); err != nil {
19+
t.Fatal(err)
20+
}
1221
close, err := initShortener()
1322
if err != nil {
1423
t.Fatalf("could not init shortener: %v", err)

util/config.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Configuration struct {
2323
AuthBackend string `yaml:"AuthBackend" env:"AUTH_BACKEND"`
2424
UseSSL bool `yaml:"EnableSSL" env:"USE_SSL"`
2525
EnableDebugMode bool `yaml:"EnableDebugMode" env:"ENABLE_DEBUG_MODE"`
26+
EnableColorLogs bool `yaml:"EnableColorLogs" env:"ENABLE_COLOR_LOGS"`
2627
ShortedIDLength int `yaml:"ShortedIDLength" env:"SHORTED_ID_LENGTH"`
2728
Google oAuthConf `yaml:"Google" env:"GOOGLE"`
2829
GitHub oAuthConf `yaml:"GitHub" env:"GITHUB"`
@@ -42,12 +43,13 @@ type proxyAuthConf struct {
4243
}
4344

4445
// config contains the default values
45-
var config = Configuration{
46+
var Config = Configuration{
4647
ListenAddr: ":8080",
4748
BaseURL: "http://localhost:3000",
4849
DataDir: "data",
4950
Backend: "boltdb",
5051
EnableDebugMode: false,
52+
EnableColorLogs: true,
5153
UseSSL: false,
5254
ShortedIDLength: 4,
5355
AuthBackend: "oauth",
@@ -57,23 +59,24 @@ var config = Configuration{
5759
func ReadInConfig() error {
5860
file, err := ioutil.ReadFile("config.yaml")
5961
if err == nil {
60-
if err := yaml.Unmarshal(file, &config); err != nil {
62+
if err := yaml.Unmarshal(file, &Config); err != nil {
6163
return errors.Wrap(err, "could not unmarshal yaml file")
6264
}
6365
} else if !os.IsNotExist(err) {
6466
return errors.Wrap(err, "could not read config file")
6567
} else {
6668
logrus.Info("No configuration file found, using defaults with environment variable overrides.")
6769
}
68-
if err := envstruct.ApplyEnvVars(&config, "GUS"); err != nil {
70+
if err := envstruct.ApplyEnvVars(&Config, "GUS"); err != nil {
6971
return errors.Wrap(err, "could not apply environment configuration")
7072
}
71-
config.DataDir, err = filepath.Abs(config.DataDir)
73+
logrus.Info("Loaded configuration: %v", Config)
74+
Config.DataDir, err = filepath.Abs(Config.DataDir)
7275
if err != nil {
7376
return errors.Wrap(err, "could not get relative data dir path")
7477
}
75-
if _, err = os.Stat(config.DataDir); os.IsNotExist(err) {
76-
if err = os.MkdirAll(config.DataDir, 0755); err != nil {
78+
if _, err = os.Stat(Config.DataDir); os.IsNotExist(err) {
79+
if err = os.MkdirAll(Config.DataDir, 0755); err != nil {
7780
return errors.Wrap(err, "could not create config directory")
7881
}
7982
}
@@ -86,10 +89,10 @@ func (o oAuthConf) Enabled() bool {
8689

8790
// GetConfig returns the configuration from the memory
8891
func GetConfig() Configuration {
89-
return config
92+
return Config
9093
}
9194

9295
// SetConfig sets the configuration into the memory
9396
func SetConfig(c Configuration) {
94-
config = c
97+
Config = c
9598
}

util/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ func TestReadInConfig(t *testing.T) {
88
if err := ReadInConfig(); err != nil {
99
t.Fatalf("could not read in config file: %v", err)
1010
}
11-
config := config
11+
config := Config
1212
config.DataDir = "./test"
1313
}

util/private.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var privateKey []byte
1414
// CheckForPrivateKey checks if already an private key exists, if not it will
1515
// be randomly generated and saved as a private.dat file in the data directory
1616
func CheckForPrivateKey() error {
17-
privateDatPath := filepath.Join(config.DataDir, "private.dat")
17+
privateDatPath := filepath.Join(Config.DataDir, "private.dat")
1818
privateDatContent, err := ioutil.ReadFile(privateDatPath)
1919
if err == nil {
2020
privateKey = privateDatContent

util/private_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestCheckforPrivateKey(t *testing.T) {
1414
if GetPrivateKey() == nil {
1515
t.Fatalf("private key is nil")
1616
}
17-
if err := os.RemoveAll(config.DataDir); err != nil {
17+
if err := os.RemoveAll(Config.DataDir); err != nil {
1818
t.Fatalf("could not remove data dir: %v", err)
1919
}
2020
}

0 commit comments

Comments
 (0)