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

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


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

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


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

def add_timestamp=(boolean)
  @log.timestamp = boolean
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:



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

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


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

def debug(data, &blk)
  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


241
242
243
244
# File 'lib/scrolls.rb', line 241

def error(data, &blk)
  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


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

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


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

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


259
260
261
262
# File 'lib/scrolls.rb', line 259

def fatal(data, &blk)
  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



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

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


277
278
279
280
# File 'lib/scrolls.rb', line 277

def info(data, &blk)
  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


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

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

#internalObject

Internal: The Logger initialized by #init



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

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


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

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(data, e) ⇒ Object

Public: Log an exception

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

Examples:

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


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

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

  @log.log_exception(data, e)
end

#loggerObject

Public: Get the primary logger



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

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


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

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)


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

def single_line_exceptions?
  @log.single_line_exceptions?
end

#streamObject

Public: Return the stream

Examples

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


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

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


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

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

#time_unitObject

Public: Return the time unit currently configured

Examples

Scrolls.time_unit
=> "seconds"


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

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"


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

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


313
314
315
316
# File 'lib/scrolls.rb', line 313

def unknown(data, &blk)
  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


295
296
297
298
# File 'lib/scrolls.rb', line 295

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