Module: Scrolls

Extended by:
Scrolls
Included in:
Scrolls
Defined in:
lib/scrolls.rb,
lib/scrolls/utils.rb,
lib/scrolls/logger.rb,
lib/scrolls/parser.rb,
lib/scrolls/version.rb,
lib/scrolls/iologger.rb,
lib/scrolls/sysloglogger.rb

Defined Under Namespace

Modules: Parser, Utils Classes: GlobalContext, IOLogger, LogLevelError, Logger, SyslogLogger, TimeUnitError

Constant Summary collapse

LOG_FACILITY_MAP =

Helpful map of syslog facilities

{
  "auth"     => Syslog::LOG_AUTH,
  "authpriv" => Syslog::LOG_AUTHPRIV,
  "cron"     => Syslog::LOG_CRON,
  "daemon"   => Syslog::LOG_DAEMON,
  "ftp"      => Syslog::LOG_FTP,
  "kern"     => Syslog::LOG_KERN,
  "mail"     => Syslog::LOG_MAIL,
  "news"     => Syslog::LOG_NEWS,
  "syslog"   => Syslog::LOG_SYSLOG,
  "user"     => Syslog::LOG_USER,
  "uucp"     => Syslog::LOG_UUCP,
  "local0"   => Syslog::LOG_LOCAL0,
  "local1"   => Syslog::LOG_LOCAL1,
  "local2"   => Syslog::LOG_LOCAL2,
  "local3"   => Syslog::LOG_LOCAL3,
  "local4"   => Syslog::LOG_LOCAL4,
  "local5"   => Syslog::LOG_LOCAL5,
  "local6"   => Syslog::LOG_LOCAL6,
  "local7"   => Syslog::LOG_LOCAL7,
}
LOG_LEVEL_MAP =

Helpful map of syslog log levels

{
  "emerg"     => 0, # Syslog::LOG_EMERG
  "emergency" => 0, # Syslog::LOG_EMERG
  "alert"     => 1, # Syslog::LOG_ALERT
  "crit"      => 2, # Syslog::LOG_CRIT
  "critical"  => 2, # Syslog::LOG_CRIT
  "error"     => 3, # Syslog::LOG_ERR
  "warn"      => 4, # Syslog::LOG_WARNING
  "warning"   => 4, # Syslog::LOG_WARNING
  "notice"    => 5, # Syslog::LOG_NOTICE
  "info"      => 6, # Syslog::LOG_INFO
  "debug"     => 7  # Syslog::LOG_DEBUG
}
ESCAPE_CHAR =
{
  "&" => "&",
  "<" => "&lt;",
  ">" => "&gt;",
  "'" => "&#x27;",
  '"' => "&quot;",
  "/" => "&#x2F;"
}
ESCAPE_CHAR_PATTERN =
Regexp.union(*ESCAPE_CHAR.keys)
LOG_FACILITY =

Default log facility

ENV['LOG_FACILITY'] || Syslog::LOG_USER
LOG_LEVEL =

Default log level

(ENV['LOG_LEVEL'] || 6).to_i
SYSLOG_OPTIONS =

Default syslog options

Syslog::LOG_PID|Syslog::LOG_CONS
VERSION =
"0.9.4"

Instance Method Summary collapse

Instance Method Details

#add_timestampObject

Public: Return whether the timestamp field will be included in the log output.

Examples

Scrolls.add_timestamp
=> true


187
188
189
# File 'lib/scrolls.rb', line 187

def add_timestamp
  @log.timestamp
end

#add_timestamp=(boolean) ⇒ Object

Public: Set whether to include a timestamp (now=<ISO8601>) field in the log output (default: false)

Examples

Scrolls.add_timestamp = true


175
176
177
# File 'lib/scrolls.rb', line 175

def add_timestamp=(boolean)
  @log.timestamp = boolean
end

#coalesce_strings_to_hash(string_or_something_else) ⇒ Object



331
332
333
334
# File 'lib/scrolls.rb', line 331

