-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
167 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package logger | ||
|
||
import ( | ||
"github.com/fatih/color" | ||
"fmt" | ||
) | ||
|
||
// Level defines all available log levels for log messages. | ||
type Level int | ||
|
||
// Log levels. | ||
const ( | ||
Error Level = iota | ||
Warning | ||
Info | ||
Debug | ||
) | ||
|
||
var levelNames = []string{ | ||
"ERROR", | ||
"WARNING", | ||
"INFO", | ||
"DEBUG", | ||
} | ||
|
||
func ERROR(message string) { | ||
Log(Error, message) | ||
} | ||
|
||
func WARNING(message string) { | ||
Log(Warning, message) | ||
} | ||
|
||
func INFO(message string) { | ||
Log(Info, message) | ||
} | ||
|
||
func DEBUG(message string) { | ||
Log(Debug, message) | ||
} | ||
|
||
func Log(level Level, message string) { | ||
c := getLevelColor(level).SprintFunc() | ||
fmt.Println(c(fmt.Sprintf("%s: %s", levelNames[level], message))) | ||
} | ||
|
||
func Red(message string) string { | ||
red := color.New(color.FgRed).SprintFunc() | ||
return red(message) | ||
} | ||
|
||
func Yellow(message string) string { | ||
yellow := color.New(color.FgYellow).SprintFunc() | ||
return yellow(message) | ||
} | ||
|
||
func Green(message string) string { | ||
yellow := color.New(color.FgGreen).SprintFunc() | ||
return yellow(message) | ||
} | ||
|
||
func getLevelColor(level Level) *color.Color { | ||
c := color.New(color.FgBlack) | ||
|
||
switch level { | ||
case Error: | ||
c = color.New(color.FgRed) | ||
case Warning: | ||
c = color.New(color.FgYellow) | ||
case Info: | ||
c = color.New(color.FgBlue) | ||
case Debug: | ||
c = color.New(color.FgGreen) | ||
} | ||
|
||
return c | ||
} |