Module: Skylight::Util::Logging Private

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#config_for_loggingObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



97
98
99
100
101
102
103
# File 'lib/skylight/util/logging.rb', line 97

def config_for_logging
  if respond_to?(:config)
    config
  elsif is_a?(Skylight::Config)
    self
  end
end

#debug(msg, *args) ⇒ Object Also known as: log_debug

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • msg (String)

    the message to log

  • args (Array)

    values for ‘Kernel#sprintf` on `msg`



62
63
64
# File 'lib/skylight/util/logging.rb', line 62

def debug(msg, *args)
  log :debug, msg, *args
end

#error(msg, *args) ⇒ Object Also known as: log_error

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • msg (String)

    the message to log

  • args (Array)

    values for ‘Kernel#sprintf` on `msg`



80
81
82
83
# File 'lib/skylight/util/logging.rb', line 80

def error(msg, *args)
  log :error, msg, *args
  raise format(msg, *args) if raise_on_error?
end

#fmt(*args) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Alias for ‘Kernel#sprintf`

Returns:



93
94
95
# File 'lib/skylight/util/logging.rb', line 93

def fmt(*args)
  sprintf(*args)
end

#info(msg, *args) ⇒ Object Also known as: log_info

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • msg (String)

    the message to log

  • args (Array)

    values for ‘Kernel#sprintf` on `msg`



68
69
70
# File 'lib/skylight/util/logging.rb', line 68

def info(msg, *args)
  log :info, msg, *args
end

#log(level, msg, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • level (String, Symbol)

    the method on ‘logger` to use for logging

  • msg (String)

    the message to log

  • args (Array)

    values for ‘Kernel#sprintf` on `msg`



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/skylight/util/logging.rb', line 108

def log(level, msg, *args)
  c = config_for_logging
  logger = c ? c.logger : nil

  msg = log_context.map { |(k, v)| "#{k}=#{v}; " }.join << msg

  if logger
    if logger.respond_to?(level)
      if args.empty?
        logger.send level, msg
      else
        logger.send level, format(msg, *args)
      end
      return # rubocop:disable Style/RedundantReturn
    else
      Kernel.warn "Invalid logger"
    end
    # Fallback to stderr for warn and error levels
  elsif %i[warn error].include?(level)
    $stderr.puts format("[SKYLIGHT] #{msg}", *args)
  end
rescue Exception => e
  if trace?
    puts "[ERROR] #{e.message}"
    puts e.backtrace
  end
end

#log_contextObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
# File 'lib/skylight/util/logging.rb', line 25

def log_context
  {}
end

#raise_on_error?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


33
34
35
# File 'lib/skylight/util/logging.rb', line 33

def raise_on_error?
  !!ENV[-"SKYLIGHT_RAISE_ON_ERROR"]
end

#t { ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Evaluates and logs the result of the block if tracing

See #trace?.

Yields:

  • block to be evaluted

Yield Returns:



54
55
56
57
58
# File 'lib/skylight/util/logging.rb', line 54

def t
  return unless trace?

  log :debug, yield
end

#trace(msg, *args) ⇒ Object Also known as: log_trace

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Logs if tracing

See #trace?.

Parameters:

  • msg (String)

    the message to log

  • args (Array)

    values for ‘Kernel#sprintf` on `msg`



42
43
44
45
46
# File 'lib/skylight/util/logging.rb', line 42

def trace(msg, *args)
  return unless trace?

  log :debug, msg, *args
end

#trace?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


29
30
31
# File 'lib/skylight/util/logging.rb', line 29

def trace?
  !!ENV[-"SKYLIGHT_ENABLE_TRACE_LOGS"]
end

#warn(msg, *args) ⇒ Object Also known as: log_warn

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • msg (String)

    the message to log

  • args (Array)

    values for ‘Kernel#sprintf` on `msg`



74
75
76
# File 'lib/skylight/util/logging.rb', line 74

def warn(msg, *args)
  log :warn, msg, *args
end