-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_logging.go
166 lines (151 loc) · 4.21 KB
/
console_logging.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
// Copyright 2023 Fortio 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"
"os"
"time"
"fortio.org/log/goroutine"
)
// to avoid making a new package/namespace for colors, we use a struct.
type color struct {
Reset string
Red string
Green string
Yellow string
Blue string
Purple string
Cyan string
Gray string
White string
BrightRed string
DarkGray string
}
var (
// these should really be constants but go doesn't have constant structs, arrays etc...
// ANSI color codes.
// This isn't meant to be used directly and is here only to document the names of the struct.
// Use the Colors variable instead.
ANSIColors = color{
Reset: "\033[0m",
Red: "\033[31m",
Green: "\033[32m",
Yellow: "\033[33m",
Blue: "\033[34m",
Purple: "\033[35m",
Cyan: "\033[36m",
Gray: "\033[37m",
White: "\033[97m",
BrightRed: "\033[91m",
DarkGray: "\033[90m",
}
// ANSI color codes or empty depending on ColorMode.
// These will be reset to empty string if color is disabled (see ColorMode() and SetColorMode()).
// Start with actual colors, will be reset to empty if color is disabled.
Colors = ANSIColors
// Mapping of log levels to color.
LevelToColor = []string{
Colors.Gray,
Colors.Cyan,
Colors.Green,
Colors.Yellow,
Colors.Red,
Colors.Purple,
Colors.BrightRed,
Colors.Green, // NoLevel log.Printf
}
// Used for color version of console logging.
LevelToText = []string{
"DBG",
"VRB",
"INF",
"WRN",
"ERR",
"CRI",
"FTL",
"",
}
// Cached flag for whether to use color output or not.
Color = false
)
// Is the file (e.g os.StdErr) Stat()able so we can detect if it's a tty or not.
// If not we switch in init() to Stdout.
func isValid(file *os.File) bool {
if file == nil {
return false
}
_, err := file.Stat()
return err == nil
}
// ConsoleLogging is a utility to check if the current logger output is a console (terminal).
func ConsoleLogging() bool {
f, ok := jWriter.w.(*os.File)
if !ok {
return false
}
s, err := f.Stat()
if err != nil {
return false
}
return (s.Mode() & os.ModeCharDevice) == os.ModeCharDevice
}
// SetColorMode computes whether we currently should be using color text mode or not.
// Need to be reset if config changes (but is already automatically re evaluated when calling SetOutput()).
// It will reset the Colors variable to either be the actual escape sequences or empty strings (when
// color is disabled).
func SetColorMode() {
Color = ColorMode()
if Color {
Colors = ANSIColors
} else {
Colors = color{}
}
// Also reset the level to color mapping to empty or customized colors, as needed.
LevelToColor = []string{
Colors.Gray,
Colors.Cyan,
Colors.Green,
Colors.Yellow,
Colors.Red,
Colors.Purple,
Colors.BrightRed,
Colors.Green, // NoLevel log.Printf
}
}
// ColorMode returns true if we should be using color text mode, which is either because it's
// forced or because we are in a console and the config allows it.
// Should not be called often, instead read/update the Color variable when needed.
func ColorMode() bool {
return Config.ForceColor || (Config.ConsoleColor && ConsoleLogging())
}
func colorTimestamp() string {
if Config.NoTimestamp {
return ""
}
return time.Now().Format(Colors.DarkGray + "15:04:05.000 ")
}
// Color version of jsonGID().
func colorGID() string {
if !Config.GoroutineID {
return ""
}
return Colors.Gray + fmt.Sprintf("r%d ", goroutine.ID())
}
// Longer version when colorizing on console of the level text.
func ColorLevelToStr(lvl Level) string {
if lvl == NoLevel {
return Colors.DarkGray
}
return Colors.DarkGray + "[" + LevelToColor[lvl] + LevelToText[lvl] + Colors.DarkGray + "]"
}