Skip to content

Commit 21269a5

Browse files
committed
docs, tests: replace runnable examples with testable example and update README
1 parent e72fbb6 commit 21269a5

File tree

6 files changed

+79
-135
lines changed

6 files changed

+79
-135
lines changed

README.md

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66
[![Telegram EN][telegram-badge]][telegram-en-url]
77
[![Telegram RU][telegram-badge]][telegram-ru-url]
88

9+
## Table of contents
10+
11+
- [go-tlog](#go-tlog)
12+
- [Features](#features)
13+
- [Installation](#installation)
14+
- [Quick start](#quick-start)
15+
- [Configuration](#configuration)
16+
- [type Opts](#type-opts)
17+
- [Main API](#main-api)
18+
- [Log levels](#log-levels)
19+
- [Output formats](#output-formats)
20+
- [Output destinations](#output-destinations)
21+
- [Examples](#examples)
22+
- [Testing](#testing)
23+
- [License](#license)
24+
925
# go-tlog
1026

1127
`go-tlog` is a lightweight and configurable logging library for Go applications.
@@ -133,41 +149,11 @@ Supported targets:
133149

134150
## Examples
135151

136-
Ready-to-run examples are located in the `_examples/` directory:
137-
138-
```
139-
_examples/
140-
├── stdout/
141-
│ └── main.go
142-
├── stderr/
143-
│ └── main.go
144-
├── file/
145-
│ └── main.go
146-
└── multi/
147-
└── main.go
148-
```
152+
Included examples:
149153

150-
Run examples:
151-
152-
```bash
153-
# Example 1 — log to STDOUT in text format
154-
go run ./_examples/stdout
155-
156-
# Example 2 — log to STDERR in JSON format
157-
# Redirect stderr to a file and inspect its contents
158-
go run ./_examples/stderr 2> logs.json
159-
cat logs.json
160-
161-
# Example 3 — log to a file in /tmp directory
162-
# The file will be created automatically if it doesn’t exist
163-
go run ./_examples/file
164-
cat /tmp/tlog_demo/app.log
165-
166-
# Example 4 — log to multiple destinations (stdout + file)
167-
# This writes the same log entry both to console and to /tmp/tlog_multi/app.log
168-
go run ./_examples/multi
169-
cat /tmp/tlog_multi/app.log
170-
```
154+
- **ExampleNew_text** — basic text logger writing to stdout
155+
- **ExampleNew_json** — JSON logging
156+
- **ExampleNew_multi** — logging to multiple destinations (`stdout,/tmp/...`)
171157

172158
Each example demonstrates different combinations of Path, Format, and Level,
173159
including how to log to multiple outputs at the same time.
@@ -177,7 +163,7 @@ including how to log to multiple outputs at the same time.
177163
## Testing
178164

179165
```bash
180-
go test ./...
166+
make test
181167
```
182168

183169
---

_examples/file/main.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

_examples/multi/main.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

_examples/stderr/main.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

_examples/stdout/main.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

example_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package tlog_test
2+
3+
import (
4+
"log/slog"
5+
6+
"github.com/tarantool/go-tlog"
7+
)
8+
9+
// ExampleNew_text shows how to create a text logger that writes to stdout.
10+
func ExampleNew_text() {
11+
log, err := tlog.New(tlog.Opts{
12+
Level: tlog.LevelInfo,
13+
Format: tlog.FormatText,
14+
Path: "stdout",
15+
})
16+
if err != nil {
17+
panic(err)
18+
}
19+
defer func() { _ = log.Close() }()
20+
21+
logger := log.Logger().With(slog.String("component", "example_text"))
22+
logger.Info("service started")
23+
}
24+
25+
// ExampleNew_json shows how to create a JSON logger that writes to stdout.
26+
func ExampleNew_json() {
27+
log, err := tlog.New(tlog.Opts{
28+
Level: tlog.LevelInfo,
29+
Format: tlog.FormatJSON,
30+
Path: "stdout",
31+
})
32+
if err != nil {
33+
panic(err)
34+
}
35+
defer func() { _ = log.Close() }()
36+
37+
logger := log.Logger().With(
38+
slog.String("component", "example_json"),
39+
slog.String("request_id", "abc-123"),
40+
)
41+
logger.Error("request failed")
42+
}
43+
44+
// ExampleNew_multi shows how to log to multiple destinations at once.
45+
func ExampleNew_multi() {
46+
log, err := tlog.New(tlog.Opts{
47+
Level: tlog.LevelInfo,
48+
Format: tlog.FormatText,
49+
Path: "stdout,/tmp/tlog_example_multi.log",
50+
})
51+
if err != nil {
52+
panic(err)
53+
}
54+
defer func() { _ = log.Close() }()
55+
56+
logger := log.Logger().With(slog.String("component", "example_multi"))
57+
logger.Info("message written to stdout and file")
58+
}

0 commit comments

Comments
 (0)