File: formatters.html.md

package info (click to toggle)
ruby-dry-logger 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 444 kB
  • sloc: ruby: 2,170; makefile: 4; sh: 4
file content (274 lines) | stat: -rw-r--r-- 6,146 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
---
title: Formatters
layout: gem-single
name: dry-logger
---

Formatters control how log entries are serialized for output. dry-logger includes three built-in formatters optimized for different use cases.

## Built-in formatters

### String formatter (default)

The `:string` formatter outputs human-readable text in `key=value` format. It's ideal for development and when you need to read logs directly.

```ruby
logger = Dry.Logger(:my_app, formatter: :string)

logger.info("User logged in", user_id: 42, role: "admin")
# User logged in user_id=42 role="admin"
```

The string formatter:

- Formats payloads as `key=value` pairs
- Quotes string values
- Supports colorized output
- Handles exceptions with full backtraces
- Works with templates for customization

### JSON formatter

The `:json` formatter outputs structured JSON, perfect for production environments and log aggregation tools.

```ruby
logger = Dry.Logger(:my_app, formatter: :json)

logger.info("User logged in", user_id: 42, role: "admin")
# {"progname":"my_app","severity":"INFO","time":"2023-10-15T14:23:45Z","message":"User logged in","user_id":42,"role":"admin"}
```

The JSON formatter:

- Outputs valid JSON on each line
- Converts timestamps to UTC ISO8601 format
- Flattens exception data into the JSON structure
- Perfect for tools like Elasticsearch, Splunk, or CloudWatch

Example with exception:

```ruby
begin
  raise ArgumentError, "Invalid input"
rescue => e
  logger.error(e)
end
# {"progname":"my_app","severity":"ERROR","time":"2023-10-15T14:24:12Z","exception":"ArgumentError","message":"Invalid input","backtrace":["..."]}
```

### Rack formatter

The `:rack` formatter is specialized for logging HTTP requests in web applications.

```ruby
logger = Dry.Logger(:my_app, formatter: :rack)

logger.info(
  verb: "GET",
  path: "/users/42",
  status: 200,
  elapsed: "12ms",
  ip: "192.168.1.1",
  length: 1024,
  params: {page: 1}
)
# [my_app] [INFO] [2023-10-15 14:25:30 +0000] GET 200 12ms 192.168.1.1 /users/42 1024
#   {"page":1}
```

The rack formatter uses a predefined template that formats common HTTP request fields in a compact, readable format.

## Per-backend formatters

Different backends can use different formatters:

```ruby
logger = Dry.Logger(:my_app) do |setup|
  # Human-readable logs to stdout for development
  setup.add_backend(
    stream: $stdout,
    formatter: :string,
    template: :details
  )

  # JSON logs to file for production analysis
  setup.add_backend(
    stream: "logs/app.json",
    formatter: :json
  )

  # Rack-formatted HTTP logs
  setup.add_backend(
    stream: "logs/requests.log",
    formatter: :rack,
    log_if: -> (entry) { entry.key?(:verb) }
  )
end

# Now this logs to all three backends in different formats
logger.info("Starting server", port: 3000)
```

## Formatter options

### String formatter options

The string formatter supports several customization options:

#### Templates

Control the output format using templates (see [Templates](docs::templates) for details):

```ruby
logger = Dry.Logger(:my_app,
  formatter: :string,
  template: "[%<severity>s] %<time>s - %<message>s %<payload>s"
)

logger.info("Server started", port: 3000)
# [INFO] 2023-10-15 14:30:00 +0000 - Server started port=3000
```

#### Colorization

Enable colorized output for better readability in terminals:

```ruby
logger = Dry.Logger(:my_app,
  formatter: :string,
  colorize: true
)

logger.debug("Debug message")  # Cyan
logger.info("Info message")    # Magenta
logger.warn("Warning")         # Yellow
logger.error("Error")          # Red
logger.fatal("Fatal error")    # Red
```

#### Custom severity colors

Customize which colors are used for each severity level:

```ruby
logger = Dry.Logger(:my_app,
  formatter: :string,
  colorize: true,
  severity_colors: {
    debug: :gray,
    info: :green,
    warn: :yellow,
    error: :red,
    fatal: :magenta
  }
)
```

Available colors:

- `:black`
- `:red`
- `:green`
- `:yellow`
- `:blue`
- `:magenta`
- `:cyan`
- `:gray`

### JSON formatter options

The JSON formatter automatically:

- Converts all timestamps to UTC
- Formats timestamps as ISO8601 strings
- Includes all metadata (progname, severity, time)
- Merges payload data into the root object

No additional configuration is needed for the JSON formatter.

## Exception formatting

### String formatter

Exceptions are formatted with full details:

```ruby
logger = Dry.Logger(:my_app, formatter: :string, template: :details)

begin
  File.read("missing.txt")
rescue => e
  logger.error(e)
end

# [my_app] [ERROR] [2023-10-15 14:35:12 +0000]
#   No such file or directory @ rb_sysopen - missing.txt (Errno::ENOENT)
#   /path/to/file.rb:10:in `read'
#   /path/to/file.rb:10:in `<main>'
```

### JSON formatter

Exceptions are serialized as JSON fields:

```ruby
logger = Dry.Logger(:my_app, formatter: :json)

begin
  File.read("missing.txt")
rescue => e
  logger.error(e)
end

# {"progname":"my_app","severity":"ERROR","time":"2023-10-15T14:35:12Z","exception":"Errno::ENOENT","message":"No such file or directory @ rb_sysopen - missing.txt","backtrace":["..."]}
```

## Custom formatters

Create formatters for specialized output needs.

### Simple custom formatter

```ruby
class CustomFormatter
  def call(_severity, _time, _progname, entry)
    # Must return a string
    "#{entry.severity} | #{entry.message} | #{entry.payload.inspect}\n"
  end
end

logger = Dry.Logger(:my_app, formatter: CustomFormatter.new)

logger.info("Test", user_id: 42)
# INFO | Test | {:user_id=>42}
```

### Extending built-in formatters

```ruby
class MyFormatter < Dry::Logger::Formatters::String
  private

  # Customize how specific fields are formatted
  def format_user_id(value)
    "USER:#{value}"
  end

  def format_duration(value)
    "#{value}ms"
  end
end

# Register your formatter
Dry::Logger.register_formatter(:my_formatter, MyFormatter)

logger = Dry.Logger(:my_app,
  formatter: :my_formatter,
  template: "%<user_id>s took %<duration>s"
)

logger.info(user_id: 42, duration: 150)
# USER:42 took 150ms
```

For complete, realistic configuration examples, see the [Examples](docs::examples) page.