-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 5c9dec8
Showing
38 changed files
with
1,178 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# | ||
# Do not edit. This file was generated via the "workflow" command line tool. | ||
# More information about the tool can be found at github.com/xh3b4sd/workflow. | ||
# | ||
# workflow create dependabot -b master -r xh3b4sd | ||
# | ||
|
||
version: 2 | ||
updates: | ||
|
||
- package-ecosystem: "github-actions" | ||
directory: "/" | ||
ignore: | ||
- dependency-name: "*" | ||
update-types: ["version-update:semver-patch"] | ||
open-pull-requests-limit: 10 | ||
reviewers: | ||
- "xh3b4sd" | ||
schedule: | ||
interval: "daily" | ||
time: "04:00" | ||
target-branch: "master" | ||
|
||
- package-ecosystem: "gomod" | ||
directory: "/" | ||
ignore: | ||
- dependency-name: "*" | ||
update-types: ["version-update:semver-patch"] | ||
open-pull-requests-limit: 10 | ||
reviewers: | ||
- "xh3b4sd" | ||
schedule: | ||
interval: "daily" | ||
time: "04:00" | ||
target-branch: "master" |
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,43 @@ | ||
# | ||
# Do not edit. This file was generated via the "workflow" command line tool. | ||
# More information about the tool can be found at github.com/xh3b4sd/workflow. | ||
# | ||
# workflow create golang | ||
# | ||
|
||
name: "go-build" | ||
|
||
on: "push" | ||
|
||
jobs: | ||
go-build: | ||
runs-on: "ubuntu-latest" | ||
steps: | ||
|
||
- name: "Setup Git Project" | ||
uses: "actions/checkout@v4" | ||
|
||
- name: "Setup Go Env" | ||
uses: "actions/setup-go@v4" | ||
with: | ||
cache: true | ||
go-version: "1.21.1" | ||
|
||
- name: "Check Go Dependencies" | ||
run: | | ||
go mod tidy | ||
git diff --exit-code | ||
- name: "Check Go Tests" | ||
run: | | ||
go test ./... -race | ||
- name: "Check Go Formatting" | ||
run: | | ||
test -z $(gofmt -l -s .) | ||
- name: "Check Go Linters" | ||
run: | | ||
curl -LOs https://github.com/golangci/golangci-lint/releases/download/v1.54.2/golangci-lint-1.54.2-linux-amd64.tar.gz | ||
tar -xzf golangci-lint-1.54.2-linux-amd64.tar.gz | ||
./golangci-lint-1.54.2-linux-amd64/golangci-lint run |
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 @@ | ||
/.vscode/ |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 xh3b4sd | ||
|
||
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. |
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,61 @@ | ||
# logger | ||
|
||
Simple structured logging with severity awareness. Logging is one essential | ||
observability technique in order to not fly blindly. Structuring logs is simple | ||
and yet powerful because it supports different formats that can be addapted to | ||
any application's needs. | ||
|
||
|
||
|
||
### Log Level Awareness | ||
|
||
There are 4 log levels supported, namely `debug`, `info`, `warning` and `error`. | ||
Logs are emitted based on filter configurations provided to the logger creation. | ||
Below is shown which logger setting causes which behaviour of emitted logs. | ||
|
||
logger setting | emitted logs | ||
---|--- | ||
`debug` | `debug`, `info`, `warning`, `error` | ||
`info` | `info`, `warning`, `error` | ||
`warning` | `warning`, `error` | ||
`error` | `error` | ||
|
||
|
||
|
||
### Logging In Code | ||
|
||
Below is an example of emitting `info` logs providing information of expected | ||
business logic behaviour. | ||
|
||
```golang | ||
r.log.Log(ctx, "level", "info", "message", "something important happened") | ||
``` | ||
|
||
Below is an example of emitting `error` logs providing information of unexpected | ||
business logic behaviour. Note that stack trace logging is wel integrated with | ||
[tracer](https://github.com/xh3b4sd/tracer). | ||
|
||
```golang | ||
r.log.Log(ctx, "level", "error", "message", "something bad happened", "stack", tracer.Stack(err)) | ||
``` | ||
|
||
|
||
|
||
### Log Line Printing | ||
|
||
Using `logger` prints log lines like shown in the example below. Note that you | ||
can configure the `io.Writer` that actually handles the byte streams. The | ||
default is configured to be `os.Stdout`. | ||
|
||
```json | ||
{ | ||
"time": "2006-01-02 15:04:05", | ||
"leve": "info", | ||
"mess": "something important happened", | ||
"stac": [ | ||
"--REPLACED--/logger_test.go:104", | ||
"--REPLACED--/logger_test.go:105" | ||
], | ||
"call": "--REPLACED--/logger_test.go:129" | ||
} | ||
``` |
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,15 @@ | ||
package logger | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
var EmptyCaller = func() string { | ||
return "" | ||
} | ||
|
||
var DefaultCaller = func() string { | ||
_, file, line, _ := runtime.Caller(2) | ||
return fmt.Sprintf("%s:%d", file, line) | ||
} |
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,15 @@ | ||
package logger | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/xh3b4sd/tracer" | ||
) | ||
|
||
var invalidConfigError = &tracer.Error{ | ||
Kind: "invalidConfigError", | ||
} | ||
|
||
func IsInvalidConfig(err error) bool { | ||
return errors.Is(err, invalidConfigError) | ||
} |
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,9 @@ | ||
package logger | ||
|
||
func Default() Interface { | ||
return New(Config{}) | ||
} | ||
|
||
func Fake() Interface { | ||
return fake{} | ||
} |
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,7 @@ | ||
package logger | ||
|
||
import "context" | ||
|
||
type fake struct{} | ||
|
||
func (r fake) Log(ctx context.Context, pai ...string) {} |
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,7 @@ | ||
package logger | ||
|
||
import "context" | ||
|
||
var EmptyFilter = func(context.Context, map[string]string) bool { return false } | ||
|
||
var DefaultFilter = NewLevelFilter(LevelInfo) |
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,160 @@ | ||
package logger | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/xh3b4sd/logger/meta" | ||
) | ||
|
||
var DefaultFormatter = JSONFormatter | ||
|
||
// JSONFormatter transforms the given map into the string representation of a | ||
// simple JSON object. One specificity of JSONFormatter is that it treats the | ||
// value of key "stack" differently. In case a key-value pair with the key | ||
// "stack" is provided, the associated value is treated like a JSON object, and | ||
// not like a JSON string. This difference in behaviour is unique compared to | ||
// all other key-value pairs map may carry with it. | ||
func JSONFormatter(c context.Context, m map[string]string) string { | ||
// Once we have the full map of key-value pairs we need the keys only in | ||
// order to sort them. We want the emitted log line to be alphabetically | ||
// ordered by keys. | ||
var ctx []string | ||
var oth []string | ||
{ | ||
for k := range m { | ||
if k == KeyCal || k == KeyLev || k == KeyMes || k == KeySta || k == KeyTim { | ||
continue | ||
} | ||
|
||
if meta.Has(c, k) { | ||
ctx = append(ctx, k) | ||
} else { | ||
oth = append(oth, k) | ||
} | ||
} | ||
|
||
sort.Strings(ctx) | ||
sort.Strings(oth) | ||
} | ||
|
||
var key func(string) string | ||
{ | ||
key = func(k string) string { | ||
if k == KeyCal || k == KeyCod || k == KeyDes || k == KeyDoc || k == KeyLev || k == KeyMes || k == KeySta || k == KeyTim { | ||
return k[:4] | ||
} | ||
|
||
return k | ||
} | ||
} | ||
|
||
var val func(string) string | ||
{ | ||
val = func(k string) string { | ||
v, e := m[k] | ||
if !e { | ||
return "" | ||
} | ||
|
||
if k == KeyLev { | ||
return v[:4] | ||
} | ||
|
||
return v | ||
} | ||
} | ||
|
||
var bui func(string) string | ||
{ | ||
bui = func(k string) string { | ||
var s string | ||
|
||
s += "\"" | ||
s += key(k) | ||
s += "\"" | ||
s += ":" | ||
|
||
if k != KeySta { | ||
s += "\"" | ||
} | ||
|
||
s += val(k) | ||
|
||
if k != KeySta { | ||
s += "\"" | ||
} | ||
|
||
return s | ||
} | ||
} | ||
|
||
// Below we compute a JSON string for the structured log line we want to | ||
// emit. | ||
var s string | ||
{ | ||
{ | ||
s += "{ " | ||
} | ||
|
||
{ | ||
s += bui(KeyTim) | ||
s += ", " | ||
s += bui(KeyLev) | ||
s += ", " | ||
} | ||
|
||
{ | ||
for _, k := range ctx { | ||
s += bui(k) | ||
s += ", " | ||
} | ||
} | ||
|
||
{ | ||
s += bui(KeyMes) | ||
} | ||
|
||
{ | ||
for i, k := range oth { | ||
if i == 0 { | ||
s += ", " | ||
} | ||
if m[k] == "" { | ||
continue | ||
} | ||
s += bui(k) | ||
if i+1 < len(oth) { | ||
s += ", " | ||
} | ||
} | ||
} | ||
|
||
{ | ||
_, e := m[KeySta] | ||
if e { | ||
if !strings.HasSuffix(s, ", ") { | ||
s += ", " | ||
} | ||
s += bui(KeySta) | ||
} | ||
} | ||
|
||
{ | ||
_, e := m[KeyCal] | ||
if e { | ||
if !strings.HasSuffix(s, ", ") { | ||
s += ", " | ||
} | ||
s += bui(KeyCal) | ||
} | ||
} | ||
|
||
{ | ||
s += " }" | ||
} | ||
} | ||
|
||
return s | ||
} |
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,10 @@ | ||
module github.com/xh3b4sd/logger | ||
|
||
go 1.21 | ||
|
||
require ( | ||
github.com/google/go-cmp v0.6.0 | ||
github.com/xh3b4sd/tracer v0.11.1 | ||
) | ||
|
||
retract [v0.0.0, v0.8.0] |
Oops, something went wrong.