https://github.com/yegor256/elapsed
Ruby function to track the time elapsed by the execution of a block and print it to the log
https://github.com/yegor256/elapsed
logging ruby ruby-gem time-lapse
Last synced: 5 months ago
JSON representation
Ruby function to track the time elapsed by the execution of a block and print it to the log
- Host: GitHub
- URL: https://github.com/yegor256/elapsed
- Owner: yegor256
- License: mit
- Created: 2024-08-14T12:08:40.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2026-01-28T03:06:28.000Z (5 months ago)
- Last Synced: 2026-01-31T16:04:51.021Z (5 months ago)
- Topics: logging, ruby, ruby-gem, time-lapse
- Language: Ruby
- Homepage: https://rubygems.org/gems/elapsed
- Size: 202 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Measures the Time Elapsed by a Block
[](https://www.rultor.com/p/yegor256/elapsed)
[](https://www.jetbrains.com/ruby/)
[](https://github.com/yegor256/elapsed/actions/workflows/rake.yml)
[](https://www.0pdd.com/p?name=yegor256/elapsed)
[](https://badge.fury.io/rb/elapsed)
[](https://codecov.io/github/yegor256/elapsed?branch=master)
[](https://rubydoc.info/github/yegor256/elapsed/master/frames)
[](https://hitsofcode.com/view/github/yegor256/elapsed)
[](https://github.com/yegor256/elapsed/blob/master/LICENSE.txt)
Here is how you can measure and log the time of a block:
```ruby
require 'elapsed'
elapsed do
run_something_slow
end
```
You can also send the log message to a log, for example to the
[Loog](https://github.com/yegor256/loog):
```ruby
require 'elapsed'
require 'loog'
elapsed(Loog::VERBOSE) do
# any code
end
```
You can also make the message custom:
```ruby
elapsed(good: 'File saved') do
File.save(f, 'Hello, world!')
end
```
Or, you can make the message even more custom:
```ruby
elapsed do
File.save(f, 'Hello, world!')
throw :"Successfully saved #{File.size(f)} bytes"
end
```
You can also filter out small durations using the `over:` parameter:
```ruby
elapsed(Loog::VERBOSE, over: 0.5) do
# This message will only be logged if the block takes more than 0.5 seconds
run_something_that_might_be_fast
end
```
You can change the level of logging
(use `Logger::INFO`, `Logger::DEBUG`, or `Logger::ERROR`), while
the default one is `DEBUG`:
```ruby
elapsed(Loog::VERBOSE, level: Logger::DEBUG) do
# The procedure
end
```
That's it.
## How to contribute
Read
[these guidelines](https://www.yegor256.com/2014/04/15/github-guidelines.html).
Make sure your build is green before you contribute
your pull request. You will need to have
[Ruby](https://www.ruby-lang.org/en/) 3.0+ and
[Bundler](https://bundler.io/) installed. Then:
```bash
bundle update
bundle exec rake
```
If it's clean and you don't see any error messages, submit your pull request.