Welcome to mirror list, hosted at ThFree Co, Russian Federation.

hook.go « log « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 96e348b42cb7ffee1b5bbdbba9a67e2ca2ecdce7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package log

import (
	"fmt"
	"io"
	"os"
	"path/filepath"

	"github.com/sirupsen/logrus"
	"gitlab.com/gitlab-org/gitaly/v15/internal/helper/perm"
)

// HookLogger is a wrapper around *logrus.Logger
type HookLogger struct {
	logger *logrus.Logger
}

// NewHookLogger creates a file logger, since both stderr and stdout will be displayed in git output
func NewHookLogger() *HookLogger {
	logger := logrus.New()

	logDir := os.Getenv(GitalyLogDirEnvKey)
	if logDir == "" {
		logger.SetOutput(io.Discard)
		return &HookLogger{logger: logger}
	}

	logFile, err := os.OpenFile(filepath.Join(logDir, "gitaly_hooks.log"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, perm.SharedFile)
	if err != nil {
		logger.SetOutput(io.Discard)
	} else {
		logger.SetOutput(logFile)
	}

	logger.SetFormatter(UTCTextFormatter())

	return &HookLogger{logger: logger}
}

// Fatal logs an error at the Fatal level and writes a generic message to stderr
func (h *HookLogger) Fatal(err error) {
	h.Fatalf("%v", err)
}

// Fatalf logs a formatted error at the Fatal level
func (h *HookLogger) Fatalf(format string, a ...interface{}) {
	fmt.Fprintln(os.Stderr, "error executing git hook")
	h.logger.Fatalf(format, a...)
}

// Errorf logs a formatted error at the Fatal level
func (h *HookLogger) Errorf(format string, a ...interface{}) {
	h.logger.Errorf(format, a...)
}

// Logger returns the underlying logrus logger
func (h *HookLogger) Logger() *logrus.Logger { return h.logger }