Skip to content

Commit eb93327

Browse files
authored
L-147 Add documentation and example project (#14)
1 parent 4f3bda2 commit eb93327

File tree

6 files changed

+183
-2
lines changed

6 files changed

+183
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
.ruby-version
1111
coverage
1212
Gemfile.lock
13+
!/example-project/Gemfile.lock
1314
*.swp
1415
*.gem
1516

README.md

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,111 @@
11
# 🪵 Logtail Integration For Rack
22

3+
[![Logtail ruby dashboard](https://user-images.githubusercontent.com/19272921/154085622-59997d5a-3f91-4bc9-a815-3b8ead16d28d.jpeg)](https://betterstack.com/logtail)
4+
35
[![ISC License](https://img.shields.io/badge/license-ISC-ff69b4.svg)](LICENSE.md)
46
[![Gem Version](https://badge.fury.io/rb/logtail-rack.svg)](https://badge.fury.io/rb/logtail-rack)
5-
[![Build Status](https://github.com/logtail/logtail-ruby-rack/workflows/build/badge.svg)](https://github.com/logtail/logtail-ruby-rack/actions?query=workflow%3Abuild)
7+
[![Build Status](https://github.com/logtail/logtail-ruby-rack/actions/workflows/main.yml/badge.svg)](https://github.com/logtail/logtail-ruby-rack/actions/workflows/main.yml)
68

79
This library integrates the [`logtail` Ruby library](https://github.com/logtail/logtail-ruby) with the [rack](https://github.com/rack/rack) framework,
810
turning your Rack logs into rich structured events.
911

10-
* **Sign-up: [https://logtail.com](https://logtail.com)**
12+
* **Sign-up: [https://logs.betterstack.com](https://logs.betterstack.com)**
13+
14+
Collect logs directly from your Ruby Rack projects. To start logging Ruby projects explore the [Logtail Ruby library](https://github.com/logtail/logtail-ruby).
15+
16+
[Better Stack](https://logs.betterstack.com) is a hosted service that centralizes all of your logs into one place.
17+
Allowing for analysis, correlation and filtering with SQL.
18+
Actionable Grafana dashboards and collaboration come built-in.
19+
Logtail works with [any language or platform and any data source](https://betterstack.com/docs/logs/).
20+
21+
### Features
22+
- Simple integration.
23+
- Support for structured logging and events.
24+
- Automatically captures useful context.
25+
- Performant, light weight, with a thoughtful design.
26+
27+
### Supported language versions
28+
- Ruby 2.3 or newer
29+
- Rack 1.2 or newer
30+
31+
# Installation
32+
Install the Logtail Rack client library, run the following command:
33+
34+
```bash
35+
bundle add logtail-rack
36+
```
37+
38+
Alternatively, add `gem "logtail-rack"` to your `Gemfile` manually and then run `bundle install`.
39+
40+
Then add following configuration into your `config.ru`:
41+
42+
```ruby
43+
# Initialization of logging middlewares (you don't have to use all)
44+
use Logtail::Integrations::Rack::HTTPContext
45+
use Logtail::Integrations::Rack::HTTPEvents
46+
use Logtail::Integrations::Rack::ErrorEvent
47+
use Logtail::Integrations::Rack::UserContext
48+
use Logtail::Integrations::Rack::SessionContext
49+
50+
http_io_device = Logtail::LogDevices::HTTP.new("<SOURCE_TOKEN>")
51+
logger = Logtail::Logger.new(http_io_device)
52+
Logtail::Config.instance.logger = logger
53+
54+
# Here is your application initialization
55+
run ...
56+
```
57+
58+
*Don't forget to replace `<SOURCE_TOKEN>` with your actual source token which you can find by going to [Better Stack Logs](https://logs.betterstack.com/dashboard) -> Source -> Edit.*
59+
60+
---
61+
62+
# Example project
63+
64+
To help you get started with using Better Stack in your Rails projects, we have prepared a simple program that showcases the usage of Logtail logger.
65+
66+
## Download and install the example project
67+
68+
You can download the [example project](https://github.com/logtail/logtail-ruby-rack/tree/main/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:
69+
70+
```bash
71+
bundle install
72+
```
73+
74+
This will install all dependencies listed in the `Gemfile.lock` file.
75+
76+
Then replace `<SOURCE_TOKEN>` in `config.ru` with your actual source token which you can find by going to [Better Stack Logs](https://logs.betterstack.com/dashboard) -> Source -> Edit.
77+
78+
```ruby
79+
http_io_device = Logtail::LogDevices::HTTP.new("<YOUR_ACTUAL_SOURCE_TOKEN>")
80+
```
81+
82+
## Run the example project
83+
84+
To run the example application, run the following command:
85+
86+
```bash
87+
rackup
88+
```
89+
90+
This will start a local server and you visit [http://127.0.0.1:9292](http://127.0.0.1:9292) in your browser.
91+
92+
You should see the following output:
93+
94+
```bash
95+
All done!
96+
Log into your Logtail account to check your logs.
97+
```
98+
99+
This will create a total of 4 different logs. You can review these logs in Better Stack.
100+
101+
You can visit any path on the server to see the request path being logged in context. Visit [/error](http://127.0.0.1:9292) to see an example exception being logged.
102+
103+
## Explore how example project works
104+
105+
Learn how to setup Ruby logging by exploring the workings of the [example project](https://github.com/logtail/logtail-ruby-rack/tree/main/example-project) in detail.
106+
107+
---
108+
109+
## Get in touch
110+
111+
Have any questions? Please explore the Better Stack [documentation](https://betterstack.com/docs/logs/) or contact our [support](https://betterstack.com/help).

example-project/Gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source "https://rubygems.org"
2+
3+
gem "rack", "~> 3.0"
4+
5+
gem "rackup", "~> 2.1"
6+
7+
gem "logtail-rack", "~> 0.2.4"

example-project/Gemfile.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
logtail (0.1.12)
5+
msgpack (~> 1.0)
6+
logtail-rack (0.2.4)
7+
logtail (~> 0.1)
8+
rack (>= 1.2, < 4.0)
9+
msgpack (1.7.2)
10+
rack (3.0.0)
11+
rackup (2.1.0)
12+
rack (>= 3)
13+
webrick (~> 1.8)
14+
webrick (1.8.1)
15+
16+
PLATFORMS
17+
arm64-darwin-22
18+
19+
DEPENDENCIES
20+
logtail-rack (~> 0.2.4)
21+
rack (~> 3.0)
22+
rackup (~> 2.1)
23+
24+
BUNDLED WITH
25+
2.3.14

example-project/config.ru

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'logtail-rack'
2+
require './logging_app'
3+
4+
# Initialization of logging middlewares
5+
use Logtail::Integrations::Rack::HTTPContext
6+
use Logtail::Integrations::Rack::HTTPEvents
7+
use Logtail::Integrations::Rack::ErrorEvent
8+
9+
# HTTP IO device sends logs to Better Stack, replace <SOURCE_TOKEN> with your real source token
10+
http_io_device = Logtail::LogDevices::HTTP.new("<SOURCE_TOKEN>")
11+
12+
# STDOUT IO device sends logs to console output
13+
stdout_io_device = STDOUT
14+
15+
# Logger initialization, you can use any number of IO devices
16+
logger = Logtail::Logger.new(http_io_device, stdout_io_device)
17+
Logtail::Config.instance.logger = logger
18+
19+
# App initialization
20+
logging_app = LoggingApp.new(logger)
21+
run do |env|
22+
logging_app.run(env)
23+
end

example-project/logging_app.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'logtail-rack'
2+
3+
class LoggingApp
4+
def initialize(logger)
5+
@logger = logger
6+
end
7+
8+
def run(env)
9+
@logger.info("I am using Better Stack! 🚀")
10+
11+
# You can also provide additional information when logging
12+
@logger.debug("Logging structured data...",
13+
name: {
14+
first: "John",
15+
last: "Smith"
16+
},
17+
id: 123456
18+
)
19+
20+
raise RuntimeError.new("Visiting /error raises an error. You should see it in Better Stack.") if env["REQUEST_PATH"].start_with?("/error")
21+
22+
[200, {}, ["All done!\nLog into your Logtail account to check your logs."]]
23+
end
24+
end

0 commit comments

Comments
 (0)