Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
psyb0t committed Nov 6, 2023
0 parents commit b9da162
Show file tree
Hide file tree
Showing 19 changed files with 773 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
_ "github.com/psyb0t/logrus-configurator"
"github.com/sirupsen/logrus"
)

func main() {
logrus.Trace("this shit's a trace")
logrus.Debug("this shit's a debug")
logrus.Info("this shit's an info")
logrus.Warn("this shit's a warn")
logrus.Error("this shit's an error")
logrus.Fatal("this shit's a fatal")
}
12 changes: 12 additions & 0 deletions .example/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
export LOG_LEVEL="trace"
export LOG_FORMAT="text"
export LOG_CALLER="true"

go run main.go

export LOG_LEVEL="warn"
export LOG_FORMAT="json"
export LOG_CALLER="false"

go run main.go
59 changes: 59 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: pipeline
on: [push]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
lint:
runs-on: ubuntu-latest
permissions:
actions: none
contents: read
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21.3

- name: Install golangci-lint
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

- name: Run Linting
run: make lint

test:
needs: lint
runs-on: ubuntu-latest
permissions:
actions: none
contents: read
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21.3

- name: Run Tests
run: make test-coverage

release:
if: contains(github.ref, 'refs/tags/')
needs: test
runs-on: ubuntu-latest
permissions:
actions: none
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Release
uses: softprops/action-gh-release@v1
with:
draft: false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
116 changes: 116 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
linters:
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- cyclop
- decorder
- depguard
- dogsled
- dupl
- dupword
- durationcheck
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
# - exhaustruct
- exportloopref
- forbidigo
- forcetypeassert
- funlen
- gci
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- goerr113
- gofmt
- gofumpt
- goheader
- goimports
- gomnd
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosmopolitan
- grouper
- importas
- interfacebloat
- ireturn
- lll
- loggercheck
- maintidx
- makezero
- mirror
- misspell
- musttag
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- nonamedreturns
- nosprintfhostport
#- paralleltest
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- sqlclosecheck
- stylecheck
- tagalign
- tagliatelle
- tenv
- testableexamples
- testpackage
- thelper
- tparallel
- unconvert
- unparam
- usestdlibvars
#- varnamelen
- wastedassign
- whitespace
- wrapcheck
- wsl
- zerologlint
- unused

linters-settings:
depguard:
rules:
main:
allow:
- $gostd
- github.com/psyb0t/gonfiguration
- github.com/sirupsen/logrus
- github.com/pkg/errors
- github.com/stretchr/testify

issues:
include:
- EXC0001
- EXC0005
- EXC0009
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 Ciprian Mandache (ciprian.51k.eu)

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
MODULE_NAME := "github.com/psyb0t/logrus-configurator"
PKG_LIST := $(shell go list ${MODULE_NAME}/...)
MIN_TEST_COVERAGE := 80

all: dep lint test ## Run dep, lint and test

dep: ## Get project dependencies
@echo "Getting project dependencies..."
@go mod tidy

lint: ## Lint all Golang files
@echo "Linting all Golang files..."
@golangci-lint run --timeout=30m0s

test: ## Run all tests
@echo "Running all tests..."
@go test -race $(PKG_LIST)

test-coverage: ## Run tests with coverage check. Fails if coverage is below the threshold.
@echo "Running tests with coverage check..."
@trap 'rm -f coverage.txt' EXIT; \
go test -race -coverprofile=coverage.txt $(PKG_LIST); \
if [ $$? -ne 0 ]; then \
echo "Test failed. Exiting."; \
exit 1; \
fi; \
result=$$(go tool cover -func=coverage.txt | grep -oP 'total:\s+\(statements\)\s+\K\d+' || echo "0"); \
if [ $$result -eq 0 ]; then \
echo "No test coverage information available."; \
exit 0; \
elif [ $$result -lt $(MIN_TEST_COVERAGE) ]; then \
echo "FAIL: Coverage $$result% is less than the minimum $(MIN_TEST_COVERAGE)%"; \
exit 1; \
fi

