Skip to content

Commit d4d2060

Browse files
authored
Create README.md
1 parent 45968d2 commit d4d2060

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

example-project/README.md

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
# Example project
2+
3+
To help you get started with using Logtail in your Ruby projects, we have prepared a simple program that showcases the usage of Logtail logger.
4+
5+
## Download and install the example project
6+
7+
You can download the example project from GitHub directly or you can clone it to a select directory. Make sure you are in the projects directory and run the following command:
8+
9+
```bash
10+
bundle install
11+
```
12+
13+
This will install all dependencies listed in the `Gemfile.lock` file.
14+
15+
Alternatively, add `gem "logtail"` to your `Gemfile` manually and then run `bundle install`.
16+
17+
## Run the example project
18+
19+
Replace `<source-token>` with your actual source token in the `main.rb` file. You can find your source token by going to logtail.com -> sources -> edit.
20+
21+
To run the example application, run the following command adding your source token:
22+
23+
```bash
24+
ruby main.rb <source-token>
25+
```
26+
27+
This will create a total of 5 different logs, each corresponding to a different log level. You can review these logs in Logtail.
28+
29+
# Logging
30+
31+
The `logger` instance we created in the installation section is used to send log messages to Logtail. It provides 5 logging methods for the 5 default log levels. The log levels and their method are:
32+
33+
- **DEBUG** - Send debug messages using the `debug()` method
34+
- **INFO** - Send informative messages about the application progress using the `info()` method
35+
- **WARN** - Report non-critical issues using the `warn()` method
36+
- **ERROR** - Send messages about serious problems using the `error()` method
37+
- **FATAL** - Send messages about fatal events that caused the app to crash using the `fatal()` method
38+
39+
## Logging example
40+
41+
In this example, we will send two logs - DEBUG and INFO
42+
43+
### Ruby
44+
45+
```ruby
46+
# Send debug logs messages using the debug() method
47+
logger.debug("Logtail is ready!")
48+
49+
# Send informative messages about interesting events using the info() method
50+
logger.info("I am using Logtail!")
51+
```
52+
53+
### Ruby on Rails
54+
55+
```ruby
56+
# Send debug logs messages using the debug() method
57+
Rails.logger.debug("Logtail is ready!")
58+
59+
# Send informative messages about interesting events using the info() method
60+
Rails.logger.info("I am using Logtail!")
61+
```
62+
63+
This will create the following output:
64+
65+
```json
66+
{
67+
"dt": "2021-03-29T11:24:54.788Z",
68+
"level": "debug",
69+
"message": "Logtail is ready!",
70+
"context": {
71+
"runtime": {
72+
"thread_id": 123,
73+
"file": "main.rb",
74+
"line": 6,
75+
"frame": null,
76+
"frame_label": "<main>"
77+
},
78+
"system": {
79+
"hostname": "hostname"
80+
"pid": 1234
81+
}
82+
}
83+
}
84+
85+
{
86+
"dt": "2021-03-29T11:24:54.788Z",
87+
"level": "info",
88+
"message": "I am using Logtail!",
89+
"context": {
90+
"runtime": {
91+
"thread_id": 123,
92+
"file": "main.rb",
93+
"line": 6,
94+
"frame": null,
95+
"frame_label": "<main>"
96+
},
97+
"system": {
98+
"hostname": "hostname"
99+
"pid": 1234
100+
}
101+
}
102+
}
103+
```
104+
105+
## Log structured data
106+
107+
You can also log additional structured data. This can help you provide additional information when debugging and troubleshooting your application. You can provide this data as the second argument to any logging method.
108+
109+
```ruby
110+
# Send messages about worrying events using the warn() method
111+
# You can also log additional structured data
112+
logger.warn(
113+
"log structured data",
114+
item: {
115+
url: "https://fictional-store.com/item-123",
116+
price: 100.00
117+
}
118+
)
119+
```
120+
121+
This will create the following output:
122+
123+
```json
124+
{
125+
"dt": "2021-03-29T11:24:54.788Z",
126+
"level": "warn",
127+
"message": "log structured data",
128+
"item": {
129+
"url": "https://fictional-store.com/item-123",
130+
"price": 100.00
131+
},
132+
"context": {
133+
"runtime": {
134+
"thread_id": 123,
135+
"file": "main.rb",
136+
"line": 7,
137+
"frame": null,
138+
"frame_label": "<main>"
139+
},
140+
"system": {
141+
"hostname": "hostname"
142+
"pid": 1234
143+
}
144+
}
145+
}
146+
```
147+
148+
## Context
149+
150+
We add information about the current runtime environment and the current process into a `context` field of the logged item by default.
151+
152+
If you want to add custom information to all logged items (e.g., the ID of the current user), you can do so by adding a custom context:
153+
154+
```ruby
155+
# Provide context to the logs
156+
Logtail.with_context(user: { id: 123 }) do
157+
logger.info('new subscription')
158+
end
159+
```
160+
161+
This will generate the following JSON output:
162+
163+
```json
164+
{
165+
"dt": "2021-03-29T11:24:54.788Z",
166+
"level": "warn",
167+
"message": "new subscription",
168+
"context": {
169+
"runtime": {
170+
"thread_id": 123456,
171+
"file": "main.rb",
172+
"line": 2,
173+
"frame": null,
174+
"frame_label": "<main>"
175+
},
176+
"system": {
177+
"hostname": "hostname"
178+
"pid": 1234
179+
},
180+
"user": {
181+
"id": 123
182+
}
183+
}
184+
}
185+
```
186+
187+
We will automatically add the information about the current user to each log if you're using Ruby on Rails and the Devise gem.
188+
189+
If you're not using Devise or you want to log some additional information for every request your Rails app handles, you can easily implement this using Rails' `around_action` in your application controller. A simple implementation could look like this:
190+
191+
```ruby
192+
class ApplicationController < ActionController::Base
193+
around_action :with_logtail_context
194+
195+
private
196+
197+
def with_logtail_context
198+
if user_signed_in?
199+
Logtail.with_context(user_context) { yield }
200+
else
201+
yield
202+
end
203+
end
204+
205+
def user_context
206+
Logtail::Contexts::User.new(
207+
id: current_user.id,
208+
name: current_user.name,
209+
email: current_user.email
210+
)
211+
end
212+
end
213+
```

0 commit comments

Comments
 (0)