-
Notifications
You must be signed in to change notification settings - Fork 311
/
log.go
192 lines (159 loc) · 4.91 KB
/
log.go
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2020 The Okteto Authors
// 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.
package log
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"github.com/fatih/color"
"github.com/okteto/okteto/pkg/config"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)
var (
redString = color.New(color.FgHiRed).SprintfFunc()
greenString = color.New(color.FgGreen).SprintfFunc()
yellowString = color.New(color.FgHiYellow).SprintfFunc()
blueString = color.New(color.FgHiBlue).SprintfFunc()
errorSymbol = color.New(color.BgHiRed, color.FgBlack).Sprint(" x ")
successSymbol = color.New(color.BgGreen, color.FgBlack).Sprint(" ✓ ")
informationSymbol = color.New(color.BgHiBlue, color.FgBlack).Sprint(" i ")
)
type logger struct {
out *logrus.Logger
file *logrus.Entry
}
var log = &logger{
out: logrus.New(),
}
func init() {
if runtime.GOOS == "windows" {
successSymbol = color.New(color.BgGreen, color.FgBlack).Sprint(" + ")
}
}
// Init configures the logger for the package to use.
func Init(level logrus.Level) {
log.out.SetOutput(os.Stdout)
log.out.SetLevel(level)
fileLogger := logrus.New()
fileLogger.SetFormatter(&logrus.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
logPath := filepath.Join(config.GetOktetoHome(), "okteto.log")
rolling := getRollingLog(logPath)
fileLogger.SetOutput(rolling)
fileLogger.SetLevel(logrus.DebugLevel)
actionID := uuid.NewV4().String()
log.file = fileLogger.WithFields(logrus.Fields{"action": actionID, "version": config.VersionString})
}
func getRollingLog(path string) io.Writer {
return &lumberjack.Logger{
Filename: path,
MaxSize: 1, // megabytes
MaxBackups: 10,
MaxAge: 28, //days
Compress: true,
}
}
// SetLevel sets the level of the main logger
func SetLevel(level string) {
l, err := logrus.ParseLevel(level)
if err == nil {
log.out.SetLevel(l)
}
}
// Debug writes a debug-level log
func Debug(args ...interface{}) {
log.out.Debug(args...)
if log.file != nil {
log.file.Debug(args...)
}
}
// Debugf writes a debug-level log with a format
func Debugf(format string, args ...interface{}) {
log.out.Debugf(format, args...)
if log.file != nil {
log.file.Debugf(format, args...)
}
}
// Info writes a info-level log
func Info(args ...interface{}) {
log.out.Info(args...)
if log.file != nil {
log.file.Info(args...)
}
}
// Infof writes a info-level log with a format
func Infof(format string, args ...interface{}) {
log.out.Infof(format, args...)
if log.file != nil {
log.file.Infof(format, args...)
}
}
// Error writes a error-level log
func Error(args ...interface{}) {
log.out.Error(args...)
if log.file != nil {
log.file.Error(args...)
}
}
// Errorf writes a error-level log with a format
func Errorf(format string, args ...interface{}) {
log.out.Errorf(format, args...)
if log.file != nil {
log.file.Errorf(format, args...)
}
}
// Yellow writes a line in yellow
func Yellow(format string, args ...interface{}) {
log.out.Infof(format, args...)
fmt.Fprintln(color.Output, yellowString(format, args...))
}
// Green writes a line in green
func Green(format string, args ...interface{}) {
log.out.Infof(format, args...)
fmt.Fprintln(color.Output, greenString(format, args...))
}
// BlueString returns a string in blue
func BlueString(format string, args ...interface{}) string {
return blueString(format, args...)
}
// Success prints a message with the success symbol first, and the text in green
func Success(format string, args ...interface{}) {
log.out.Infof(format, args...)
fmt.Fprintf(color.Output, "%s %s\n", successSymbol, greenString(format, args...))
}
// Information prints a message with the information symbol first, and the text in blue
func Information(format string, args ...interface{}) {
log.out.Infof(format, args...)
fmt.Fprintf(color.Output, "%s %s\n", informationSymbol, blueString(format, args...))
}
// Hint prints a message with the text in blue
func Hint(format string, args ...interface{}) {
log.out.Infof(format, args...)
fmt.Fprintf(color.Output, "%s\n", blueString(format, args...))
}
// Fail prints a message with the error symbol first, and the text in red
func Fail(format string, args ...interface{}) {
log.out.Infof(format, args...)
fmt.Fprintf(color.Output, "%s %s\n", errorSymbol, redString(format, args...))
}
// Println writes a line with colors
func Println(args ...interface{}) {
log.out.Info(args...)
fmt.Fprintln(color.Output, args...)
}