File: logging.go

package info (click to toggle)
golang-github-crc-org-crc 2.34.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,548 kB
  • sloc: sh: 398; makefile: 326; javascript: 40
file content (126 lines) | stat: -rw-r--r-- 2,390 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package logging

import (
	"os"

	"github.com/sirupsen/logrus"
	"github.com/spf13/pflag"
	terminal "golang.org/x/term"
	"gopkg.in/natefinch/lumberjack.v2"
)

var (
	lumberjackLogger *lumberjack.Logger
	logLevel         = defaultLogLevel()
	Memory           = newInMemoryHook(100)
)

func CloseLogging() {
	if lumberjackLogger != nil {
		_ = lumberjackLogger.Close()
	}
	logrus.StandardLogger().ReplaceHooks(make(logrus.LevelHooks))
}

func BackupLogFile() {
	if lumberjackLogger == nil {
		return
	}
	_ = lumberjackLogger.Rotate()
}

func InitLogrus(logFilePath string) {
	if lumberjackLogger != nil {
		return
	}

	lumberjackLogger = &lumberjack.Logger{
		Filename:   logFilePath,
		MaxSize:    5, // 5MB
		MaxBackups: 2,
	}
	// send logs to file
	logrus.SetOutput(lumberjackLogger)

	logrus.SetLevel(logrus.TraceLevel)

	level, err := logrus.ParseLevel(logLevel)
	if err != nil {
		level = logrus.InfoLevel
	}

	logrus.AddHook(Memory)

	// Add hook to send error/fatal to stderr
	logrus.AddHook(newstdErrHook(level, &logrus.TextFormatter{
		ForceColors:            terminal.IsTerminal(int(os.Stderr.Fd())),
		DisableTimestamp:       true,
		DisableLevelTruncation: false,
	}))
}

func DefaultLogLevel() logrus.Level {
	level, err := logrus.ParseLevel(logLevel)
	if err != nil {
		level = logrus.InfoLevel
	}
	return level
}

func defaultLogLevel() string {
	defaultLevel := "info"
	envLogLevel := os.Getenv("CRC_LOG_LEVEL")
	if envLogLevel != "" {
		defaultLevel = envLogLevel
	}

	return defaultLevel
}

func AddLogLevelFlag(flagset *pflag.FlagSet) {
	flagset.StringVar(&logLevel, "log-level", defaultLogLevel(), "log level (e.g. \"debug | info | warn | error\")")
}

func IsDebug() bool {
	return logLevel == "debug"
}

func Info(args ...interface{}) {
	logrus.Info(args...)
}

func Infof(s string, args ...interface{}) {
	logrus.Infof(s, args...)
}

func Warn(args ...interface{}) {
	logrus.Warn(args...)
}

func Warnf(s string, args ...interface{}) {
	logrus.Warnf(s, args...)
}

func Fatal(args ...interface{}) {
	logrus.Fatal(args...)
}

func Fatalf(s string, args ...interface{}) {
	logrus.Fatalf(s, args...)
}

func Error(args ...interface{}) {
	logrus.Error(args...)
}

func Errorf(s string, args ...interface{}) {
	logrus.Errorf(s, args...)
}

func Debug(args ...interface{}) {
	logrus.Debug(args...)
}

func Debugf(s string, args ...interface{}) {
	logrus.Debugf(s, args...)
}