Class: MLserver::Logger
- Inherits:
-
Object
- Object
- MLserver::Logger
- Defined in:
- lib/MLserver/logger.rb
Constant Summary collapse
- @@log_levels =
[:info, :warn, :error, :traffic_in, :traffic_out]
Instance Method Summary collapse
- #format_ip_address(address) ⇒ Object
-
#initialize(out:, err:, log_colors: {}, outputs: {error: :err}, levels_with_timestamp: [:traffic_in, :traffic_out]) ⇒ Logger
constructor
A new instance of Logger.
- #log(message, level = :info) ⇒ Object
- #log_traffic(ip, direction, data) ⇒ Object
Constructor Details
#initialize(out:, err:, log_colors: {}, outputs: {error: :err}, levels_with_timestamp: [:traffic_in, :traffic_out]) ⇒ Logger
Returns a new instance of Logger.
5 6 7 8 9 10 11 |
# File 'lib/MLserver/logger.rb', line 5 def initialize(out:, err:, log_colors: {}, outputs: {error: :err}, levels_with_timestamp: [:traffic_in, :traffic_out]) @out = out @err = err @log_colors = log_colors @outputs = outputs @levels_with_timestamp = end |
Instance Method Details
#format_ip_address(address) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/MLserver/logger.rb', line 26 def format_ip_address(address) ipv4_re = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/ ipv4_anywhere_re = /((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}/ ipv4_in_ipv6_re = /^(\:\:ffff\:)((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/ ipv6_re = /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/ return address if address.match?(ipv4_re) if address.match?(ipv4_in_ipv6_re) ipv4_address = address.match(ipv4_anywhere_re).to_s return ipv4_address end if address.match?(ipv6_re) return "[#{address}]" end end |
#log(message, level = :info) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/MLserver/logger.rb', line 13 def log(, level = :info) out = @out out = @err if @outputs[level] == :err if @log_colors[level] = .color @log_colors[level] end = "#{Time.now} | #{}" if @levels_with_timestamp.include?(level) out.puts end |
#log_traffic(ip, direction, data) ⇒ Object
42 43 44 45 46 |
# File 'lib/MLserver/logger.rb', line 42 def log_traffic(ip, direction, data) symbol = (direction == :incoming ? "=>" : "<=") log("#{format_ip_address ip} #{symbol} #{data}", (direction == :incoming ? :traffic_in : :traffic_out)) end |