summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/config/defaults.go4
-rw-r--r--internal/config/keys.go9
-rw-r--r--internal/config/values.go4
-rw-r--r--internal/log/log.go53
-rw-r--r--internal/log/syslog_test.go70
-rw-r--r--internal/media/util_test.go10
6 files changed, 134 insertions, 16 deletions
diff --git a/internal/config/defaults.go b/internal/config/defaults.go
index 01eef8c8c..f1666e1a5 100644
--- a/internal/config/defaults.go
+++ b/internal/config/defaults.go
@@ -84,4 +84,8 @@ var Defaults = Values{
SMTPUsername: "",
SMTPPassword: "",
SMTPFrom: "GoToSocial",
+
+ SyslogEnabled: false,
+ SyslogProtocol: "udp",
+ SyslogAddress: "localhost:514",
}
diff --git a/internal/config/keys.go b/internal/config/keys.go
index 80ca36381..a451a8dc6 100644
--- a/internal/config/keys.go
+++ b/internal/config/keys.go
@@ -95,6 +95,11 @@ type KeyNames struct {
SMTPPassword string
SMTPFrom string
+ // syslog
+ SyslogEnabled string
+ SyslogProtocol string
+ SyslogAddress string
+
// admin
AdminAccountUsername string
AdminAccountEmail string
@@ -168,6 +173,10 @@ var Keys = KeyNames{
SMTPPassword: "smtp-password",
SMTPFrom: "smtp-from",
+ SyslogEnabled: "syslog-enabled",
+ SyslogProtocol: "syslog-protocol",
+ SyslogAddress: "syslog-address",
+
AdminAccountUsername: "username",
AdminAccountEmail: "email",
AdminAccountPassword: "password",
diff --git a/internal/config/values.go b/internal/config/values.go
index 387f934d8..a8ffed1af 100644
--- a/internal/config/values.go
+++ b/internal/config/values.go
@@ -83,6 +83,10 @@ type Values struct {
SMTPPassword string
SMTPFrom string
+ SyslogEnabled bool
+ SyslogProtocol string
+ SyslogAddress string
+
AdminAccountUsername string
AdminAccountEmail string
AdminAccountPassword string
diff --git a/internal/log/log.go b/internal/log/log.go
index b06146368..59c994035 100644
--- a/internal/log/log.go
+++ b/internal/log/log.go
@@ -22,31 +22,60 @@ import (
"bytes"
"os"
+ "log/syslog"
+
"github.com/sirupsen/logrus"
+ lSyslog "github.com/sirupsen/logrus/hooks/syslog"
+ "github.com/spf13/viper"
+ "github.com/superseriousbusiness/gotosocial/internal/config"
)
-// Initialize initializes the global Logrus logger to the specified level
+// Initialize initializes the global Logrus logger, reading the desired
+// log level from the viper store, or using a default if the level
+// has not been set in viper.
+//
// It also sets the output to log.outputSplitter,
// so you get error logs on stderr and normal logs on stdout.
-func Initialize(level string) error {
+//
+// If syslog settings are also in viper, then Syslog will be initialized as well.
+func Initialize() error {
logrus.SetOutput(&outputSplitter{})
- logLevel, err := logrus.ParseLevel(level)
- if err != nil {
- return err
- }
- logrus.SetLevel(logLevel)
-
- if logLevel == logrus.TraceLevel {
- logrus.SetReportCaller(true)
- }
-
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: true,
DisableQuote: true,
FullTimestamp: true,
})
+ keys := config.Keys
+
+ // check if a desired log level has been set
+ logLevel := viper.GetString(keys.LogLevel)
+ if logLevel != "" {
+ level, err := logrus.ParseLevel(logLevel)
+ if err != nil {
+ return err
+ }
+ logrus.SetLevel(level)
+
+ if level == logrus.TraceLevel {
+ logrus.SetReportCaller(true)
+ }
+ }
+
+ // check if syslog has been enabled, and configure it if so
+ if syslogEnabled := viper.GetBool(keys.SyslogEnabled); syslogEnabled {
+ protocol := viper.GetString(keys.SyslogProtocol)
+ address := viper.GetString(keys.SyslogAddress)
+
+ hook, err := lSyslog.NewSyslogHook(protocol, address, syslog.LOG_INFO, "")
+ if err != nil {
+ return err
+ }
+
+ logrus.AddHook(hook)
+ }
+
return nil
}
diff --git a/internal/log/syslog_test.go b/internal/log/syslog_test.go
new file mode 100644
index 000000000..4b6ee14ab
--- /dev/null
+++ b/internal/log/syslog_test.go
@@ -0,0 +1,70 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package log_test
+
+import (
+ "testing"
+
+ "github.com/sirupsen/logrus"
+ "github.com/spf13/viper"
+ "github.com/stretchr/testify/suite"
+ "github.com/superseriousbusiness/gotosocial/internal/config"
+ "github.com/superseriousbusiness/gotosocial/testrig"
+ "gopkg.in/mcuadros/go-syslog.v2"
+ "gopkg.in/mcuadros/go-syslog.v2/format"
+)
+
+type SyslogTestSuite struct {
+ suite.Suite
+ syslogServer *syslog.Server
+ syslogChannel chan format.LogParts
+}
+
+func (suite *SyslogTestSuite) SetupTest() {
+ testrig.InitTestConfig()
+
+ viper.Set(config.Keys.SyslogEnabled, true)
+ viper.Set(config.Keys.SyslogProtocol, "udp")
+ viper.Set(config.Keys.SyslogAddress, "localhost:42069")
+ server, channel, err := testrig.InitTestSyslog()
+ if err != nil {
+ panic(err)
+ }
+ suite.syslogServer = server
+ suite.syslogChannel = channel
+
+ testrig.InitTestLog()
+}
+
+func (suite *SyslogTestSuite) TearDownTest() {
+ if err := suite.syslogServer.Kill(); err != nil {
+ panic(err)
+ }
+}
+
+func (suite *SyslogTestSuite) TestSyslog() {
+ logrus.Warn("this is a test of the emergency broadcast system!")
+
+ message := <-suite.syslogChannel
+ suite.Contains(message["content"], "this is a test of the emergency broadcast system!")
+}
+
+func TestSyslogTestSuite(t *testing.T) {
+ suite.Run(t, &SyslogTestSuite{})
+}
diff --git a/internal/media/util_test.go b/internal/media/util_test.go
index c54fb6f2c..fb36e1476 100644
--- a/internal/media/util_test.go
+++ b/internal/media/util_test.go
@@ -19,11 +19,13 @@
package media
import (
- "github.com/sirupsen/logrus"
- "github.com/superseriousbusiness/gotosocial/internal/log"
"io/ioutil"
"testing"
+ "github.com/spf13/viper"
+ "github.com/superseriousbusiness/gotosocial/internal/config"
+ "github.com/superseriousbusiness/gotosocial/internal/log"
+
"github.com/stretchr/testify/suite"
)
@@ -38,11 +40,11 @@ type MediaUtilTestSuite struct {
// SetupSuite sets some variables on the suite that we can use as consts (more or less) throughout
func (suite *MediaUtilTestSuite) SetupSuite() {
// doesn't use testrig.InitTestLog() helper to prevent import cycle
- err := log.Initialize(logrus.TraceLevel.String())
+ viper.Set(config.Keys.LogLevel, "trace")
+ err := log.Initialize()
if err != nil {
panic(err)
}
-
}
func (suite *MediaUtilTestSuite) TearDownSuite() {