help: ## Display this help message
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
103 changes: 103 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# logrus-configurator 🤖

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. 😎

## What's This Shit About? 💩

`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.

## Features

- No-nonsense log level setting (trace your bugs or go full-on panic mode, we don't judge).
- Formatting logs like a boss with JSON or text formats – keep it structured or keep it simple.
- Caller reporting for when you need to backtrack who messed up. It's like `CSI` for your code.
- Automated configuration using environment variables, because who has time for manual setup?

## Usage Example

Ready to rock with `logrus-configurator`? Check this out.

### main.go

```go
package main

import (
_ "github.com/psyb0t/logrus-configurator"
"github.com/sirupsen/logrus"
)

func main() {
// Here's where the magic happens - just logging some stuff.
logrus.Trace("this shit's a trace") // Ninja mode, won't show unless you want it to.
logrus.Debug("this shit's a debug") // Debugging like a boss.
logrus.Info("this shit's an info") // Cool, calm, and collected info.
logrus.Warn("this shit's a warn") // Warning: badass logger at work.
logrus.Error("this shit's an error") // Oh crap, something went sideways.
logrus.Fatal("this shit's a fatal") // Critical hit! It's super effective!
}
```

### Crank It Up

Get your environment dialed in like the soundboard at a goth concert:

```bash
export LOG_LEVEL="trace" # Choose the verbosity level.
export LOG_FORMAT="text" # Pick your poison: json or text.
export LOG_CALLER="true" # Decide if you want to see who's calling the logs.
```

Unleash the beast with:

```bash
go run main.go
```

And let the good times roll with the output:

```plaintext
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
TRAC[0000]/github.com/psyb0t/logrus-configurator/.example/main.go:9 main.main() this shit's a trace
DEBU[0000]/github.com/psyb0t/logrus-configurator/.example/main.go:10 main.main() this shit's a debug
INFO[0000]/github.com/psyb0t/logrus-configurator/.example/main.go:11 main.main() this shit's an info
WARN[0000]/github.com/psyb0t/logrus-configurator/.example/main.go:12 main.main() this shit's a warn
ERRO[0000]/github.com/psyb0t/logrus-configurator/.example/main.go:13 main.main() this shit's an error
FATA[0000]/github.com/psyb0t/logrus-configurator/.example/main.go:14 main.main() this shit's a fatal
exit status 1
```

Wanna switch it up? Change the environment variables to mix the brew.

```bash
export LOG_LEVEL="warn"
export LOG_FORMAT="json"
export LOG_CALLER="false"
```

Then let it simmer with:

```bash
go run main.go
```

And enjoy the sweet sound of (almost) silence:

```plaintext
{"level":"warning","msg":"this shit's a warn","time":"2023-11-06T21:15:49+02:00"}
{"level":"error","msg":"this shit's an error","time":"2023-11-06T21:15:49+02:00"}
{"level":"fatal","msg":"this shit's a fatal","time":"2023-11-06T21:15:49+02:00"}
exit status 1
```

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))

And that's damn it. You've just pimped your logger!

## Contribute

Got an idea? Throw in a PR! Found a bug? Raise an issue! Let's make `logrus-configurator` as tight as your favorite jeans.

## License

It's MIT. Free as in 'do whatever the hell you want with it', just don't blame me if shit hits the fan.
15 changes: 15 additions & 0 deletions common_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package logrusconfigurator

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

func unsetEnvs(t *testing.T) {
t.Helper()

require.Nil(t, os.Unsetenv(configKeyLogLevel), "Unexpected error")
require.Nil(t, os.Unsetenv(configKeyLogFormat), "Unexpected error")
}
8 changes: 8 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package logrusconfigurator

import "errors"

var (
errInvalidLogLevel = errors.New("invalid log level")
errInvalidLogFormat = errors.New("invalid log format")
)
Loading

0 comments on commit b9da162

Please sign in to comment.