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: 54789ab3d4a3c22d019d625171645d62dea02a41 (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
package log

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

	"github.com/sirupsen/logrus"
)

// 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()

	filepath := filepath.Join(os.Getenv("GITALY_LOG_DIR"), "gitaly_hooks.log")

	logFile, err := os.OpenFile(filepath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
	if err != nil {
		logger.SetOutput(ioutil.Discard)
	} else {
		logger.SetOutput(logFile)
	}

	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.Fprintf(os.Stderr, "error executing git hook")
	h.logger.Fatalf(format, a...)
}