def coalesce_strings_to_hash(string_or_something_else)
  return string_or_something_else unless string_or_something_else.is_a?(String)
  { "log_message" => string_or_something_else }
end

#context(data, &blk) ⇒ Object

Public: Set a context in a block for logs

data - A hash of key/values to prepend to each log in a block blk - The block that our context wraps

Examples:



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

def context(data, &blk)
  @log.with_context(data, &blk)
end

#debug(data, &blk) ⇒ Object

Public: Convience method for Logger replacement

data - A hash of key/values to log blk - A block to be wrapped by log lines

Examples:

Scrolls.debug(test: "test")
test=test level=debug
=> nil


224
225
226
227
228
# File 'lib/scrolls.rb', line 224

def debug(data, &blk)
  data = coalesce_strings_to_hash(data)
  data = data.merge(:level => "debug") if data.is_a?(Hash)
  @log.log(data, &blk)
end

#error(data, &blk) ⇒ Object

Public: Convience method for Logger replacement

Translates the level to Syslog equivalent

data - A hash of key/values to log blk - A block to be wrapped by log lines

Examples:

Scrolls.error(test: "test")
test=test level=warning
=> nil


243
244
245
246
247
# File 'lib/scrolls.rb', line 243

def error(data, &blk)
  data = coalesce_strings_to_hash(data)
  data = data.merge(:level => "warning") if data.is_a?(Hash)
  @log.log(data, &blk)
end

#facilityObject

Public: Return the Syslog facility

Examples

Scrolls.facility
=> 8


114
115
116
# File 'lib/scrolls.rb', line 114

def facility
  @log.facility
end

#facility=(f) ⇒ Object

Public: Setup a logging facility (default: Syslog::LOG_USER)

facility - Syslog facility

Examples

Scrolls.facility = Syslog::LOG_LOCAL7


103
104
105
# File 'lib/scrolls.rb', line 103

def facility=(f)
  @log.facility=(f)
end

#fatal(data, &blk) ⇒ Object

Public: Convience method for Logger replacement

Translates the level to Syslog equivalent

data - A hash of key/values to log blk - A block to be wrapped by log lines

Examples:

Scrolls.fatal(test: "test")
test=test level=error
=> nil


262
263
264
265
266
# File 'lib/scrolls.rb', line 262

def fatal(data, &blk)
  data = coalesce_strings_to_hash(data)
  data = data.merge(:level => "error") if data.is_a?(Hash)
  @log.log(data, &blk)
end

#global_contextObject

Public: Get the global context that prefixs all logs



45
46
47
# File 'lib/scrolls.rb', line 45

def global_context
  @log.global_context
end

#info(data, &blk) ⇒ Object

Public: Convience method for Logger replacement

Translates the level to Syslog equivalent

data - A hash of key/values to log blk - A block to be wrapped by log lines

Examples:

Scrolls.info(test: "test")
test=test level=info
=> nil


281
282
283
284
285
# File 'lib/scrolls.rb', line 281

def info(data, &blk)
  data = coalesce_strings_to_hash(data)
  data = data.merge(:level => "info") if data.is_a?(Hash)
  @log.log(data, &blk)
end

#init(options = {}) ⇒ Object

Public: Initialize a Scrolls logger

options - A hash of key/values for configuring Scrolls

stream         - Stream to output data (default: STDOUT)
log_facility   - Syslog facility (default: Syslog::LOG_USER)
time_unit      - Unit of time (default: seconds)
timestamp      - Prepend logs with a timestamp (default: false)
exceptions     - Method for outputting exceptions (default: single line)
global_context - Immutable context to prepend all messages with
syslog_options - Syslog options (default: Syslog::LOG_PID|Syslog::LOG_CONS)
escape_keys    - Escape chars in keys
strict_logfmt  - Always use double quotes to quote values


20
21
22
23
24
# File 'lib/scrolls.rb', line 20

def init(options={})
  # Set a hint whether #init was called.
  @initialized = true
  @log = Logger.new(options)
end

#internalObject

Internal: The Logger initialized by #init



327
328
329
# File 'lib/scrolls.rb', line 327

