discourse/lib/discourse_logstash_logger.rb
Sam Saffron 28292d2759
PERF: avoid shelling to get hostname aggressively
Previously we had many places in the app that called `hostname` to get
hostname of a server. This commit replaces the pattern in 2 ways

1. We cache the result in `Discourse.os_hostname` so it is only ever called once

2. We prefer to use Socket.gethostname which avoids making a shell command

This improves performance as we are not spawning hostname processes throughout
the app lifetime
2020-02-18 15:13:19 +11:00

20 lines
489 B
Ruby

# frozen_string_literal: true
require 'logstash-logger'
class DiscourseLogstashLogger
def self.logger(uri:, type:)
LogStashLogger.new(
uri: uri,
sync: true,
customize_event: ->(event) {
event['hostname'] = Discourse.os_hostname
event['severity_name'] = event['severity']
event['severity'] = Object.const_get("Logger::Severity::#{event['severity']}")
event['type'] = type
event['pid'] = Process.pid
},
)
end
end