Skip to content

Commit

Permalink
Merge pull request #5 from StephanHCB/issue-4-improvements
Browse files Browse the repository at this point in the history
Issue 4 improvements
  • Loading branch information
Roshick authored Dec 9, 2023
2 parents ef6f2ab + 264015c commit 20b7ec6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Go

on:
push:
branches: [ main ]
paths-ignore:
- '**.md'
pull_request:
branches: [ main ]
paths-ignore:
- '**.md'

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.21'

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,41 @@ by, for instance, 'PANIC' being emitted as 'ERROR+8'.

This section delves into practical use cases to aid the integration of the library into various applications.

### Simple Plaintext Logging

```
// create a simple plaintext logger and set the autumn global default logger to it
aulogging.Logger = logging.New()
// use it
aulogging.Logger.NoCtx().Info().Print("hello")
```

### Structured JSON Logging and Context Awareness

```
// build a structured logger using the standard slog.NewJSONHandler (could use any other slog.Handler)
structuredLogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
// set the autumn global default logger to it
aulogging.Logger = logging.New().WithLogger(structuredLogger)
// use it (chain style or convenience style)
aulogging.Logger.NoCtx().Info().Print("hello")
aulogging.Info(context.Background(), "hello, too")
// augment the logger with an extra field (could be done in a middleware, as long as the field value won't change)
augmentedLogger := structuredLogger.With("some-field", "some-value")
// place it in a context (could be done in a middleware)
ctx := context.Background()
ctx = logging.ContextWithLogger(ctx, augmentedLogger)
// use it from the context (chain style or convenience style)
aulogging.Logger.Ctx(ctx).Info().Print("hi")
aulogging.Info(ctx, "hi, again")
```

### Tracing

Consider a scenario where we aim to append tracing information to every log record generated during a request to one of
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/Roshick/go-autumn-slog

go 1.21.4
go 1.21

require (
github.com/StephanHCB/go-autumn-config-api v0.2.1
Expand Down
4 changes: 3 additions & 1 deletion pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ type Logging struct {
}

func New() *Logging {
return &Logging{}
return &Logging{
logger: slog.Default(), // ensure non-nil
}
}

func (l *Logging) WithLogger(logger *slog.Logger) *Logging {
Expand Down

0 comments on commit 20b7ec6

Please sign in to comment.