2017-04-20 23:45:18 +00:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-05-21 13:40:13 +00:00
|
|
|
package internal
|
2017-04-20 12:40:56 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-18 10:17:18 +00:00
|
|
|
"context"
|
2019-07-24 04:44:05 +00:00
|
|
|
"fmt"
|
2020-03-18 10:17:18 +00:00
|
|
|
"io"
|
2020-05-11 17:21:39 +00:00
|
|
|
"net/http"
|
2017-04-20 12:40:56 +00:00
|
|
|
"os"
|
2018-04-10 12:21:20 +00:00
|
|
|
"path"
|
2017-04-20 12:40:56 +00:00
|
|
|
"path/filepath"
|
2019-07-24 04:44:05 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2023-01-23 12:17:15 +00:00
|
|
|
"sync"
|
2017-04-20 12:40:56 +00:00
|
|
|
|
2020-03-18 10:17:18 +00:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
|
2017-04-20 12:40:56 +00:00
|
|
|
"github.com/matrix-org/dugong"
|
2017-11-16 10:12:02 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-08-05 09:12:41 +00:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2017-04-20 12:40:56 +00:00
|
|
|
)
|
|
|
|
|
2022-12-08 07:24:06 +00:00
|
|
|
// logrus is using a global variable when we're using `logrus.AddHook`
|
|
|
|
// this unfortunately results in us adding the same hook multiple times.
|
|
|
|
// This map ensures we only ever add one level hook.
|
|
|
|
var stdLevelLogAdded = make(map[logrus.Level]bool)
|
2023-01-23 12:17:15 +00:00
|
|
|
var levelLogAddedMu = &sync.Mutex{}
|
2022-12-08 07:24:06 +00:00
|
|
|
|
2017-04-20 15:15:30 +00:00
|
|
|
type utcFormatter struct {
|
|
|
|
logrus.Formatter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f utcFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|
|
|
entry.Time = entry.Time.UTC()
|
|
|
|
return f.Formatter.Format(entry)
|
|
|
|
}
|
|
|
|
|
2018-04-10 12:21:20 +00:00
|
|
|
// Logrus hook which wraps another hook and filters log entries according to their level.
|
|
|
|
// (Note that we cannot use solely logrus.SetLevel, because Dendrite supports multiple
|
|
|
|
// levels of logging at the same time.)
|
|
|
|
type logLevelHook struct {
|
|
|
|
level logrus.Level
|
|
|
|
logrus.Hook
|
|
|
|
}
|
|
|
|
|
|
|
|
// Levels returns all the levels supported by this hook.
|
|
|
|
func (h *logLevelHook) Levels() []logrus.Level {
|
|
|
|
levels := make([]logrus.Level, 0)
|
|
|
|
|
|
|
|
for _, level := range logrus.AllLevels {
|
|
|
|
if level <= h.level {
|
|
|
|
levels = append(levels, level)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return levels
|
|
|
|
}
|
|
|
|
|
2019-07-24 04:44:05 +00:00
|
|
|
// callerPrettyfier is a function that given a runtime.Frame object, will
|
|
|
|
// extract the calling function's name and file, and return them in a nicely
|
|
|
|
// formatted way
|
|
|
|
func callerPrettyfier(f *runtime.Frame) (string, string) {
|
|
|
|
// Retrieve just the function name
|
|
|
|
s := strings.Split(f.Function, ".")
|
|
|
|
funcname := s[len(s)-1]
|
|
|
|
|
|
|
|
// Append a newline + tab to it to move the actual log content to its own line
|
|
|
|
funcname += "\n\t"
|
|
|
|
|
2021-07-08 12:13:27 +00:00
|
|
|
// Use a shortened file path which just has the filename to avoid having lots of redundant
|
|
|
|
// directories which contribute significantly to overall log sizes!
|
|
|
|
filename := fmt.Sprintf(" [%s:%d]", path.Base(f.File), f.Line)
|
2019-07-24 04:44:05 +00:00
|
|
|
|
|
|
|
return funcname, filename
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:21:39 +00:00
|
|
|
// SetupPprof starts a pprof listener. We use the DefaultServeMux here because it is
|
|
|
|
// simplest, and it gives us the freedom to run pprof on a separate port.
|
|
|
|
func SetupPprof() {
|
|
|
|
if hostPort := os.Getenv("PPROFLISTEN"); hostPort != "" {
|
|
|
|
logrus.Warn("Starting pprof on ", hostPort)
|
|
|
|
go func() {
|
|
|
|
logrus.WithError(http.ListenAndServe(hostPort, nil)).Error("Failed to setup pprof listener")
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 12:21:20 +00:00
|
|
|
// SetupStdLogging configures the logging format to standard output. Typically, it is called when the config is not yet loaded.
|
|
|
|
func SetupStdLogging() {
|
2023-01-23 17:55:12 +00:00
|
|
|
levelLogAddedMu.Lock()
|
|
|
|
defer levelLogAddedMu.Unlock()
|
2019-07-24 04:44:05 +00:00
|
|
|
logrus.SetReportCaller(true)
|
2017-04-21 00:10:54 +00:00
|
|
|
logrus.SetFormatter(&utcFormatter{
|
|
|
|
&logrus.TextFormatter{
|
|
|
|
TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00",
|
|
|
|
FullTimestamp: true,
|
|
|
|
DisableColors: false,
|
|
|
|
DisableTimestamp: false,
|
2019-07-24 04:44:05 +00:00
|
|
|
QuoteEmptyFields: true,
|
|
|
|
CallerPrettyfier: callerPrettyfier,
|
2017-04-21 00:10:54 +00:00
|
|
|
},
|
|
|
|
})
|
2018-04-10 12:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// File type hooks should be provided a path to a directory to store log files
|
|
|
|
func checkFileHookParams(params map[string]interface{}) {
|
|
|
|
path, ok := params["path"]
|
|
|
|
if !ok {
|
|
|
|
logrus.Fatalf("Expecting a parameter \"path\" for logging hook of type \"file\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := path.(string); !ok {
|
|
|
|
logrus.Fatalf("Parameter \"path\" for logging hook of type \"file\" should be a string")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new FSHook to the logger. Each component will log in its own file
|
2023-02-14 11:47:47 +00:00
|
|
|
func setupFileHook(hook config.LogrusHook, level logrus.Level) {
|
2018-04-10 12:21:20 +00:00
|
|
|
dirPath := (hook.Params["path"]).(string)
|
2023-02-14 11:47:47 +00:00
|
|
|
fullPath := filepath.Join(dirPath, "dendrite.log")
|
2018-04-10 12:21:20 +00:00
|
|
|
|
|
|
|
if err := os.MkdirAll(path.Dir(fullPath), os.ModePerm); err != nil {
|
|
|
|
logrus.Fatalf("Couldn't create directory %s: %q", path.Dir(fullPath), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.AddHook(&logLevelHook{
|
|
|
|
level,
|
|
|
|
dugong.NewFSHook(
|
|
|
|
fullPath,
|
2017-04-21 00:06:06 +00:00
|
|
|
&utcFormatter{
|
|
|
|
&logrus.TextFormatter{
|
|
|
|
TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00",
|
|
|
|
DisableColors: true,
|
|
|
|
DisableTimestamp: false,
|
|
|
|
DisableSorting: false,
|
2019-07-24 04:44:05 +00:00
|
|
|
QuoteEmptyFields: true,
|
2017-04-21 00:06:06 +00:00
|
|
|
},
|
|
|
|
},
|
2017-04-20 14:17:43 +00:00
|
|
|
&dugong.DailyRotationSchedule{GZip: true},
|
2018-04-10 12:21:20 +00:00
|
|
|
),
|
|
|
|
})
|
2017-04-20 12:40:56 +00:00
|
|
|
}
|
2020-03-18 10:17:18 +00:00
|
|
|
|
2022-08-05 09:12:41 +00:00
|
|
|
// CloseAndLogIfError Closes io.Closer and logs the error if any
|
2020-03-18 10:17:18 +00:00
|
|
|
func CloseAndLogIfError(ctx context.Context, closer io.Closer, message string) {
|
|
|
|
if closer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := closer.Close()
|
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.TODO()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error(message)
|
|
|
|
}
|
|
|
|
}
|