lib/timber/contexts/runtime.rb in timber-2.5.1 vs lib/timber/contexts/runtime.rb in timber-2.6.0.pre.beta1

- old
+ new

@@ -5,27 +5,46 @@ # The runtime context adds current runtime data to your logs, such as the file, line number, # class or module name, etc. This makes it easy to tail and search your logs by their # origin in your code. For example, if you are debugging a specific class, you can narrow # by that class and see only it's logs. class Runtime < Context + APPLICATION_MAX_BYTES = 256.freeze + CLASS_NAME_MAX_BYTES = 256.freeze + FILE_MAX_BYTES = 1024.freeze + FUNCTION_MAX_BYTES = 256.freeze + MODULE_NAME_MAX_BYTES = 256.freeze + VM_PID_MAX_BYTES = 256.freeze + @keyspace = :runtime attr_reader :application, :class_name, :file, :function, :line, :module_name, :vm_pid def initialize(attributes) - @application = attributes[:application] - @class_name = attributes[:class_name] - @file = attributes[:file] - @function = attributes[:function] - @line = attributes[:line] - @module_name = attributes[:module_name] - @vm_pid = Timber::Util::Object.try(attributes[:vm_pid], :to_s) + normalizer = Util::AttributeNormalizer.new(attributes) + @application = normalizer.fetch(:application, :string, :limit => APPLICATION_MAX_BYTES) + @class_name = normalizer.fetch(:class_name, :string, :limit => CLASS_NAME_MAX_BYTES) + @file = normalizer.fetch(:file, :string, :limit => FILE_MAX_BYTES) + @function = normalizer.fetch(:function, :string, :limit => FUNCTION_MAX_BYTES) + @line = normalizer.fetch(:line, :integer) + @module_name = normalizer.fetch(:module_name, :string, :limit => MODULE_NAME_MAX_BYTES) + @vm_pid = normalizer.fetch(:vm_pid, :string, :limit => VM_PID_MAX_BYTES) end # Builds a hash representation containing simple objects, suitable for serialization (JSON). + def to_hash + @to_hash ||= Util::NonNilHashBuilder.build do |h| + h.add(:application, application) + h.add(:class_name, class_name) + h.add(:file, file) + h.add(:function, function) + h.add(:line, line) + h.add(:module_name, module_name) + h.add(:vm_pid, vm_pid) + end + end + def as_json(_options = {}) - {application: application, class_name: class_name, file: file, function: function, - line: line, module_name: module_name, vm_pid: vm_pid} + to_hash end end end end \ No newline at end of file