From 1a8d41d56503016b520603126f94dd5c3f04effe Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Wed, 4 Oct 2023 01:41:16 +0200 Subject: [PATCH] initial commit --- .github/FUNDING.yml | 1 + .github/workflows/lint.yml | 44 ++++++++++++++ .github/workflows/release.yml | 56 ++++++++++++++++++ .github/workflows/test.yml | 37 ++++++++++++ .gitignore | 38 ++++++++++++ LICENSE | 21 +++++++ Makefile | 43 ++++++++++++++ README.md | 107 ++++++++++++++++++++++++++++++++++ attributes.go | 89 ++++++++++++++++++++++++++++ finder.go | 27 +++++++++ go.mod | 10 ++++ go.sum | 14 +++++ groups.go | 59 +++++++++++++++++++ main_test.go | 11 ++++ 14 files changed, 557 insertions(+) create mode 100644 .github/FUNDING.yml create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/test.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 attributes.go create mode 100644 finder.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 groups.go create mode 100644 main_test.go diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..e4e0d3c --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: [samber] diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..746a876 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,44 @@ +name: Lint + +on: + push: + tags: + branches: + pull_request: + +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/setup-go@v2 + with: + go-version: 1.21 + stable: false + - uses: actions/checkout@v2 + - name: golangci-lint + uses: golangci/golangci-lint-action@v2 + with: + # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version + version: latest + + # Optional: working directory, useful for monorepos + working-directory: ./ + + # Optional: golangci-lint command line arguments. + args: --timeout 60s --max-same-issues 50 + + # Optional: show only new issues if it's a pull request. The default value is `false`. + # only-new-issues: true + + # Optional: if set to true then the action will use pre-installed Go. + # skip-go-installation: true + + # Optional: if set to true then the action don't cache or restore ~/go/pkg. + # skip-pkg-cache: true + + # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. + # skip-build-cache: true + + # optionally use a specific version of Go rather than the latest one + go_version: '1.21' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..f9fadc8 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,56 @@ +name: Release + +on: + workflow_dispatch: + inputs: + semver: + type: string + description: 'Semver (eg: v1.2.3)' + required: true + +jobs: + release: + if: github.triggering_actor == 'samber' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.21 + stable: false + + - name: Test + run: make test + + # remove tests in order to clean dependencies + - name: Remove xxx_test.go files + run: rm -rf *_test.go ./examples ./images + + # cleanup test dependencies + - name: Cleanup dependencies + run: go mod tidy + + - name: List files + run: tree -Cfi + - name: Write new go.mod into logs + run: cat go.mod + - name: Write new go.sum into logs + run: cat go.sum + + - name: Create tag + run: | + git config --global user.name '${{ github.triggering_actor }}' + git config --global user.email "${{ github.triggering_actor}}@users.noreply.github.com" + + git add . + git commit --allow-empty -m 'bump ${{ inputs.semver }}' + git tag ${{ inputs.semver }} + git push origin ${{ inputs.semver }} + + - name: Release + uses: softprops/action-gh-release@v1 + with: + name: ${{ inputs.semver }} + tag_name: ${{ inputs.semver }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..5000529 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,37 @@ +name: Tests + +on: + push: + tags: + branches: + pull_request: + +jobs: + + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.21 + stable: false + + - name: Build + run: make build + + - name: Test + run: make test + + - name: Test + run: make coverage + + - name: Codecov + uses: codecov/codecov-action@v2 + with: + token: ${{ secrets.CODECOV_TOKEN }} + file: ./cover.out + flags: unittests + verbose: true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e5ecc5c --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/go +# Edit at https://www.toptal.com/developers/gitignore?templates=go + +### Go ### +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +### Go Patch ### +/vendor/ +/Godeps/ + +# End of https://www.toptal.com/developers/gitignore/api/go + +cover.out +cover.html +.vscode + +.idea/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4845c99 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Samuel Berthe + +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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9249766 --- /dev/null +++ b/Makefile @@ -0,0 +1,43 @@ + +BIN=go + +build: + ${BIN} build -v ./... + +test: + go test -race -v ./... +watch-test: + reflex -t 50ms -s -- sh -c 'gotest -race -v ./...' + +bench: + go test -benchmem -count 3 -bench ./... +watch-bench: + reflex -t 50ms -s -- sh -c 'go test -benchmem -count 3 -bench ./...' + +coverage: + ${BIN} test -v -coverprofile=cover.out -covermode=atomic . + ${BIN} tool cover -html=cover.out -o cover.html + +tools: + ${BIN} install github.com/cespare/reflex@latest + ${BIN} install github.com/rakyll/gotest@latest + ${BIN} install github.com/psampaz/go-mod-outdated@latest + ${BIN} install github.com/jondot/goweight@latest + ${BIN} install github.com/golangci/golangci-lint/cmd/golangci-lint@latest + ${BIN} get -t -u golang.org/x/tools/cmd/cover + ${BIN} install github.com/sonatype-nexus-community/nancy@latest + go mod tidy + +lint: + golangci-lint run --timeout 60s --max-same-issues 50 ./... +lint-fix: + golangci-lint run --timeout 60s --max-same-issues 50 --fix ./... + +audit: + ${BIN} list -json -m all | nancy sleuth + +outdated: + ${BIN} list -u -m -json all | go-mod-outdated -update -direct + +weight: + goweight diff --git a/README.md b/README.md new file mode 100644 index 0000000..ffa6f3e --- /dev/null +++ b/README.md @@ -0,0 +1,107 @@ + +# slog toolchain + +[![tag](https://img.shields.io/github/tag/samber/slog-common.svg)](https://github.com/samber/slog-common/releases) +![Go Version](https://img.shields.io/badge/Go-%3E%3D%201.21-%23007d9c) +[![GoDoc](https://godoc.org/github.com/samber/slog-common?status.svg)](https://pkg.go.dev/github.com/samber/slog-common) +![Build Status](https://github.com/samber/slog-common/actions/workflows/test.yml/badge.svg) +[![Go report](https://goreportcard.com/badge/github.com/samber/slog-common)](https://goreportcard.com/report/github.com/samber/slog-common) +[![Coverage](https://img.shields.io/codecov/c/github/samber/slog-common)](https://codecov.io/gh/samber/slog-common) +[![Contributors](https://img.shields.io/github/contributors/samber/slog-common)](https://github.com/samber/slog-common/graphs/contributors) +[![License](https://img.shields.io/github/license/samber/slog-common)](./LICENSE) + +A toolchain for [slog](https://pkg.go.dev/log/slog) Go library. + +**See also:** + +- [slog-multi](https://github.com/samber/slog-multi): `slog.Handler` chaining, fanout, routing, failover, load balancing... +- [slog-formatter](https://github.com/samber/slog-formatter): `slog` attribute formatting +- [slog-sampling](https://github.com/samber/slog-sampling): `slog` sampling policy +- [slog-gin](https://github.com/samber/slog-gin): Gin middleware for `slog` logger +- [slog-echo](https://github.com/samber/slog-echo): Echo middleware for `slog` logger +- [slog-fiber](https://github.com/samber/slog-fiber): Fiber middleware for `slog` logger +- [slog-datadog](https://github.com/samber/slog-datadog): A `slog` handler for `Datadog` +- [slog-rollbar](https://github.com/samber/slog-rollbar): A `slog` handler for `Rollbar` +- [slog-sentry](https://github.com/samber/slog-sentry): A `slog` handler for `Sentry` +- [slog-syslog](https://github.com/samber/slog-syslog): A `slog` handler for `Syslog` +- [slog-logstash](https://github.com/samber/slog-logstash): A `slog` handler for `Logstash` +- [slog-fluentd](https://github.com/samber/slog-fluentd): A `slog` handler for `Fluentd` +- [slog-graylog](https://github.com/samber/slog-graylog): A `slog` handler for `Graylog` +- [slog-loki](https://github.com/samber/slog-loki): A `slog` handler for `Loki` +- [slog-slack](https://github.com/samber/slog-slack): A `slog` handler for `Slack` +- [slog-telegram](https://github.com/samber/slog-telegram): A `slog` handler for `Telegram` +- [slog-mattermost](https://github.com/samber/slog-mattermost): A `slog` handler for `Mattermost` +- [slog-microsoft-teams](https://github.com/samber/slog-microsoft-teams): A `slog` handler for `Microsoft Teams` +- [slog-webhook](https://github.com/samber/slog-webhook): A `slog` handler for `Webhook` +- [slog-kafka](https://github.com/samber/slog-kafka): A `slog` handler for `Kafka` +- [slog-parquet](https://github.com/samber/slog-parquet): A `slog` handler for `Parquet` + `Object Storage` +- [slog-zap](https://github.com/samber/slog-zap): A `slog` handler for `Zap` +- [slog-zerolog](https://github.com/samber/slog-zerolog): A `slog` handler for `Zerolog` +- [slog-logrus](https://github.com/samber/slog-logrus): A `slog` handler for `Logrus` + +## 🚀 Install + +```sh +go get github.com/samber/slog-common +``` + +**Compatibility**: go >= 1.21 + +No breaking changes will be made to exported APIs before v2.0.0. + +## 💡 Usage + +GoDoc: [https://pkg.go.dev/github.com/samber/slog-common](https://pkg.go.dev/github.com/samber/slog-common) + +```go +m := AttrsToValue(slog.Bool("foo", true), slog.String("bar", "baz")) +// {"foo": true, "bar": "baz"} + +k, v := AttrToValue(slog.Bool("foo", true)) +// "foo", true + +v := AnyValueToString(slog.IntValue(42)) +// "42" + +err := fmt.Errorf("an error") + +m := FormatErrorKey(map[string]any{"foo": "bar", "msg": err}, "msg") +// {"foo": "bar", "msg": {"kind": "*errorString", "error": "an error", "stack": nil}} + +m := FormatError(err) +// {"kind": "*errorString", "error": "an error", "stack": nil} +``` + +## 🤝 Contributing + +- Ping me on twitter [@samuelberthe](https://twitter.com/samuelberthe) (DMs, mentions, whatever :)) +- Fork the [project](https://github.com/samber/slog-common) +- Fix [open issues](https://github.com/samber/slog-common/issues) or request new features + +Don't hesitate ;) + +```bash +# Install some dev dependencies +make tools + +# Run tests +make test +# or +make watch-test +``` + +## 👤 Contributors + +![Contributors](https://contrib.rocks/image?repo=samber/slog-common) + +## 💫 Show your support + +Give a ⭐️ if this project helped you! + +[![GitHub Sponsors](https://img.shields.io/github/sponsors/samber?style=for-the-badge)](https://github.com/sponsors/samber) + +## 📝 License + +Copyright © 2023 [Samuel Berthe](https://github.com/samber). + +This project is [MIT](./LICENSE) licensed. diff --git a/attributes.go b/attributes.go new file mode 100644 index 0000000..6fcc70b --- /dev/null +++ b/attributes.go @@ -0,0 +1,89 @@ +package slogcommon + +import ( + "encoding" + "fmt" + "log/slog" + "reflect" +) + +func AttrsToValue(attrs ...slog.Attr) map[string]any { + log := map[string]any{} + + for i := range attrs { + k, v := AttrToValue(attrs[i]) + log[k] = v + } + + return log +} + +func AttrToValue(attr slog.Attr) (string, any) { + k := attr.Key + v := attr.Value + kind := v.Kind() + + switch kind { + case slog.KindAny: + return k, v.Any() + case slog.KindLogValuer: + return k, v.Any() + case slog.KindGroup: + return k, AttrsToValue(v.Group()...) + case slog.KindInt64: + return k, v.Int64() + case slog.KindUint64: + return k, v.Uint64() + case slog.KindFloat64: + return k, v.Float64() + case slog.KindString: + return k, v.String() + case slog.KindBool: + return k, v.Bool() + case slog.KindDuration: + return k, v.Duration() + case slog.KindTime: + return k, v.Time().UTC() + default: + return k, AnyValueToString(v) + } +} + +func AnyValueToString(v slog.Value) string { + if tm, ok := v.Any().(encoding.TextMarshaler); ok { + data, err := tm.MarshalText() + if err != nil { + return "" + } + + return string(data) + } + + return fmt.Sprintf("%+v", v.Any()) +} + +func FormatErrorKey(values map[string]any, errorKey string) map[string]any { + if err, ok := values["error"]; ok { + if e, ok := err.(error); ok { + values["error"] = FormatError(e) + } + } else if err, ok := values["err"]; ok { + if e, ok := err.(error); ok { + values["err"] = FormatError(e) + } + } else if err, ok := values[errorKey]; ok { + if e, ok := err.(error); ok { + values[errorKey] = FormatError(e) + } + } + + return values +} + +func FormatError(err error) map[string]any { + return map[string]any{ + "kind": reflect.TypeOf(err).String(), + "error": err.Error(), + "stack": nil, // @TODO + } +} diff --git a/finder.go b/finder.go new file mode 100644 index 0000000..1dce87e --- /dev/null +++ b/finder.go @@ -0,0 +1,27 @@ +package slogcommon + +import "log/slog" + +func FindAttrByKey(attrs []slog.Attr, key string) (slog.Attr, bool) { + for i := range attrs { + if attrs[i].Key == key { + return attrs[i], true + } + } + + return slog.Attr{}, false +} + +func FindAttrByGroupAndKey(attrs []slog.Attr, groups []string, key string) (slog.Attr, bool) { + if len(groups) == 0 { + return FindAttrByKey(attrs, key) + } + + for i := range attrs { + if attrs[i].Key == key && attrs[i].Value.Kind() == slog.KindGroup { + return FindAttrByGroupAndKey(attrs[i].Value.Group(), groups[1:], key) + } + } + + return slog.Attr{}, false +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8a1e8f8 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/samber/slog-common + +go 1.21 + +require ( + github.com/samber/lo v1.38.1 + go.uber.org/goleak v1.2.1 +) + +require golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6c5bdfd --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= +github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A= +go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= +golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM= +golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/groups.go b/groups.go new file mode 100644 index 0000000..405b1e5 --- /dev/null +++ b/groups.go @@ -0,0 +1,59 @@ +package slogcommon + +import ( + "log/slog" + + "github.com/samber/lo" +) + +func AppendAttrsToGroup(groups []string, actualAttrs []slog.Attr, newAttrs ...slog.Attr) []slog.Attr { + if len(groups) == 0 { + return UniqAttrs(append(actualAttrs, newAttrs...)) + } + + for i := range actualAttrs { + attr := actualAttrs[i] + if attr.Key == groups[0] && attr.Value.Kind() == slog.KindGroup { + actualAttrs[i] = slog.Group(groups[0], lo.ToAnySlice(AppendAttrsToGroup(groups[1:], attr.Value.Group(), newAttrs...))...) + return actualAttrs + } + } + + return UniqAttrs( + append( + actualAttrs, + slog.Group( + groups[0], + lo.ToAnySlice(AppendAttrsToGroup(groups[1:], []slog.Attr{}, newAttrs...))..., + ), + ), + ) +} + +// @TODO: should be recursive +func UniqAttrs(attrs []slog.Attr) []slog.Attr { + return uniqByLast(attrs, func(item slog.Attr) string { + return item.Key + }) +} + +func uniqByLast[T any, U comparable](collection []T, iteratee func(item T) U) []T { + result := make([]T, 0, len(collection)) + seen := make(map[U]int, len(collection)) + seenIndex := 0 + + for _, item := range collection { + key := iteratee(item) + + if index, ok := seen[key]; ok { + result[index] = item + continue + } + + seen[key] = seenIndex + seenIndex++ + result = append(result, item) + } + + return result +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..aeeff97 --- /dev/null +++ b/main_test.go @@ -0,0 +1,11 @@ +package slogcommon + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +}