An open API service indexing awesome lists of open source software.

https://github.com/zaben903/journalrb

JournalRB is a pure Ruby library for logging to Journald through its Unix Socket.
https://github.com/zaben903/journalrb

ruby

Last synced: 3 months ago
JSON representation

JournalRB is a pure Ruby library for logging to Journald through its Unix Socket.

Awesome Lists containing this project

README

          

# JournalRB

[![Gem Version](https://badge.fury.io/rb/journalrb.svg)](https://badge.fury.io/rb/journalrb)
[![CI Workflow](https://github.com/zaben903/journalrb/actions/workflows/main.yml/badge.svg)](https://github.com/zaben903/journalrb/actions/workflows/main.yml)
[![CodeQL](https://github.com/zaben903/journalrb/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/zaben903/journalrb/actions/workflows/github-code-scanning/codeql)

JournalRB is a pure Ruby library for logging to Journald through its Unix Socket.

## Installation

Install the gem and add to the application's Gemfile by executing:

```bash
bundle add journalrb
```

If bundler is not being used to manage dependencies, install the gem by executing:

```bash
gem install journalrb
```

## Usage

Basic usage:

```ruby
require "journalrb"

logger = JournalRB::Logger.new # Defaults to DEBUG level.
logger.debug("Messages which may need to be enabled first, only useful for debugging")
logger.info("Normal operational messages that require no action") # or logger.informational("Normal operational messages that require no action")
logger.notice("Events that are unusual, but not error conditions")
logger.warn("May indicate that an error will occur if action is not taken") # or logger.warning("May indicate that an error will occur if action is not taken")
logger.err("Error conditions") # or logger.error("Error conditions")
logger.crit("Critical conditions") # or logger.critical("Critical conditions")
logger.alert("Should be corrected immediately")
logger.emerg("System is unusable") # or logger.emergency("System is unusable")

# Blocks can also be used to provide the message as long as the value returned is a string.
logger.info { "This is an informational message provided by a block" }

# Setting the log level
logger = JournalRB::Logger.new(level: :warning)
# or
logger.level = :warning # or JournalRB::Logger::WARNING or 4
# or
logger.warning!

# You can check if the currently set log level allows with a given severity using
logger.debug?
logger.info? # or logger.informational?
logger.notice?
logger.warn? # or logger.warning?
logger.err? # or logger.error?
logger.crit? # or logger.critical?
logger.alert?
logger.emerg? # or logger.emergency?
```

JournalRB supports all standard Journald log priorities (emerg, alert, crit, err, warning, notice, info, debug).
Additional journal fields can be included in log entries by passing keyword arguments to the logging methods, and the message string can be provided either as a parameter or as a block.

Example additional fields can be found at [systemd.journal-fields](https://www.freedesktop.org/software/systemd/man/latest/systemd.journal-fields.html) or as described in the `#add` method.

```ruby
require "journalrb"

logger = JournalRB::Logger.new
logger.error("An error occurred", errno: 123, code_file: "app.rb", code_line: 42, code_func: "perform_task", documentation: "https://example.com/docs/errors#123", custom_field: "custom_value")
```

If Journald is not configured to use the default socket path (`/run/systemd/journal/socket`), you can specify a custom socket path when initializing the logger:

```ruby
require "journalrb"

logger = JournalRB::Logger.new(socket: "/custom/path/to/journal/socket")
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/zaben903/journalrb.

## License

Copyright (C) 2026 Zach Bensley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see .