Skip to content

Commit b6f881e

Browse files
committed
Genesis
0 parents  commit b6f881e

File tree

6 files changed

+240
-0
lines changed

6 files changed

+240
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# gologger
2+
3+
gologger is a very simple logger for fast logging in simple command line tools.

doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package gologger provides a simple layer for leveled logging in go.
2+
package gologger

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/projectdiscovery/gologger
2+
3+
go 1.14
4+
5+
require (
6+
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381
7+
github.com/stretchr/testify v1.5.1
8+
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381 h1:bqDmpDG49ZRnB5PcgP0RXtQvnMSgIF14M7CBd2shtXs=
4+
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
9+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
12+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

gologger.go

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
}

gologger_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package gologger
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/logrusorgru/aurora"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestGetLabel(t *testing.T) {
13+
tests := []struct {
14+
level Level
15+
label string
16+
expected string
17+
}{
18+
{Fatal, "", fmt.Sprintf("[%s] ", aurora.Bold(aurora.Red(labels[Fatal])).String())},
19+
{Silent, "hello", ""},
20+
{Error, "error", fmt.Sprintf("[%s] ", aurora.Red(labels[Error]).String())},
21+
{Info, "", fmt.Sprintf("[%s] ", aurora.Blue(labels[Info]).String())},
22+
{Warning, "", fmt.Sprintf("[%s] ", aurora.Yellow(labels[Warning]).String())},
23+
{Verbose, "dns", fmt.Sprintf("[%s] ", aurora.Blue("dns").String())},
24+
}
25+
26+
sb := &strings.Builder{}
27+
for _, test := range tests {
28+
sb.Reset()
29+
getLabel(test.level, test.label, sb)
30+
data := sb.String()
31+
32+
assert.Equal(t, data, test.expected, "Expected message and generate message don't match")
33+
}
34+
}

0 commit comments

Comments
 (0)