2024-10-17 16:16:37 +01:00
|
|
|
// Copyright 2024 New Vector Ltd.
|
2021-11-02 16:50:59 +00:00
|
|
|
// Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
2024-10-18 15:14:08 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2021-11-02 16:50:59 +00:00
|
|
|
|
2021-12-16 15:37:01 +00:00
|
|
|
//go:build !windows
|
2021-11-02 16:50:59 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2022-08-05 10:26:59 +01:00
|
|
|
"io"
|
2021-11-02 16:50:59 +00:00
|
|
|
"log/syslog"
|
|
|
|
|
2021-12-16 15:37:01 +00:00
|
|
|
"github.com/MFAshby/stdemuxerhook"
|
2021-11-02 16:50:59 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
|
2022-12-08 07:24:06 +00:00
|
|
|
|
2024-10-17 16:33:45 +01:00
|
|
|
"github.com/element-hq/dendrite/setup/config"
|
2021-11-02 16:50:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetupHookLogging configures the logging hooks defined in the configuration.
|
|
|
|
// If something fails here it means that the logging was improperly configured,
|
|
|
|
// so we just exit with the error
|
2023-02-14 11:47:47 +00:00
|
|
|
func SetupHookLogging(hooks []config.LogrusHook) {
|
2023-01-23 17:55:12 +00:00
|
|
|
levelLogAddedMu.Lock()
|
|
|
|
defer levelLogAddedMu.Unlock()
|
2021-11-02 16:50:59 +00:00
|
|
|
for _, hook := range hooks {
|
|
|
|
// Check we received a proper logging level
|
|
|
|
level, err := logrus.ParseLevel(hook.Level)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("Unrecognised logging level %s: %q", hook.Level, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform a first filter on the logs according to the lowest level of all
|
|
|
|
// (Eg: If we have hook for info and above, prevent logrus from processing debug logs)
|
|
|
|
if logrus.GetLevel() < level {
|
|
|
|
logrus.SetLevel(level)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch hook.Type {
|
|
|
|
case "file":
|
|
|
|
checkFileHookParams(hook.Params)
|
2023-02-14 11:47:47 +00:00
|
|
|
setupFileHook(hook, level)
|
2021-11-02 16:50:59 +00:00
|
|
|
case "syslog":
|
|
|
|
checkSyslogHookParams(hook.Params)
|
2023-02-14 11:47:47 +00:00
|
|
|
setupSyslogHook(hook, level)
|
2021-12-16 15:37:01 +00:00
|
|
|
case "std":
|
|
|
|
setupStdLogHook(level)
|
2021-11-02 16:50:59 +00:00
|
|
|
default:
|
|
|
|
logrus.Fatalf("Unrecognised logging hook type: %s", hook.Type)
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 07:24:06 +00:00
|
|
|
setupStdLogHook(logrus.InfoLevel)
|
2021-12-16 15:37:01 +00:00
|
|
|
// Hooks are now configured for stdout/err, so throw away the default logger output
|
2022-08-05 10:26:59 +01:00
|
|
|
logrus.SetOutput(io.Discard)
|
2021-11-02 16:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkSyslogHookParams(params map[string]interface{}) {
|
|
|
|
addr, ok := params["address"]
|
|
|
|
if !ok {
|
|
|
|
logrus.Fatalf("Expecting a parameter \"address\" for logging hook of type \"syslog\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := addr.(string); !ok {
|
|
|
|
logrus.Fatalf("Parameter \"address\" for logging hook of type \"syslog\" should be a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
proto, ok2 := params["protocol"]
|
|
|
|
if !ok2 {
|
|
|
|
logrus.Fatalf("Expecting a parameter \"protocol\" for logging hook of type \"syslog\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok2 := proto.(string); !ok2 {
|
|
|
|
logrus.Fatalf("Parameter \"protocol\" for logging hook of type \"syslog\" should be a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-16 15:37:01 +00:00
|
|
|
func setupStdLogHook(level logrus.Level) {
|
2022-12-08 07:24:06 +00:00
|
|
|
if stdLevelLogAdded[level] {
|
|
|
|
return
|
|
|
|
}
|
2021-12-16 15:37:01 +00:00
|
|
|
logrus.AddHook(&logLevelHook{level, stdemuxerhook.New(logrus.StandardLogger())})
|
2022-12-08 07:24:06 +00:00
|
|
|
stdLevelLogAdded[level] = true
|
2021-12-16 15:37:01 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 11:47:47 +00:00
|
|
|
func setupSyslogHook(hook config.LogrusHook, level logrus.Level) {
|
|
|
|
syslogHook, err := lSyslog.NewSyslogHook(hook.Params["protocol"].(string), hook.Params["address"].(string), syslog.LOG_INFO, "dendrite")
|
2021-11-02 16:50:59 +00:00
|
|
|
if err == nil {
|
|
|
|
logrus.AddHook(&logLevelHook{level, syslogHook})
|
|
|
|
}
|
|
|
|
}
|