def internal
  @log
end

#log(data, &blk) ⇒ Object

Public: Log data and/or wrap a block with start/finish

data - A hash of key/values to log blk - A block to be wrapped by log lines

Examples:

Scrolls.log(test: "test")
test=test
=> nil

Scrolls.log(test: "test") { puts "inner block" }
test=test at=start
inner block
test=test at=finish elapsed=0.000
=> nil


66
67
68
69
70
71
# File 'lib/scrolls.rb', line 66

def log(data, &blk)
  # Allows us to call #log directly and initialize defaults
  @log = Logger.new({}) unless @initialized

  @log.log(data, &blk)
end

#log_exception(e, data) ⇒ Object

Public: Log an exception

e - An exception to pass to the logger data - A hash of key/values to log

Examples:

begin
  raise Exception
rescue Exception => e
  Scrolls.log_exception(e, {test: "test"})
end
test=test at=exception class=Exception message=Exception exception_id=70321999017240
...


88
89
90
91
92
93
# File 'lib/scrolls.rb', line 88

def log_exception(e, data)
  # Allows us to call #log directly and initialize defaults
  @log = Logger.new({}) unless @initialized

  @log.log_exception(e, data)
end

#loggerObject

Public: Get the primary logger



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

def logger
  @log.logger
end

#single_line_exceptions=(boolean) ⇒ Object

Public: Set whether exceptions should generate a single log message. (default: false)

Examples

Scrolls.single_line_exceptions = true


198
199
200
# File 'lib/scrolls.rb', line 198

def single_line_exceptions=(boolean)
  @log.exceptions = boolean
end

#single_line_exceptions?Boolean

Public: Return whether exceptions generate a single log message.

Examples

Scrolls.single_line_exceptions
=> true

Returns:

  • (Boolean)


209
210
211
# File 'lib/scrolls.rb', line 209

def single_line_exceptions?
  @log.single_line_exceptions?
end

#streamObject

Public: Return the stream

Examples

Scrolls.stream
=> #<IO:<STDOUT>>


141
142
143
# File 'lib/scrolls.rb', line 141

def stream
  @log.stream
end

#stream=(out) ⇒ Object

Public: Setup a new output (default: STDOUT)

out - New output

Options

syslog - Load 'Scrolls::SyslogLogger'

Examples

Scrolls.stream = StringIO.new


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

def stream=(out)
  @log.stream=(out)
end

#time_unitObject

Public: Return the time unit currently configured

Examples

Scrolls.time_unit
=> "seconds"


164
165
166
# File 'lib/scrolls.rb', line 164

def time_unit
  @log.time_unit
end

#time_unit=(unit) ⇒ Object

Public: Set the time unit we use for ‘elapsed’ (default: “seconds”)

unit - The time unit (“milliseconds” currently supported)

Examples

Scrolls.time_unit = "milliseconds"


153
154
155
# File 'lib/scrolls.rb', line 153

def time_unit=(unit)
  @log.time_unit = unit
end

#unknown(data, &blk) ⇒ Object

Public: Convience method for Logger replacement

Translates the level to Syslog equivalent

data - A hash of key/values to log blk - A block to be wrapped by log lines

Examples:

Scrolls.unknown(test: "test")
test=test level=alert
=> nil


319
320
321
322
323
# File 'lib/scrolls.rb', line 319

def unknown(data, &blk)
  data = coalesce_strings_to_hash(data)
  data = data.merge(:level => "alert") if data.is_a?(Hash)
  @log.log(data, &blk)
end

#warn(data, &blk) ⇒ Object

Public: Convience method for Logger replacement

Translates the level to Syslog equivalent

data - A hash of key/values to log blk - A block to be wrapped by log lines

Examples:

Scrolls.warn(test: "test")
test=test level=notice
=> nil


300
301
302
303
304
# File 'lib/scrolls.rb', line 300

def warn(data, &blk)
  data = coalesce_strings_to_hash(data)
  data = data.merge(:level => "notice") if data.is_a?(Hash)
  @log.log(data, &blk)
end