lib/logasm/adapters/stdout_json_adapter.rb in logasm-jruby-1.2.0 vs lib/logasm/adapters/stdout_json_adapter.rb in logasm-jruby-1.2.1
- old
+ new
@@ -3,16 +3,17 @@
class StdoutJsonAdapter
def initialize(level, service_name, *)
@level = level
@service_name = service_name
@application_name = Utils.application_name(service_name)
+ @mutex = Mutex.new if RUBY_ENGINE == "jruby"
end
def log(level, metadata = {})
if meets_threshold?(level)
message = Utils.build_event(metadata, level, @application_name)
- STDOUT.puts(Utils.generate_json(message))
+ print_line(Utils.generate_json(message))
end
end
def debug?
meets_threshold?(:debug)
@@ -36,9 +37,24 @@
private
def meets_threshold?(level)
LOG_LEVELS.index(level.to_s) >= @level
+ end
+
+ # puts is atomic in MRI starting from 2.5.0
+ if RUBY_ENGINE == "ruby" && RUBY_VERSION >= "2.5.0"
+ def print_line(str)
+ $stdout.puts(str)
+ end
+ elsif RUBY_ENGINE == "jruby"
+ def print_line(str)
+ @mutex.synchronize { $stdout.write(str + "\n") }
+ end
+ else
+ def print_line(str)
+ $stdout.write(str + "\n")
+ end
end
end
end
end