Class: Betterlog::Log

Inherits:
Object
  • Object
show all
Includes:
Tins::SexySingleton
Defined in:
lib/betterlog/log.rb,
lib/betterlog/log/event.rb,
lib/betterlog/log/severity.rb,
lib/betterlog/log/event_formatter.rb

Defined Under Namespace

Classes: Event, EventFormatter, Severity

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.context(data_hash) ⇒ Object



130
131
132
# File 'lib/betterlog/log.rb', line 130

def self.context(data_hash)
  instance.context(data_hash)
end

Instance Method Details

#context(data_hash) ⇒ Object



125
126
127
128
# File 'lib/betterlog/log.rb', line 125

def context(data_hash)
  .add data_hash
  self
end

#debug(object, **rest) ⇒ Log

Logs a message on severity debug.

Parameters:

  • object

    this object is logged

  • **rest

    additional data is logged as well.

Returns:

  • (Log)

    this object itself.



45
46
47
48
49
# File 'lib/betterlog/log.rb', line 45

def debug(object, **rest)
  protect do
    emit Log::Event.ify(object, severity: __method__, rest: rest)
  end
end

#error(object, **rest) ⇒ Log

Logs a message on severity error.

Parameters:

  • object

    this object is logged

  • **rest

    additional data is logged as well.

Returns:

  • (Log)

    this object itself.



56
57
58
59
60
# File 'lib/betterlog/log.rb', line 56

def error(object, **rest)
  protect do
    emit Log::Event.ify(object, severity: __method__, rest: rest)
  end
end

#fatal(object, **rest) ⇒ Log

Logs a message on severity fatal.

Parameters:

  • object

    this object is logged

  • **rest

    additional data is logged as well.

Returns:

  • (Log)

    this object itself.



67
68
69
70
71
# File 'lib/betterlog/log.rb', line 67

def fatal(object, **rest)
  protect do
    emit Log::Event.ify(object, severity: __method__, rest: rest)
  end
end

#info(object, **rest) ⇒ Log

Logs a message on severity info.

Parameters:

  • object

    this object is logged

  • **rest

    additional data is logged as well.

Returns:

  • (Log)

    this object itself.



23
24
25
26
27
# File 'lib/betterlog/log.rb', line 23

def info(object, **rest)
  protect do
    emit Log::Event.ify(object, severity: __method__, rest: rest)
  end
end

#loggerObject



14
15
16
# File 'lib/betterlog/log.rb', line 14

def logger
  defined?(Rails) && Rails.logger || self.class.default_logger
end

#measure(metric:, **rest, &block) ⇒ Log

Logs a time measure on severity debug, by default, this can be changed by passing the severity: keyword.

If an error occurs during measurement details about it are added to the metric event.

Parameters:

  • metric

    the name of the recorded metric.

  • **rest

    additional rest is logged as well.

  • block

    the block around which the measure is teaken.

Returns:

  • (Log)

    this object itself.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/betterlog/log.rb', line 110

def measure(metric:, **rest, &block)
  raise ArgumentError, 'must be called with a block' unless block_given?
  time_block { yield }
rescue => error
  e = Log::Event.ify(error)
  rest |= e.as_hash.subhash(:error_class, :backtrace, :message)
  rest[:message] = "#{rest[:message].inspect} while measuring metric #{metric}"
  raise error
ensure
  protect do
    event = build_metric(metric: metric, type: 'seconds', value: timed_duration, **rest)
    emit event
  end
end

#metric(metric:, type:, value:, **rest) ⇒ Log

Logs a metric on severity debug, by default, this can be changed by passing the severity: keyword.

Parameters:

  • metric

    the name of the recorded metric.

  • type

    of the recorded metric.

  • value

    of the recorded metric.

  • **rest

    additional rest is logged as well.

Returns:

  • (Log)

    this object itself.



93
94
95
96
97
98
# File 'lib/betterlog/log.rb', line 93

def metric(metric:, type:, value:, **rest)
  protect do
    event = build_metric(metric: metric, type: type, value: value, **rest)
    emit event
  end
end

#output(object, **rest) ⇒ Log

Logs a message on severity debug, by default, this can be changed by passing the severity: keyword.

Parameters:

  • object

    this object is logged

  • **rest

    additional data is logged as well.

Returns:

  • (Log)

    this object itself.



79
80
81
82
83
# File 'lib/betterlog/log.rb', line 79

def output(object, **rest)
  protect do
    emit Log::Event.ify(object, severity: rest[:severity], rest: rest)
  end
end

#warn(object, **rest) ⇒ Log

Logs a message on severity warn.

Parameters:

  • object

    this object is logged

  • **rest

    additional data is logged as well.

Returns:

  • (Log)

    this object itself.



34
35
36
37
38
# File 'lib/betterlog/log.rb', line 34

def warn(object, **rest)
  protect do
    emit Log::Event.ify(object, severity: __method__, rest: rest)
  end
end