journalrb 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/COPYING +674 -0
- data/COPYING.LESSER +165 -0
- data/README.md +102 -0
- data/Rakefile +10 -0
- data/lib/journalrb/logger.rb +307 -0
- data/lib/journalrb/version.rb +20 -0
- data/lib/journalrb.rb +23 -0
- data/sig/journalrb/logger.rbs +78 -0
- data/sig/journalrb/version.rbs +3 -0
- data/sig/journalrb.rbs +4 -0
- metadata +55 -0
data/COPYING.LESSER
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
data/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# JournalRB
|
|
2
|
+
|
|
3
|
+
JournalRB is a pure Ruby library for logging to Journald through its Unix Socket.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bundle add journalrb
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
gem install journalrb
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
Basic usage:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
require "journalrb"
|
|
25
|
+
|
|
26
|
+
logger = JournalRB::Logger.new # Defaults to DEBUG level.
|
|
27
|
+
logger.debug("Messages which may need to be enabled first, only useful for debugging")
|
|
28
|
+
logger.info("Normal operational messages that require no action") # or logger.informational("Normal operational messages that require no action")
|
|
29
|
+
logger.notice("Events that are unusual, but not error conditions")
|
|
30
|
+
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")
|
|
31
|
+
logger.err("Error conditions") # or logger.error("Error conditions")
|
|
32
|
+
logger.crit("Critical conditions") # or logger.critical("Critical conditions")
|
|
33
|
+
logger.alert("Should be corrected immediately")
|
|
34
|
+
logger.emerg("System is unusable") # or logger.emergency("System is unusable")
|
|
35
|
+
|
|
36
|
+
# Blocks can also be used to provide the message as long as the value returned is a string.
|
|
37
|
+
logger.info { "This is an informational message provided by a block" }
|
|
38
|
+
|
|
39
|
+
# Setting the log level
|
|
40
|
+
logger = JournalRB::Logger.new(level: :warning)
|
|
41
|
+
# or
|
|
42
|
+
logger.level = :warning # or JournalRB::Logger::WARNING or 4
|
|
43
|
+
# or
|
|
44
|
+
logger.warning!
|
|
45
|
+
|
|
46
|
+
# You can check if the currently set log level allows with a given severity using
|
|
47
|
+
logger.debug?
|
|
48
|
+
logger.info? # or logger.informational?
|
|
49
|
+
logger.notice?
|
|
50
|
+
logger.warn? # or logger.warning?
|
|
51
|
+
logger.err? # or logger.error?
|
|
52
|
+
logger.crit? # or logger.critical?
|
|
53
|
+
logger.alert?
|
|
54
|
+
logger.emerg? # or logger.emergency?
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
JournalRB supports all standard Journald log priorities (emerg, alert, crit, err, warning, notice, info, debug).
|
|
58
|
+
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.
|
|
59
|
+
|
|
60
|
+
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.
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
require "journalrb"
|
|
64
|
+
|
|
65
|
+
logger = JournalRB::Logger.new
|
|
66
|
+
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")
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
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:
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
require "journalrb"
|
|
73
|
+
|
|
74
|
+
logger = JournalRB::Logger.new(socket: "/custom/path/to/journal/socket")
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Development
|
|
78
|
+
|
|
79
|
+
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.
|
|
80
|
+
|
|
81
|
+
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).
|
|
82
|
+
|
|
83
|
+
## Contributing
|
|
84
|
+
|
|
85
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/zaben903/journalrb.
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
Copyright (C) 2026 Zach Bensley
|
|
90
|
+
|
|
91
|
+
This program is free software: you can redistribute it and/or modify
|
|
92
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
93
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
94
|
+
(at your option) any later version.
|
|
95
|
+
|
|
96
|
+
This program is distributed in the hope that it will be useful,
|
|
97
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
98
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
99
|
+
GNU Lesser General Public License for more details.
|
|
100
|
+
|
|
101
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
102
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (C) 2026 Zach Bensley
|
|
4
|
+
#
|
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU Lesser General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
require "socket"
|
|
19
|
+
|
|
20
|
+
module JournalRB
|
|
21
|
+
# Logger for journald events.
|
|
22
|
+
#
|
|
23
|
+
# @see https://systemd.io/JOURNAL_NATIVE_PROTOCOL/
|
|
24
|
+
class Logger
|
|
25
|
+
# System is unusable
|
|
26
|
+
EMERGENCY = 0
|
|
27
|
+
# Should be corrected immediately
|
|
28
|
+
ALERT = 1
|
|
29
|
+
# Critical conditions
|
|
30
|
+
CRITICAL = 2
|
|
31
|
+
# Error conditions
|
|
32
|
+
ERROR = 3
|
|
33
|
+
# May indicate that an error will occur if action is not taken
|
|
34
|
+
WARNING = 4
|
|
35
|
+
# Events that are unusual, but not error conditions
|
|
36
|
+
NOTICE = 5
|
|
37
|
+
# Normal operational messages that require no action
|
|
38
|
+
INFORMATIONAL = 6
|
|
39
|
+
# Messages which may need to be enabled first, only useful for debugging
|
|
40
|
+
DEBUG = 7
|
|
41
|
+
|
|
42
|
+
EMERG = EMERGENCY
|
|
43
|
+
CRIT = CRITICAL
|
|
44
|
+
ERR = ERROR
|
|
45
|
+
WARN = WARNING
|
|
46
|
+
INFO = INFORMATIONAL
|
|
47
|
+
|
|
48
|
+
LEVELS = {
|
|
49
|
+
"emergency" => EMERGENCY,
|
|
50
|
+
"emerg" => EMERGENCY,
|
|
51
|
+
"alert" => ALERT,
|
|
52
|
+
"critical" => CRITICAL,
|
|
53
|
+
"crit" => CRITICAL,
|
|
54
|
+
"error" => ERROR,
|
|
55
|
+
"err" => ERROR,
|
|
56
|
+
"warning" => WARNING,
|
|
57
|
+
"warn" => WARNING,
|
|
58
|
+
"notice" => NOTICE,
|
|
59
|
+
"informational" => INFORMATIONAL,
|
|
60
|
+
"info" => INFORMATIONAL,
|
|
61
|
+
"debug" => DEBUG
|
|
62
|
+
}.freeze
|
|
63
|
+
private_constant :LEVELS
|
|
64
|
+
|
|
65
|
+
# @param socket [String] the path to the journald socket to write log entries to
|
|
66
|
+
# @param level [Integer, String, Symbol] the log level to use for this logger
|
|
67
|
+
def initialize(socket: "/run/systemd/journal/socket", level: DEBUG)
|
|
68
|
+
self.socket = socket
|
|
69
|
+
self.level = level
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Logs a message with the given severity and metadata.
|
|
73
|
+
#
|
|
74
|
+
# @param level [Integer, String, Symbol] A priority value between 0 ("emerg") and 7 ("debug").
|
|
75
|
+
# @param message [String] The human-readable message string for this entry.
|
|
76
|
+
# @param message_id [String] A 128-bit message identifier ID for recognizing certain message types, if this is desirable.
|
|
77
|
+
# This should contain a 128-bit ID formatted as a lower-case hexadecimal string, without any separating dashes or suchlike.
|
|
78
|
+
# This is recommended to be a UUID-compatible ID, but this is not enforced, and formatted differently.
|
|
79
|
+
# @param code_file [String] The code location generating this message, if known.
|
|
80
|
+
# @param code_line [Integer] The code location generating this message, if known.
|
|
81
|
+
# @param code_func [String] The code location generating this message, if known.
|
|
82
|
+
# @param errno [Integer] The low-level Unix error number causing this entry, if any.
|
|
83
|
+
# @param invocation_id [String] A randomized, unique 128-bit ID identifying each runtime cycle of the unit.
|
|
84
|
+
# @param user_invocation_id [String] A randomized, unique 128-bit ID identifying each runtime cycle of the unit.
|
|
85
|
+
# @param documentation [String] A documentation URL with further information about the topic of the log message.
|
|
86
|
+
# @param tid [Integer] The numeric thread ID (TID) the log message originates from.
|
|
87
|
+
# @param kwargs [Hash] additional key/value pairs to include in the log entry, where keys are strings or symbols and values are strings or can be converted to strings.
|
|
88
|
+
#
|
|
89
|
+
# @yieldreturn [String] the human-readable message string for this entry, if the message parameter is not provided.
|
|
90
|
+
#
|
|
91
|
+
# @see https://www.freedesktop.org/software/systemd/man/latest/systemd.journal-fields.html
|
|
92
|
+
def add(level, message = nil, message_id: nil, code_file: nil, code_line: nil, code_func: nil, errno: nil, invocation_id: nil, user_invocation_id: nil, documentation: nil, tid: nil, **kwargs)
|
|
93
|
+
level = normalize_level(level)
|
|
94
|
+
return unless level <= self.level
|
|
95
|
+
|
|
96
|
+
message ||= if block_given?
|
|
97
|
+
yield
|
|
98
|
+
else
|
|
99
|
+
""
|
|
100
|
+
end
|
|
101
|
+
raise(ArgumentError, "message must be a String") unless message.respond_to?(:to_s)
|
|
102
|
+
|
|
103
|
+
payload = {
|
|
104
|
+
MESSAGE: message,
|
|
105
|
+
PRIORITY: level,
|
|
106
|
+
MESSAGE_ID: message_id,
|
|
107
|
+
CODE_FILE: code_file,
|
|
108
|
+
CODE_LINE: code_line,
|
|
109
|
+
CODE_FUNC: code_func,
|
|
110
|
+
ERRNO: errno,
|
|
111
|
+
INVOCATION_ID: invocation_id,
|
|
112
|
+
USER_INVOCATION_ID: user_invocation_id,
|
|
113
|
+
DOCUMENTATION: documentation,
|
|
114
|
+
TID: tid
|
|
115
|
+
}.merge(kwargs.transform_keys { |k| k.to_s.upcase }).compact
|
|
116
|
+
serialized_payload = payload.map { |k, v| serialize_field(k, v.to_s) }.join
|
|
117
|
+
@socket.send(serialized_payload, 0) && true
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# @see #add
|
|
121
|
+
def debug(message = nil, **kwargs, &block)
|
|
122
|
+
add(DEBUG, message, **kwargs, &block)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# @see #add
|
|
126
|
+
def informational(message = nil, **kwargs, &block)
|
|
127
|
+
add(INFORMATIONAL, message, **kwargs, &block)
|
|
128
|
+
end
|
|
129
|
+
alias_method :info, :informational
|
|
130
|
+
|
|
131
|
+
# @see #add
|
|
132
|
+
def notice(message = nil, **kwargs, &block)
|
|
133
|
+
add(NOTICE, message, **kwargs, &block)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# @see #add
|
|
137
|
+
def warning(message = nil, **kwargs, &block)
|
|
138
|
+
add(WARNING, message, **kwargs, &block)
|
|
139
|
+
end
|
|
140
|
+
alias_method :warn, :warning
|
|
141
|
+
|
|
142
|
+
# @see #add
|
|
143
|
+
def error(message = nil, **kwargs, &block)
|
|
144
|
+
add(ERROR, message, **kwargs, &block)
|
|
145
|
+
end
|
|
146
|
+
alias_method :err, :error
|
|
147
|
+
|
|
148
|
+
# @see #add
|
|
149
|
+
def critical(message = nil, **kwargs, &block)
|
|
150
|
+
add(CRITICAL, message, **kwargs, &block)
|
|
151
|
+
end
|
|
152
|
+
alias_method :crit, :critical
|
|
153
|
+
|
|
154
|
+
# @see #add
|
|
155
|
+
def alert(message = nil, **kwargs, &block)
|
|
156
|
+
add(ALERT, message, **kwargs, &block)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# @see #add
|
|
160
|
+
def emergency(message = nil, **kwargs, &block)
|
|
161
|
+
add(EMERGENCY, message, **kwargs, &block)
|
|
162
|
+
end
|
|
163
|
+
alias_method :emerg, :emergency
|
|
164
|
+
|
|
165
|
+
attr_reader :level
|
|
166
|
+
alias_method :priority, :level
|
|
167
|
+
|
|
168
|
+
def level=(level)
|
|
169
|
+
@level = normalize_level(level)
|
|
170
|
+
end
|
|
171
|
+
alias_method :priority=, :level=
|
|
172
|
+
|
|
173
|
+
# @return [Boolean] whether the log level allows entries with severity Logger::DEBUG to be written
|
|
174
|
+
def debug?
|
|
175
|
+
level >= DEBUG
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Sets the log level to Logger::DEBUG.
|
|
179
|
+
def debug!
|
|
180
|
+
self.level = DEBUG
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# @return [Boolean] whether the log level allows entries with severity Logger::INFORMATIONAL to be written
|
|
184
|
+
def informational?
|
|
185
|
+
level >= INFORMATIONAL
|
|
186
|
+
end
|
|
187
|
+
alias_method :info?, :informational?
|
|
188
|
+
|
|
189
|
+
# Sets the log level to Logger::INFORMATIONAL.
|
|
190
|
+
def informational!
|
|
191
|
+
self.level = INFORMATIONAL
|
|
192
|
+
end
|
|
193
|
+
alias_method :info!, :informational!
|
|
194
|
+
|
|
195
|
+
# @return [Boolean] whether the log level allows entries with severity Logger::NOTICE to be written
|
|
196
|
+
def notice?
|
|
197
|
+
level >= NOTICE
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Sets the log level to Logger::NOTICE.
|
|
201
|
+
def notice!
|
|
202
|
+
self.level = NOTICE
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# @return [Boolean] whether the log level allows entries with severity Logger::WARNING to be written
|
|
206
|
+
def warning?
|
|
207
|
+
level >= WARNING
|
|
208
|
+
end
|
|
209
|
+
alias_method :warn?, :warning?
|
|
210
|
+
|
|
211
|
+
# Sets the log level to Logger::WARNING.
|
|
212
|
+
def warning!
|
|
213
|
+
self.level = WARNING
|
|
214
|
+
end
|
|
215
|
+
alias_method :warn!, :warning!
|
|
216
|
+
|
|
217
|
+
# @return [Boolean] whether the log level allows entries with severity Logger::ERROR to be written
|
|
218
|
+
def error?
|
|
219
|
+
level >= ERROR
|
|
220
|
+
end
|
|
221
|
+
alias_method :err?, :error?
|
|
222
|
+
|
|
223
|
+
# Sets the log level to Logger::ERROR.
|
|
224
|
+
def error!
|
|
225
|
+
self.level = ERROR
|
|
226
|
+
end
|
|
227
|
+
alias_method :err!, :error!
|
|
228
|
+
|
|
229
|
+
# @return [Boolean] whether the log level allows entries with severity Logger::CRITICAL to be written
|
|
230
|
+
def critical?
|
|
231
|
+
level >= CRITICAL
|
|
232
|
+
end
|
|
233
|
+
alias_method :crit?, :critical?
|
|
234
|
+
|
|
235
|
+
# Sets the log level to Logger::CRITICAL.
|
|
236
|
+
def critical!
|
|
237
|
+
self.level = CRITICAL
|
|
238
|
+
end
|
|
239
|
+
alias_method :crit!, :critical!
|
|
240
|
+
|
|
241
|
+
# @return [Boolean] whether the log level allows entries with severity Logger::ALERT to be written
|
|
242
|
+
def alert?
|
|
243
|
+
level >= ALERT
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# Sets the log level to Logger::ALERT.
|
|
247
|
+
def alert!
|
|
248
|
+
self.level = ALERT
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
# @return [Boolean] whether the log level allows entries with severity Logger::EMERGENCY to be written
|
|
252
|
+
def emergency?
|
|
253
|
+
level >= EMERGENCY
|
|
254
|
+
end
|
|
255
|
+
alias_method :emerg?, :emergency?
|
|
256
|
+
|
|
257
|
+
# Sets the log level to Logger::EMERGENCY.
|
|
258
|
+
def emergency!
|
|
259
|
+
self.level = EMERGENCY
|
|
260
|
+
end
|
|
261
|
+
alias_method :emerg!, :emergency!
|
|
262
|
+
|
|
263
|
+
private
|
|
264
|
+
|
|
265
|
+
# Normalizes a log level input to an integer value between 0 and 7.
|
|
266
|
+
#
|
|
267
|
+
# @param level [Integer, String, Symbol] the log level to normalize
|
|
268
|
+
#
|
|
269
|
+
# @return [Integer] the normalized log level
|
|
270
|
+
def normalize_level(level)
|
|
271
|
+
if level.is_a?(Integer)
|
|
272
|
+
return level if (EMERGENCY..DEBUG).cover?(level)
|
|
273
|
+
|
|
274
|
+
raise(ArgumentError, "invalid log level: #{level}")
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
key = level.to_s.downcase
|
|
278
|
+
LEVELS[key] || raise(ArgumentError, "invalid log level: #{level}")
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# @param socket [String] the path to the journald socket to write log entries to
|
|
282
|
+
def socket=(socket)
|
|
283
|
+
raise(ArgumentError, "socket does not exist: #{socket}") unless File.exist?(socket)
|
|
284
|
+
raise(ArgumentError, "socket is not a UNIX socket: #{socket}") unless File.socket?(socket)
|
|
285
|
+
|
|
286
|
+
@socket = Socket.new(:UNIX, :DGRAM, 0)
|
|
287
|
+
@socket.connect(Socket.sockaddr_un(socket))
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
# Encodes a single journald field.
|
|
291
|
+
#
|
|
292
|
+
# Each key/value pair is serialized via one of two methods:
|
|
293
|
+
# The first method inserts a `=` character between key and value, and suffixes the result with `\n`.
|
|
294
|
+
# The second method should be used if the value of a field contains a `\n` byte.
|
|
295
|
+
# In this case, the key name is serialized as is, followed by a `\n` character, followed by a (non-aligned) little-endian unsigned 64-bit integer encoding the size of the value, followed by the literal value data, followed by `\n`.
|
|
296
|
+
#
|
|
297
|
+
# @example a key `FOO` with a value `BAR` is serialized `F, O, O, =, B, A, R, \n`
|
|
298
|
+
# @example a key `FOO` with a value `BAR` may be serialized using this second method as: `F, O, O, \n, \003, \000, \000, \000, \000, \000, \000, \000, B, A, R, \n`.
|
|
299
|
+
def serialize_field(key, value)
|
|
300
|
+
if value.include?("\n")
|
|
301
|
+
"#{key}\n#{[value.bytesize].pack("Q<")}#{value}\n"
|
|
302
|
+
else
|
|
303
|
+
"#{key}=#{value}\n"
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
end
|
|
307
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (C) 2026 Zach Bensley
|
|
4
|
+
#
|
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU Lesser General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
module JournalRB
|
|
19
|
+
VERSION = "0.1.0"
|
|
20
|
+
end
|
data/lib/journalrb.rb
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (C) 2026 Zach Bensley
|
|
4
|
+
#
|
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU Lesser General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
require_relative "journalrb/version"
|
|
19
|
+
require_relative "journalrb/logger"
|
|
20
|
+
|
|
21
|
+
module JournalRB
|
|
22
|
+
class Error < StandardError; end
|
|
23
|
+
end
|