Class: Logtail::LogEntry
- Inherits:
-
Object
- Object
- Logtail::LogEntry
- Defined in:
- lib/logtail/log_entry.rb
Overview
Represents a new log entry into the log. This is an intermediary class between ‘Logger` and the log device that you set it up with.
Constant Summary collapse
- BINARY_LIMIT_THRESHOLD =
:nodoc:
1_000.freeze
- DT_PRECISION =
6.freeze
- MESSAGE_MAX_BYTES =
8192.freeze
- LOGGER_FILE =
'/logtail/logger.rb'.freeze
Instance Attribute Summary collapse
-
#context_snapshot ⇒ Object
readonly
Returns the value of attribute context_snapshot.
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#progname ⇒ Object
readonly
Returns the value of attribute progname.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Instance Method Summary collapse
-
#initialize(level, time, progname, message, context_snapshot, event, options = {}) ⇒ LogEntry
constructor
Creates a log entry suitable to be sent to the Logtail API.
- #inspect ⇒ Object
-
#to_hash(options = {}) ⇒ Object
Builds a hash representation containing simple objects, suitable for serialization (JSON).
- #to_json(options = {}) ⇒ Object
- #to_msgpack(*args) ⇒ Object
-
#to_s ⇒ Object
This is used when LogEntry objects make it to a non-Logtail logger.
Constructor Details
#initialize(level, time, progname, message, context_snapshot, event, options = {}) ⇒ LogEntry
Creates a log entry suitable to be sent to the Logtail API.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/logtail/log_entry.rb', line 29 def initialize(level, time, progname, , context_snapshot, event, = {}) @level = level @time = time.utc @progname = progname # If the message is not a string we call inspect to ensure it is a string. # This follows the default behavior set by ::Logger # See: https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L615 @message = .is_a?(String) ? : .inspect @message = @message.byteslice(0, MESSAGE_MAX_BYTES) @tags = [:tags] @context_snapshot = context_snapshot @event = event @runtime_context = current_runtime_context || {} end |
Instance Attribute Details
#context_snapshot ⇒ Object (readonly)
Returns the value of attribute context_snapshot.
17 18 19 |
# File 'lib/logtail/log_entry.rb', line 17 def context_snapshot @context_snapshot end |
#event ⇒ Object (readonly)
Returns the value of attribute event.
17 18 19 |
# File 'lib/logtail/log_entry.rb', line 17 def event @event end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
17 18 19 |
# File 'lib/logtail/log_entry.rb', line 17 def level @level end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
17 18 19 |
# File 'lib/logtail/log_entry.rb', line 17 def @message end |
#progname ⇒ Object (readonly)
Returns the value of attribute progname.
17 18 19 |
# File 'lib/logtail/log_entry.rb', line 17 def progname @progname end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
17 18 19 |
# File 'lib/logtail/log_entry.rb', line 17 def @tags end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
17 18 19 |
# File 'lib/logtail/log_entry.rb', line 17 def time @time end |
Instance Method Details
#inspect ⇒ Object
83 84 85 |
# File 'lib/logtail/log_entry.rb', line 83 def inspect to_s end |
#to_hash(options = {}) ⇒ Object
Builds a hash representation containing simple objects, suitable for serialization (JSON).
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/logtail/log_entry.rb', line 46 def to_hash( = {}) ||= {} hash = { :level => level, :dt => formatted_dt, :message => , } if !.nil? && .length > 0 hash[:tags] = end if !event.nil? hash.merge!(event) end if !context_snapshot.nil? && context_snapshot.length > 0 hash[:context] = context_snapshot end hash[:context] ||= {} hash[:context][:runtime] ||= {} hash[:context][:runtime].merge!(@runtime_context) if [:only] hash.select do |key, _value| [:only].include?(key) end elsif [:except] hash.select do |key, _value| ![:except].include?(key) end else hash end end |
#to_json(options = {}) ⇒ Object
87 88 89 |
# File 'lib/logtail/log_entry.rb', line 87 def to_json( = {}) to_hash.to_json end |
#to_msgpack(*args) ⇒ Object
91 92 93 |
# File 'lib/logtail/log_entry.rb', line 91 def to_msgpack(*args) to_hash.to_msgpack(*args) end |
#to_s ⇒ Object
This is used when LogEntry objects make it to a non-Logtail logger.
96 97 98 |
# File 'lib/logtail/log_entry.rb', line 96 def to_s + "\n" end |