Module: Skylight::Core::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.



104
105
106
107
108
109
110
# File 'lib/skylight/core/util/logging.rb', line 104

def config_for_logging
  if respond_to?(:config)
    config
  elsif is_a?(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`



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

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`



87
88
89
90
# File 'lib/skylight/core/util/logging.rb', line 87

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:

  • (String)


100
101
102
# File 'lib/skylight/core/util/logging.rb', line 100

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`



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

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`



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/skylight/core/util/logging.rb', line 115

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, format(msg, *args)
      else
        logger.send level, msg
      end
      return
    else
      Kernel.warn "Invalid logger"
    end
  end

  # Fallback
  if (module_name = is_a?(Module) ? name : self.class.name)
    root_name = module_name.split("::").first.upcase
    msg.prepend("[#{root_name}] ")
  end
  puts format(msg, *args)
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.



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

def log_context
  {}
end

#log_env_prefixObject

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.



30
31
32
33
34
35
36
# File 'lib/skylight/core/util/logging.rb', line 30

def log_env_prefix
  if (c = config_for_logging)
    c.class.env_prefix
  else
    "SKYLIGHT_"
  end
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)


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

def raise_on_error?
  !!ENV["#{log_env_prefix}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:



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

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`



51
52
53
54
# File 'lib/skylight/core/util/logging.rb', line 51

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)


38
39
40
# File 'lib/skylight/core/util/logging.rb', line 38

def trace?
  !!ENV["#{log_env_prefix}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`



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

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