Skip to content

Commit

Permalink
Add the ability to use logfmt logging format.
Browse files Browse the repository at this point in the history
  • Loading branch information
treacher authored and PatrickTulskie committed Aug 1, 2023
1 parent 05a3a9f commit ffa02ec
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ Resque Scheduler authors
- sawanoboly
- serek
- iloveitaly
- treacher
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ requiring `resque` and `resque/scheduler` (default empty).
* `RESQUE_SCHEDULER_INTERVAL` - Interval in seconds for checking if a
scheduled job must run (coerced with `Kernel#Float()`) (default `5`)
* `LOGFILE` - Log file name (default empty, meaning `$stdout`)
* `LOGFORMAT` - Log output format to use (either `'text'` or `'json'`,
* `LOGFORMAT` - Log output format to use (either `'text'`, `'json'` or `'logfmt'`,
default `'text'`)
* `PIDFILE` - If non-empty, write process PID to file (default empty)
* `QUIET` - Silence most output if non-empty (equivalent to a level of
Expand Down Expand Up @@ -663,7 +663,7 @@ are toggled by environment variables:
- `QUIET` will stop logging anything. Completely silent.
- `VERBOSE` opposite of 'QUIET'; will log even debug information
- `LOGFILE` specifies the file to write logs to. (default standard output)
- `LOGFORMAT` specifies either "text" or "json" output format
- `LOGFORMAT` specifies either "text", "json" or "logfmt" output format
(default "text")

All of these variables are optional and will be given the following default
Expand Down
2 changes: 1 addition & 1 deletion lib/resque/scheduler/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def logfile
@logfile ||= environment['LOGFILE']
end

# Sets whether to log in 'text' or 'json'
# Sets whether to log in 'text', 'json' or 'logfmt'
attr_writer :logformat

def logformat
Expand Down
11 changes: 10 additions & 1 deletion lib/resque/scheduler/logger_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class LoggerBuilder
# - :quiet if logger needs to be silent for all levels. Default - false
# - :verbose if there is a need in debug messages. Default - false
# - :log_dev to output logs into a desired file. Default - STDOUT
# - :format log format, either 'text' or 'json'. Default - 'text'
# - :format log format, either 'text', 'json' or 'logfmt'. Default - 'text'
#
# Example:
#
Expand Down Expand Up @@ -67,6 +67,15 @@ def json_formatter
) + "\n"
end
end

def logfmt_formatter
proc do |severity, datetime, _progname, msg|
"Timestamp=\"#{datetime.iso8601}\" " \
"SeverityText=\"#{severity}\" " \
'InstrumentationScope="resque-scheduler" ' \
"Body=\"#{msg}\"\n"
end
end
end
end
end
23 changes: 23 additions & 0 deletions test/scheduler_setup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,27 @@ def wipe
assert $stdout.string =~ /: another thing/
end
end

context 'logger with logfmt formatter' do
setup do
nullify_logger
Resque::Scheduler.logformat = 'logfmt'
$stdout = StringIO.new
end

teardown do
$stdout = STDOUT
end

test 'logs with logfmt' do
Timecop.freeze do
Resque::Scheduler.log! 'another thing'

expected_output = "Timestamp=\"#{DateTime.now.iso8601}\" SeverityText=\"INFO\" " \
"InstrumentationScope=\"resque-scheduler\" Body=\"another thing\"\n"

assert_equal $stdout.string, expected_output
end
end
end
end

0 comments on commit ffa02ec

Please sign in to comment.