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.5"

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


189
190
191
# File 'lib/scrolls.rb', line 189

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


177
178
179
# File 'lib/scrolls.rb', line 177

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

#coalesce_strings_to_hash(string_or_something_else) ⇒ Object



333
334
335
336
# File 'lib/scrolls.rb', line 333

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:



41
42
43
# File 'lib/scrolls.rb', line 41

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


226
227
228
229
230
# File 'lib/scrolls.rb', line 226

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


245
246
247
248
249
# File 'lib/scrolls.rb', line 245

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

#facilityObject

Public: Return the Syslog facility

Examples

Scrolls.facility
=> 8


116
117
118
# File 'lib/scrolls.rb', line 116

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


105
106
107
# File 'lib/scrolls.rb', line 105

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


264
265
266
267
268
# File 'lib/scrolls.rb', line 264

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

#global_contextObject

Public: Get the global context that prefixs all logs



47
48
49
# File 'lib/scrolls.rb', line 47

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


283
284
285
286
287
# File 'lib/scrolls.rb', line 283

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
adapt_severity_for_syslog - Downgrade severity one level to match syslog (default: true) per https://docs.ruby-lang.org/en/2.1.0/Syslog/Logger.html


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

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

#internalObject

Internal: The Logger initialized by #init



329
330
331
# File 'lib/scrolls.rb', line 329

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


68
69
70
71
72
73
# File 'lib/scrolls.rb', line 68

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
...


90
91
92
93
94
95
# File 'lib/scrolls.rb', line 90

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



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

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


200
201
202
# File 'lib/scrolls.rb', line 200

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)


211
212
213
# File 'lib/scrolls.rb', line 211

def single_line_exceptions?
  @log.single_line_exceptions?
end

#streamObject

Public: Return the stream

Examples

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


143
144
145
# File 'lib/scrolls.rb', line 143

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


132
133
134
# File 'lib/scrolls.rb', line 132

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

#time_unitObject

Public: Return the time unit currently configured

Examples

Scrolls.time_unit
=> "seconds"


166
167
168
# File 'lib/scrolls.rb', line 166

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"


155
156
157
# File 'lib/scrolls.rb', line 155

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


321
322
323
324
325
# File 'lib/scrolls.rb', line 321

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


302
303
304
305
306
# File 'lib/scrolls.rb', line 302

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