git-chglog/logger.go
Derek Smith ae3382b7c8
chore(ci): add golangci-lint action and apply linting changes (#120)
BREAKING CHANGE: `JiraIssueId` has been renamed to `JiraIssueID`. This impacts the value for `pattern_maps` in `config.yml`.

* chore(ci): add golangci-lint action

* chore(lint): address errcheck lint failures

* chore(lint): address misspell lint failures

* chore(lint): address gocritic lint failures

* chore(lint): address golint lint failures

* chore(lint): address structcheck lint failures

* chore(lint): address gosimple lint failures

* chore(lint): address gofmt lint failures

* chore(ci): port to official golangci-lint github action

* Update golangci configuration for better coverage

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>

* fix: file is not goimports-ed

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>

* fix: golint and exported functions comments

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>

* chore(lint): address gosec G304 warning

* chore(lint): address uparam warnings

* chore(lint): address scopelint lint failures

* fix: cyclomatic complexity

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>

* chore(lint): address prealloc warning, noting that we are warning for now

* chore(lint): address govet and errorlint failures

* chore: clean up defer logic when checking errors

Co-authored-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-03-16 21:24:36 -05:00

59 lines
1.1 KiB
Go

package chglog
import (
"fmt"
"io"
"log"
"regexp"
"github.com/fatih/color"
"github.com/kyokomi/emoji/v2"
)
// Logger ...
type Logger struct {
stdout io.Writer
stderr io.Writer
silent bool
noEmoji bool
reEmoji *regexp.Regexp
}
// NewLogger ...
func NewLogger(stdout, stderr io.Writer, silent, noEmoji bool) *Logger {
return &Logger{
stdout: stdout,
stderr: stderr,
silent: silent,
noEmoji: noEmoji,
reEmoji: regexp.MustCompile(`:[\w\+_\-]+:\s?`),
}
}
// Log ...
func (l *Logger) Log(msg string) {
if !l.silent {
l.log(l.stdout, msg+"\n")
}
}
// Error ...
func (l *Logger) Error(msg string) {
prefix := color.New(color.FgWhite, color.BgRed, color.Bold).SprintFunc()
l.log(l.stderr, fmt.Sprintf("%s %s\n", prefix(" ERROR "), color.RedString(msg)))
}
func (l *Logger) log(w io.Writer, msg string) {
var printer func(io.Writer, ...interface{}) (int, error)
if l.noEmoji {
msg = l.reEmoji.ReplaceAllString(msg, "")
printer = fmt.Fprint
} else {
printer = emoji.Fprint
}
if _, err := printer(w, msg); err != nil {
log.Fatalln(err)
}
}