|
| 1 | +package gologger |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/logrusorgru/aurora" |
| 10 | +) |
| 11 | + |
| 12 | +// Level defines all the available levels we can log at |
| 13 | +type Level int |
| 14 | + |
| 15 | +// Available logging levels |
| 16 | +const ( |
| 17 | + Null Level = iota |
| 18 | + Fatal |
| 19 | + Silent |
| 20 | + Label |
| 21 | + Misc |
| 22 | + Error |
| 23 | + Info |
| 24 | + Warning |
| 25 | + Debug |
| 26 | + Verbose |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + // UseColors can be used to control coloring of the output |
| 31 | + UseColors = true |
| 32 | + // MaxLevel is the maximum level to log at. By default, logging |
| 33 | + // is done at Info level. Using verbose will display all the errors too, |
| 34 | + // Using silent will display only the most relevant information. |
| 35 | + MaxLevel = Info |
| 36 | + |
| 37 | + labels = map[Level]string{ |
| 38 | + Warning: "WRN", |
| 39 | + Error: "ERR", |
| 40 | + Label: "WRN", |
| 41 | + Fatal: "FTL", |
| 42 | + Debug: "DBG", |
| 43 | + Info: "INF", |
| 44 | + } |
| 45 | + |
| 46 | + mutex = &sync.Mutex{} |
| 47 | +) |
| 48 | + |
| 49 | +var stringBuilderPool = &sync.Pool{New: func() interface{} { |
| 50 | + return new(strings.Builder) |
| 51 | +}} |
| 52 | + |
| 53 | +// wrap wraps a given label for a message to a logg-able representation. |
| 54 | +// It checks if colors are specified and what level we are logging at. |
| 55 | +func wrap(label string, level Level) string { |
| 56 | + // Check if we are not using colors, if not, return |
| 57 | + if !UseColors { |
| 58 | + return label |
| 59 | + } |
| 60 | + |
| 61 | + switch level { |
| 62 | + case Silent: |
| 63 | + return label |
| 64 | + case Info, Verbose: |
| 65 | + return aurora.Blue(label).String() |
| 66 | + case Fatal: |
| 67 | + return aurora.Bold(aurora.Red(label)).String() |
| 68 | + case Error: |
| 69 | + return aurora.Red(label).String() |
| 70 | + case Debug: |
| 71 | + return aurora.Magenta(label).String() |
| 72 | + case Warning, Label: |
| 73 | + return aurora.Yellow(label).String() |
| 74 | + default: |
| 75 | + return label |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// getLabel generates a label for a given message, depending on the level |
| 80 | +// and the label passed. |
| 81 | +func getLabel(level Level, label string, sb *strings.Builder) { |
| 82 | + switch level { |
| 83 | + case Silent, Misc: |
| 84 | + return |
| 85 | + case Error, Fatal, Info, Warning, Debug, Label: |
| 86 | + sb.WriteString("[") |
| 87 | + sb.WriteString(wrap(labels[level], level)) |
| 88 | + sb.WriteString("]") |
| 89 | + sb.WriteString(" ") |
| 90 | + return |
| 91 | + case Verbose: |
| 92 | + sb.WriteString("[") |
| 93 | + sb.WriteString(wrap(label, level)) |
| 94 | + sb.WriteString("]") |
| 95 | + sb.WriteString(" ") |
| 96 | + return |
| 97 | + default: |
| 98 | + return |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// log logs the actual message to the screen |
| 103 | +func log(level Level, label string, format string, args ...interface{}) { |
| 104 | + // Don't log if the level is null |
| 105 | + if level == Null { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + if level <= MaxLevel { |
| 110 | + // Build the log message using the string builder pool |
| 111 | + sb := stringBuilderPool.Get().(*strings.Builder) |
| 112 | + |
| 113 | + // Get the label and append it to string builder |
| 114 | + getLabel(level, label, sb) |
| 115 | + |
| 116 | + message := fmt.Sprintf(format, args...) |
| 117 | + sb.WriteString(message) |
| 118 | + |
| 119 | + if strings.HasSuffix(message, "\n") == false { |
| 120 | + sb.WriteString("\n") |
| 121 | + } |
| 122 | + |
| 123 | + mutex.Lock() |
| 124 | + switch level { |
| 125 | + case Silent: |
| 126 | + fmt.Fprintf(os.Stdout, sb.String()) |
| 127 | + default: |
| 128 | + fmt.Fprintf(os.Stderr, sb.String()) |
| 129 | + } |
| 130 | + mutex.Unlock() |
| 131 | + |
| 132 | + sb.Reset() |
| 133 | + stringBuilderPool.Put(sb) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// Infof writes a info message on the screen with the default label |
| 138 | +func Infof(format string, args ...interface{}) { |
| 139 | + log(Info, "", format, args...) |
| 140 | +} |
| 141 | + |
| 142 | +// Warningf writes a warning message on the screen with the default label |
| 143 | +func Warningf(format string, args ...interface{}) { |
| 144 | + log(Warning, "", format, args...) |
| 145 | +} |
| 146 | + |
| 147 | +// Errorf writes an error message on the screen with the default label |
| 148 | +func Errorf(format string, args ...interface{}) { |
| 149 | + log(Error, "", format, args...) |
| 150 | +} |
| 151 | + |
| 152 | +// Debugf writes an error message on the screen with the default label |
| 153 | +func Debugf(format string, args ...interface{}) { |
| 154 | + log(Debug, "", format, args...) |
| 155 | +} |
| 156 | + |
| 157 | +// Verbosef writes a verbose message on the screen with a tabel |
| 158 | +func Verbosef(format string, label string, args ...interface{}) { |
| 159 | + log(Verbose, label, format, args...) |
| 160 | +} |
| 161 | + |
| 162 | +// Silentf writes a message on the stdout with no label |
| 163 | +func Silentf(format string, args ...interface{}) { |
| 164 | + log(Silent, "", format, args...) |
| 165 | +} |
| 166 | + |
| 167 | +// Fatalf exits the program if we encounter a fatal error |
| 168 | +func Fatalf(format string, args ...interface{}) { |
| 169 | + log(Fatal, "", format, args...) |
| 170 | + os.Exit(1) |
| 171 | +} |
| 172 | + |
| 173 | +// Printf prints a string on screen without any extra stuff |
| 174 | +func Printf(format string, args ...interface{}) { |
| 175 | + log(Misc, "", format, args...) |
| 176 | +} |
| 177 | + |
| 178 | +// Labelf prints a string on screen with a label interface |
| 179 | +func Labelf(format string, args ...interface{}) { |
| 180 | + log(Label, "", format, args...) |
| 181 | +} |
0 commit comments