Skip to content

Commit b9da162

Browse files
committed
initial commit
0 parents  commit b9da162

19 files changed

+773
-0
lines changed

.example/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
_ "github.com/psyb0t/logrus-configurator"
5+
"github.com/sirupsen/logrus"
6+
)
7+
8+
func main() {
9+
logrus.Trace("this shit's a trace")
10+
logrus.Debug("this shit's a debug")
11+
logrus.Info("this shit's an info")
12+
logrus.Warn("this shit's a warn")
13+
logrus.Error("this shit's an error")
14+
logrus.Fatal("this shit's a fatal")
15+
}

.example/run.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
export LOG_LEVEL="trace"
3+
export LOG_FORMAT="text"
4+
export LOG_CALLER="true"
5+
6+
go run main.go
7+
8+
export LOG_LEVEL="warn"
9+
export LOG_FORMAT="json"
10+
export LOG_CALLER="false"
11+
12+
go run main.go

.github/workflows/pipeline.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: pipeline
2+
on: [push]
3+
env:
4+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5+
6+
jobs:
7+
lint:
8+
runs-on: ubuntu-latest
9+
permissions:
10+
actions: none
11+
contents: read
12+
steps:
13+
- name: Check out code
14+
uses: actions/checkout@v3
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v3
18+
with:
19+
go-version: 1.21.3
20+
21+
- name: Install golangci-lint
22+
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
23+
24+
- name: Run Linting
25+
run: make lint
26+
27+
test:
28+
needs: lint
29+
runs-on: ubuntu-latest
30+
permissions:
31+
actions: none
32+
contents: read
33+
steps:
34+
- name: Check out code
35+
uses: actions/checkout@v3
36+
37+
- name: Set up Go
38+
uses: actions/setup-go@v3
39+
with:
40+
go-version: 1.21.3
41+
42+
- name: Run Tests
43+
run: make test-coverage
44+
45+
release:
46+
if: contains(github.ref, 'refs/tags/')
47+
needs: test
48+
runs-on: ubuntu-latest
49+
permissions:
50+
actions: none
51+
contents: write
52+
steps:
53+
- name: Checkout code
54+
uses: actions/checkout@v3
55+
56+
- name: Release
57+
uses: softprops/action-gh-release@v1
58+
with:
59+
draft: false

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

