Class: AuditLoggable::Logger::JSONFormatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/audit_loggable/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timezone:) ⇒ JSONFormatter

Returns a new instance of JSONFormatter.

Raises:

  • (ArgumentError)


10
11
12
13
14
# File 'lib/audit_loggable/logger.rb', line 10

def initialize(timezone:)
  raise ArgumentError unless %i[utc local].include? timezone

  @timezone = timezone
end

Instance Attribute Details

#timezoneObject (readonly)

Returns the value of attribute timezone.



8
9
10
# File 'lib/audit_loggable/logger.rb', line 8

def timezone
  @timezone
end

Instance Method Details

#call(_severity, time, _progname, message) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/audit_loggable/logger.rb', line 16

def call(_severity, time, _progname, message)
  timestamp =
    case timezone
    when :utc then time.getutc
    when :local then time.getlocal
    end

  h = { timestamp: timestamp, record: message }
  "#{h.to_json}\n"
end