Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to prefix log lines with a.. prefix. #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/logstasher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class << self
attr_writer :serialize_parameters
attr_writer :silence_standard_logging
attr_accessor :metadata
attr_accessor :prefix

def append_fields(&block)
@append_fields_callback = block
Expand Down Expand Up @@ -57,7 +58,7 @@ def log_as_json(payload, as_logstash_event: false)
payload.to_json
end

logger << json_payload + $INPUT_RECORD_SEPARATOR
logger << (prefix.nil? ? "" : prefix) + json_payload + $INPUT_RECORD_SEPARATOR
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefix.to_s will work here since nil.to_s #=> "".

Copy link

@film42 film42 Nov 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might actually be a bit problematic since the expectation is that the output is a pure json blob without any formatting or parsing required.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea we were discussing was that prefixing the blob with a prefix would allow us to positively identify logstasher output in a stdout stream without having to fully parse the json

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this break logstash filters that try to parse a json payload?

    filter {
      json {
        source => "message"
      }
    }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if someone defines a prefix then the logstash pipeline will extract the json string after the prefix to a field and then json parse on that new field.

end

def logger
Expand Down
2 changes: 2 additions & 0 deletions lib/logstasher/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Railtie < ::Rails::Railtie
config.logstasher.log_level = ::Logger::INFO

config.logstasher.metadata = {}
config.logstasher.prefix = ""
config.before_initialize do
options = config.logstasher

Expand All @@ -20,6 +21,7 @@ class Railtie < ::Rails::Railtie
::LogStasher.logger = options.logger || default_logger
::LogStasher.logger.level = options.log_level
::LogStasher.metadata = options.metadata
::LogStasher.prefix = options.prefix
end

initializer 'logstasher.load' do
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/logstasher/logstasher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
end
end

context "with prefix" do
before { ::LogStasher.prefix = "LOGSTASHER " }
after { ::LogStasher.prefix = nil }

it "appends a prefix to log lines." do
expect(::LogStasher.logger).to receive(:<<) do |line|
expect(line).to match(/^LOGSTASHER /)
end

::LogStasher.log_as_json(:yolo => :brolo)
end
end

context "with metadata" do
before { ::LogStasher.metadata = { :namespace => :cooldude } }
after { ::LogStasher.metadata = {} }
Expand Down