Module: JsonLogging::Helpers
- Defined in:
- lib/json_logging/helpers.rb
Constant Summary collapse
- ISO8601_MICROSECOND_UTC_PATTERN =
/\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z\z/
Class Method Summary collapse
-
.current_time ⇒ Object
Get current time, using Time.zone if available, otherwise Time.now.
- .current_timestamp ⇒ Object
- .format_timestamp(time) ⇒ Object
- .iso8601_microsecond_utc?(timestamp) ⇒ Boolean
-
.normalize_timestamp(timestamp) ⇒ Object
Normalize timestamp to ISO8601 with microseconds.
-
.safe_string(obj) ⇒ Object
Safely convert object to string, never raises.
Class Method Details
.current_time ⇒ Object
Get current time, using Time.zone if available, otherwise Time.now
27 28 29 30 31 32 33 |
# File 'lib/json_logging/helpers.rb', line 27 def current_time if defined?(Time.zone) && Time.zone Time.zone.now else Time.now end end |
.current_timestamp ⇒ Object
14 15 16 |
# File 'lib/json_logging/helpers.rb', line 14 def (current_time) end |
.format_timestamp(time) ⇒ Object
18 19 20 |
# File 'lib/json_logging/helpers.rb', line 18 def (time) time.utc.iso8601(6) end |
.iso8601_microsecond_utc?(timestamp) ⇒ Boolean
22 23 24 |
# File 'lib/json_logging/helpers.rb', line 22 def iso8601_microsecond_utc?() .is_a?(String) && .match?(ISO8601_MICROSECOND_UTC_PATTERN) end |
.normalize_timestamp(timestamp) ⇒ Object
Normalize timestamp to ISO8601 with microseconds
8 9 10 11 12 |
# File 'lib/json_logging/helpers.rb', line 8 def () return if iso8601_microsecond_utc?() ( || current_time) end |
.safe_string(obj) ⇒ Object
Safely convert object to string, never raises
36 37 38 39 40 |
# File 'lib/json_logging/helpers.rb', line 36 def safe_string(obj) obj.to_s rescue "<unprintable>" end |