Module: Scrolls::Log

Extended by:
Log, Parser, Utils
Included in:
Log
Defined in:
lib/scrolls/log.rb

Constant Summary collapse

LOG_LEVEL =
(ENV['LOG_LEVEL'] || 6).to_i
LOG_LEVEL_MAP =
{
  "emergency" => 0,
  "alert"     => 1,
  "critical"  => 2,
  "error"     => 3,
  "warning"   => 4,
  "notice"    => 5,
  "info"      => 6,
  "debug"     => 7
}

Instance Method Summary collapse

Methods included from Parser

parse, unparse

Methods included from Utils

hashified_list, hashify

Instance Method Details

#add_global_context(new_data) ⇒ Object



43
44
45
46
# File 'lib/scrolls/log.rb', line 43

def add_global_context(new_data)
  default_global_context unless @global_context
  @global_context.update { |previous_data| previous_data.merge(new_data) }
end

#add_timestampObject



84
85
86
# File 'lib/scrolls/log.rb', line 84

def add_timestamp
  @add_timestamp || false
end

#add_timestamp=(b) ⇒ Object



80
81
82
# File 'lib/scrolls/log.rb', line 80

def add_timestamp=(b)
  @add_timestamp = !!b
end

#contextObject



27
28
29
# File 'lib/scrolls/log.rb', line 27

def context
  Thread.current[:scrolls_context] ||= {}
end

#context=(h) ⇒ Object



31
32
33
# File 'lib/scrolls/log.rb', line 31

def context=(h)
  Thread.current[:scrolls_context] = h
end

#facilityObject



55
56
57
# File 'lib/scrolls/log.rb', line 55

def facility
  @facility ||= default_log_facility
end

#facility=(f) ⇒ Object



48
49
50
51
52
53
# File 'lib/scrolls/log.rb', line 48

def facility=(f)
  @facility = LOG_FACILITY_MAP[f] if f
  if Scrolls::SyslogLogger.opened?
    Scrolls::SyslogLogger.new(progname, facility)
  end
end

#global_contextObject



35
36
37
# File 'lib/scrolls/log.rb', line 35

def global_context
  get_global_context
end

#global_context=(data) ⇒ Object



39
40
41
# File 'lib/scrolls/log.rb', line 39

def global_context=(data)
  set_global_context(data)
end

#log(data, &blk) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
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
135
136
# File 'lib/scrolls/log.rb', line 96

def log(data, &blk)
  # If we get a string lets bring it into our structure.
  if data.kind_of? String
    rawhash = { "log_message" => data }
  else
    rawhash = data
  end

  if gc = get_global_context
    ctx = gc.merge(context)
    logdata = ctx.merge(rawhash)
  end

  # By merging the logdata into the timestamp, rather than vice-versa, we
  # ensure that the timestamp comes first in the Hash, and is placed first
  # on the output, which helps with readability.
  logdata = { :now => Time.now.utc }.merge(logdata) if add_timestamp

  unless blk
    write(logdata)
  else
    start = Time.now
    res = nil
    log(logdata.merge(:at => "start"))
    begin
      res = yield
    rescue StandardError => e
      log(logdata.merge(
        :at           => "exception",
        :reraise      => true,
        :class        => e.class,
        :message      => e.message,
        :exception_id => e.object_id.abs,
        :elapsed      => calc_time(start, Time.now)
      ))
      raise e
    end
    log(logdata.merge(:at => "finish", :elapsed => calc_time(start, Time.now)))
    res
  end
end

#log_exception(data, e) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/scrolls/log.rb', line 138

def log_exception(data, e)
  sync_stream(STDERR) unless @defined

  # If we get a string lets bring it into our structure.
  if data.kind_of? String
    rawhash = { "log_message" => data }
  else
    rawhash = data
  end

  if gc = get_global_context
    logdata = gc.merge(rawhash)
  end

  excepdata = {
    :at           => "exception",
    :class        => e.class,
    :message      => e.message,
    :exception_id => e.object_id.abs
  }

  if e.backtrace
    if single_line_exceptions?
      btlines = []
      e.backtrace.each do |line|
        btlines << line.gsub(/[`'"]/, "")
      end

      if btlines.length > 0
        squish = { :site => btlines.join('\n') }
        log(logdata.merge(excepdata.merge(squish)))
      end
    else
      log(logdata.merge(excepdata))

      e.backtrace.each do |line|
        log(logdata.merge(excepdata).merge(
            :at           => "exception",
            :class        => e.class,
            :exception_id => e.object_id.abs,
            :site         => line.gsub(/[`'"]/, "")
        ))
      end
    end
  end
end

#single_line_exceptions=(b) ⇒ Object



88
89
90
# File 'lib/scrolls/log.rb', line 88

def single_line_exceptions=(b)
  @single_line_exceptions = !!b
end

#single_line_exceptions?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/scrolls/log.rb', line 92

def single_line_exceptions?
  @single_line_exceptions || false
end

#streamObject



68
69
70
# File 'lib/scrolls/log.rb', line 68

def stream
  @stream ||= sync_stream
end

#stream=(out = nil) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/scrolls/log.rb', line 59

def stream=(out=nil)
  @defined = out.nil? ? false : true
  if out == 'syslog'
    @stream = Scrolls::SyslogLogger.new(progname, facility)
  else
    @stream = sync_stream(out)
  end
end

#time_unitObject



76
77
78
# File 'lib/scrolls/log.rb', line 76

def time_unit
  @tunit ||= default_time_unit
end

#time_unit=(u) ⇒ Object



72
73
74
# File 'lib/scrolls/log.rb', line 72

def time_unit=(u)
  set_time_unit(u)
end

#with_context(prefix) ⇒ Object



185
186
187
188
189
190
191
192
193
# File 'lib/scrolls/log.rb', line 185

def with_context(prefix)
  return unless block_given?
  old = context
  self.context = old.merge(prefix)
  res = yield if block_given?
ensure
  self.context = old
  res
end