|
| 1 | +[](https://github.com/tarantool/go-tlog/actions/workflows/ci.yml) • |
| 2 | +[Telegram EN](https://t.me/tarantool) • |
| 3 | +[Telegram RU](https://t.me/tarantoolru) |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +<p href="https://www.tarantool.io"> |
| 8 | + <img src="https://github.com/tarantool.png" align="right" width=250> |
| 9 | +</p> |
| 10 | + |
1 | 11 | # go-tlog |
| 12 | + |
| 13 | +`go-tlog` is a lightweight and configurable logging library for Go applications. |
| 14 | +It provides structured logging with multiple output destinations, flexible formatting, |
| 15 | +and fine-grained log-level control. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Features |
| 20 | + |
| 21 | +- Simple setup via configuration struct |
| 22 | +- Text or JSON output formats |
| 23 | +- Multiple output targets: **stdout**, **stderr**, **files** |
| 24 | +- Log levels: `Trace`, `Debug`, `Info`, `Warn`, `Error` |
| 25 | +- Automatic timestamp, source file, and line number |
| 26 | +- Stacktrace for errors |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## Installation |
| 31 | + |
| 32 | +```bash |
| 33 | +go get github.com/tarantool/go-tlog@latest |
| 34 | +``` |
| 35 | + |
| 36 | +Then import: |
| 37 | + |
| 38 | +```go |
| 39 | +import "github.com/tarantool/go-tlog" |
| 40 | +``` |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## Quick start |
| 45 | + |
| 46 | +```go |
| 47 | +package main |
| 48 | + |
| 49 | +import "github.com/tarantool/go-tlog" |
| 50 | + |
| 51 | +func main() { |
| 52 | + log, err := tlog.New(tlog.Opts{ |
| 53 | + Level: tlog.LevelInfo, |
| 54 | + Format: tlog.FormatText, |
| 55 | + Path: "stdout", |
| 56 | + }) |
| 57 | + if err != nil { |
| 58 | + panic(err) |
| 59 | + } |
| 60 | + defer log.Close() |
| 61 | + |
| 62 | + logger := log.Logger().With(tlog.String("component", "demo")) |
| 63 | + logger.Info("service started", "port", 8080) |
| 64 | + logger.Error("failed to connect", "err", "timeout") |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +Output: |
| 69 | + |
| 70 | +``` |
| 71 | +2025-11-10T13:30:01+05:00 INFO service started component=demo port=8080 |
| 72 | +2025-11-10T13:30:01+05:00 ERROR failed to connect err=timeout component=demo stacktrace="..." |
| 73 | +``` |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +## Configuration |
| 78 | + |
| 79 | +### `type Opts` |
| 80 | + |
| 81 | +```go |
| 82 | +type Opts struct { |
| 83 | + Level Level // minimal log level |
| 84 | + Format Format // FormatText or FormatJSON |
| 85 | + Path string // comma-separated outputs: "stdout,/var/log/app.log" |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Main API |
| 90 | + |
| 91 | +| Function | Description | |
| 92 | +|------------------|------------------------------------------| |
| 93 | +| `tlog.New(opts)` | Create a new logger | |
| 94 | +| `Logger()` | Return the underlying logger for use | |
| 95 | +| `Close()` | Flush buffers and close file descriptors | |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +## Log levels |
| 100 | + |
| 101 | +| Level | When to use | |
| 102 | +|---------|---------------------------------------------| |
| 103 | +| `Trace` | Low-level tracing | |
| 104 | +| `Debug` | Debugging information | |
| 105 | +| `Info` | Normal operational messages | |
| 106 | +| `Warn` | Non-fatal warnings | |
| 107 | +| `Error` | Errors and exceptions (includes stacktrace) | |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +## Output formats |
| 112 | + |
| 113 | +| Format | Example | |
| 114 | +|--------------|---------------------------------------------------------------| |
| 115 | +| `FormatText` | `2025-11-10T13:31:45+05:00 INFO message key=value` | |
| 116 | +| `FormatJSON` | `{"time":"...","level":"INFO","msg":"message","key":"value"}` | |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## Output destinations |
| 121 | + |
| 122 | +You can specify multiple targets separated by commas: |
| 123 | + |
| 124 | +```go |
| 125 | +Path: "stdout,/tmp/app.log" |
| 126 | +``` |
| 127 | + |
| 128 | +Supported targets: |
| 129 | + |
| 130 | +- `stdout` |
| 131 | +- `stderr` |
| 132 | +- File paths (created automatically if not present) |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## Examples |
| 137 | + |
| 138 | +Ready-to-run examples are located in the `_examples/` directory: |
| 139 | + |
| 140 | +``` |
| 141 | +_examples/ |
| 142 | + ├── stdout/ |
| 143 | + │ └── main.go |
| 144 | + ├── stderr/ |
| 145 | + │ └── main.go |
| 146 | + ├── file/ |
| 147 | + │ └── main.go |
| 148 | + └── multi/ |
| 149 | + └── main.go |
| 150 | +``` |
| 151 | + |
| 152 | +Run examples: |
| 153 | + |
| 154 | +```bash |
| 155 | +# Example 1 — log to STDOUT in text format |
| 156 | +go run ./_examples/stdout |
| 157 | + |
| 158 | +# Example 2 — log to STDERR in JSON format |
| 159 | +# Redirect stderr to a file and inspect its contents |
| 160 | +go run ./_examples/stderr 2> logs.json |
| 161 | +cat logs.json |
| 162 | + |
| 163 | +# Example 3 — log to a file in /tmp directory |
| 164 | +# The file will be created automatically if it doesn’t exist |
| 165 | +go run ./_examples/file |
| 166 | +cat /tmp/tlog_demo/app.log |
| 167 | + |
| 168 | +# Example 4 — log to multiple destinations (stdout + file) |
| 169 | +# This writes the same log entry both to console and to /tmp/tlog_multi/app.log |
| 170 | +go run ./_examples/multi |
| 171 | +cat /tmp/tlog_multi/app.log |
| 172 | +``` |
| 173 | + |
| 174 | +Each example demonstrates different combinations of Path, Format, and Level, |
| 175 | +including how to log to multiple outputs at the same time. |
| 176 | + |
| 177 | +--- |
| 178 | + |
| 179 | +## Testing |
| 180 | + |
| 181 | +```bash |
| 182 | +go test ./... |
| 183 | +``` |
| 184 | + |
| 185 | +--- |
| 186 | + |
| 187 | +## License |
| 188 | + |
| 189 | +BSD 2-Clause License — see [LICENSE](LICENSE) |
0 commit comments