.golangci.yml

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
linters:
2+
enable:
3+
- errcheck
4+
- gosimple
5+
- govet
6+
- ineffassign
7+
- staticcheck
8+
- unused
9+
- asasalint
10+
- asciicheck
11+
- bidichk
12+
- bodyclose
13+
- containedctx
14+
- contextcheck
15+
- cyclop
16+
- decorder
17+
- depguard
18+
- dogsled
19+
- dupl
20+
- dupword
21+
- durationcheck
22+
- errchkjson
23+
- errname
24+
- errorlint
25+
- execinquery
26+
- exhaustive
27+
# - exhaustruct
28+
- exportloopref
29+
- forbidigo
30+
- forcetypeassert
31+
- funlen
32+
- gci
33+
- ginkgolinter
34+
- gocheckcompilerdirectives
35+
- gochecknoglobals
36+
- gochecknoinits
37+
- gocognit
38+
- goconst
39+
- gocritic
40+
- gocyclo
41+
- godot
42+
- godox
43+
- goerr113
44+
- gofmt
45+
- gofumpt
46+
- goheader
47+
- goimports
48+
- gomnd
49+
- gomoddirectives
50+
- gomodguard
51+
- goprintffuncname
52+
- gosec
53+
- gosmopolitan
54+
- grouper
55+
- importas
56+
- interfacebloat
57+
- ireturn
58+
- lll
59+
- loggercheck
60+
- maintidx
61+
- makezero
62+
- mirror
63+
- misspell
64+
- musttag
65+
- nakedret
66+
- nestif
67+
- nilerr
68+
- nilnil
69+
- nlreturn
70+
- noctx
71+
- nolintlint
72+
- nonamedreturns
73+
- nosprintfhostport
74+
#- paralleltest
75+
- prealloc
76+
- predeclared
77+
- promlinter
78+
- reassign
79+
- revive
80+
- rowserrcheck
81+
- sqlclosecheck
82+
- stylecheck
83+
- tagalign
84+
- tagliatelle
85+
- tenv
86+
- testableexamples
87+
- testpackage
88+
- thelper
89+
- tparallel
90+
- unconvert
91+
- unparam
92+
- usestdlibvars
93+
#- varnamelen
94+
- wastedassign
95+
- whitespace
96+
- wrapcheck
97+
- wsl
98+
- zerologlint
99+
- unused
100+
101+
linters-settings:
102+
depguard:
103+
rules:
104+
main:
105+
allow:
106+
- $gostd
107+
- github.com/psyb0t/gonfiguration
108+
- github.com/sirupsen/logrus
109+
- github.com/pkg/errors
110+
- github.com/stretchr/testify
111+
112+
issues:
113+
include:
114+
- EXC0001
115+
- EXC0005
116+
- EXC0009

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2023 Ciprian Mandache (ciprian.51k.eu)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
MODULE_NAME := "github.com/psyb0t/logrus-configurator"
2+
PKG_LIST := $(shell go list ${MODULE_NAME}/...)
3+
MIN_TEST_COVERAGE := 80
4+
5+
all: dep lint test ## Run dep, lint and test
6+
7+
dep: ## Get project dependencies
8+
@echo "Getting project dependencies..."
9+
@go mod tidy
10+
11+
lint: ## Lint all Golang files
12+
@echo "Linting all Golang files..."
13+
@golangci-lint run --timeout=30m0s
14+
15+
test: ## Run all tests
16+
@echo "Running all tests..."
17+
@go test -race $(PKG_LIST)
18+
19+
test-coverage: ## Run tests with coverage check. Fails if coverage is below the threshold.
20+
@echo "Running tests with coverage check..."
21+
@trap 'rm -f coverage.txt' EXIT; \
22+
go test -race -coverprofile=coverage.txt $(PKG_LIST); \
23+
if [ $$? -ne 0 ]; then \
24+
echo "Test failed. Exiting."; \
25+
exit 1; \
26+
fi; \
27+
result=$$(go tool cover -func=coverage.txt | grep -oP 'total:\s+\(statements\)\s+\K\d+' || echo "0"); \
28+
if [ $$result -eq 0 ]; then \
29+
echo "No test coverage information available."; \
30+
exit 0; \
31+
elif [ $$result -lt $(MIN_TEST_COVERAGE) ]; then \
32+
echo "FAIL: Coverage $$result% is less than the minimum $(MIN_TEST_COVERAGE)%"; \
33+
exit 1; \
34+
fi
35+
36+
help: ## Display this help message
37+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# logrus-configurator 🤖
2+
3+
Welcome to `logrus-configurator`, the badass sidekick for your logging adventures with Go! You've stumbled upon the dark alley of loggers where things get configured so slick that even your professors can't help but nod in approval. 😎
4+
5+
## What's This Shit About? 💩
6+
7+
`logrus-configurator` is a Go package that whips your `logrus` logger into shape without you breaking a sweat. Think of it as that one plugin at a rave that just knows how to tune things up. Want to set the log level? Bam! 🎚️ Prefer JSON over plain text? Wham! 📄 Want to know who called the logger? Boom! 🔍 It's got you covered.
8+
9+
## Features
10+
11+
- No-nonsense log level setting (trace your bugs or go full-on panic mode, we don't judge).
12+
- Formatting logs like a boss with JSON or text formats – keep it structured or keep it simple.
13+
- Caller reporting for when you need to backtrack who messed up. It's like `CSI` for your code.
14+
- Automated configuration using environment variables, because who has time for manual setup?
15+
16+
## Usage Example
17+
18+
Ready to rock with `logrus-configurator`? Check this out.
19+
20+
### main.go
21+
22+
```go
23+
package main
24+
25+
import (
26+
_ "github.com/psyb0t/logrus-configurator"
27+
"github.com/sirupsen/logrus"
28+
)
29+
30+
func main() {
31+
// Here's where the magic happens - just logging some stuff.
32+
logrus.Trace("this shit's a trace") // Ninja mode, won't show unless you want it to.
33+
logrus.Debug("this shit's a debug") // Debugging like a boss.
34+
logrus.Info("this shit's an info") // Cool, calm, and collected info.
35+
logrus.Warn("this shit's a warn") // Warning: badass logger at work.
36+
logrus.Error("this shit's an error") // Oh crap, something went sideways.
37+
logrus.Fatal("this shit's a fatal") // Critical hit! It's super effective!
38+
}
39+
```
40+
41+
### Crank It Up
42+
43+
Get your environment dialed in like the soundboard at a goth concert:
44+
45+
```bash
46+
export LOG_LEVEL="trace" # Choose the verbosity level.
47+
export LOG_FORMAT="text" # Pick your poison: json or text.
48+
export LOG_CALLER="true" # Decide if you want to see who's calling the logs.
49+
```
50+
51+
Unleash the beast with:
52+
53+
```bash
54+
go run main.go
55+
```
56+
57+
And let the good times roll with the output:
58+
59+
```plaintext
60+
DEBU[0000]/github.com/psyb0t/logrus-configurator/log.go:28 github.com/psyb0t/logrus-configurator.config.log() logrus-configurator: level: trace, format: text, reportCaller: true
61+
TRAC[0000]/github.com/psyb0t/logrus-configurator/.example/main.go:9 main.main() this shit's a trace
62+
DEBU[0000]/github.com/psyb0t/logrus-configurator/.example/main.go:10 main.main() this shit's a debug
63+
INFO[0000]/github.com/psyb0t/logrus-configurator/.example/main.go:11 main.main() this shit's an info
64+
WARN[0000]/github.com/psyb0t/logrus-configurator/.example/main.go:12 main.main() this shit's a warn
65+
ERRO[0000]/github.com/psyb0t/logrus-configurator/.example/main.go:13 main.main() this shit's an error
66+
FATA[0000]/github.com/psyb0t/logrus-configurator/.example/main.go:14 main.main() this shit's a fatal
67+
exit status 1
68+
```
69+
70+
Wanna switch it up? Change the environment variables to mix the brew.
71+
72+
```bash
73+
export LOG_LEVEL="warn"
74+
export LOG_FORMAT="json"
75+
export LOG_CALLER="false"
76+
```
77+
78+
Then let it simmer with:
79+
80+
```bash
81+
go run main.go
82+
```
83+
84+
And enjoy the sweet sound of (almost) silence:
85+
86+
```plaintext
87+
{"level":"warning","msg":"this shit's a warn","time":"2023-11-06T21:15:49+02:00"}
88+
{"level":"error","msg":"this shit's an error","time":"2023-11-06T21:15:49+02:00"}
89+
{"level":"fatal","msg":"this shit's a fatal","time":"2023-11-06T21:15:49+02:00"}
90+
exit status 1
91+
```
92+
93+
Whether you're in for a riot or a silent disco, `logrus-configurator` is your ticket. 🎟️ (check out all of the supported levels in [`level.go`](level.go))
94+
95+
And that's damn it. You've just pimped your logger!
96+
97+
## Contribute
98+
99+
Got an idea? Throw in a PR! Found a bug? Raise an issue! Let's make `logrus-configurator` as tight as your favorite jeans.
100+
101+
## License
102+
103+
It's MIT. Free as in 'do whatever the hell you want with it', just don't blame me if shit hits the fan.

common_internal_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package logrusconfigurator
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func unsetEnvs(t *testing.T) {
11+
t.Helper()
12+
13+
require.Nil(t, os.Unsetenv(configKeyLogLevel), "Unexpected error")
14+
require.Nil(t, os.Unsetenv(configKeyLogFormat), "Unexpected error")
15+
}

errors.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package logrusconfigurator
2+
3+
import "errors"
4+
5+
var (
6+
errInvalidLogLevel = errors.New("invalid log level")
7+
errInvalidLogFormat = errors.New("invalid log format")
8+
)

0 commit comments

Comments
 (0)