Module: Lines

Extended by:
UniqueIDs
Defined in:
lib/lines.rb,
lib/lines/loader.rb,
lib/lines/logger.rb,
lib/lines/version.rb,
lib/lines/rack_logger.rb,
lib/lines/active_record.rb

Overview

Lines is an opinionated structured log format and a library.

Log everything in development AND production. Logs should be easy to read, grep and parse. Logging something should never fail. Let the system handle the storage. Write to syslog or STDERR. No log levels necessary. Just log whatever you want.

Example:

log("Oops !", foo: {}, g: [])
#outputs:
# at=2013-03-07T09:21:39+00:00 pid=3242 app=some-process msg="Oops !" foo={} g=[]

Usage:

Lines.use(Syslog, $stderr)
Lines.log(foo: 3, msg: "This")

ctx = Lines.context(encoding_id: Log.id)
ctx.log({})

Lines.context(foo: 'bar') do |l|
  l.log(items_count: 3)
end

Defined Under Namespace

Modules: Error, UniqueIDs Classes: ActiveRecordSubscriber, Context, Dumper, Loader, Logger, RackLogger, StreamOutputter, SyslogOutputter

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from UniqueIDs

id

Class Attribute Details

.dumperObject

Serializing object. Responds to #dump(hash)



43
# File 'lib/lines.rb', line 43

def dumper; @dumper ||= Dumper.new end

.globalObject (readonly)

Returns the value of attribute global.



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

def global
  @global
end

.loaderObject

Parsing object. Responds to #load(string)



35
36
37
38
39
40
# File 'lib/lines.rb', line 35

def loader
  @loader ||= (
    require 'lines/loader'
    Loader
  )
end

Class Method Details

.context(data = {}) {|new_context| ... } ⇒ Object

Add data to the logs

data - a ruby hash

return a Context instance

Yields:

  • (new_context)


84
85
86
87
88
# File 'lib/lines.rb', line 84

def context(data={})
  new_context = Context.new ensure_hash!(data)
  yield new_context if block_given?
  new_context
end

.dump(obj) ⇒ Object

Generates a lines-formatted string from the given object



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

def dump(obj)
  dumper.dump ensure_hash!(obj)
end

.ensure_hash!(obj) ⇒ Object

:nodoc:



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

def ensure_hash!(obj) # :nodoc:
  return {} unless obj
  return obj if obj.kind_of?(Hash)
  return obj.to_h if obj.respond_to?(:to_h)
  {msg: obj}
end

.load(string) ⇒ Object

Parses a lines-formatted string



98
99
100
# File 'lib/lines.rb', line 98

def load(string)
  loader.load(string)
end

.log(obj, args = {}) ⇒ Object

The main function. Used to record objects in the logs as lines.

obj - a ruby hash. coerced to {“msg”=>obj} otherwise args - complementary values to put in the line



73
74
75
76
77
# File 'lib/lines.rb', line 73

def log(obj, args={})
  obj = prepare_obj(obj, args)
  @outputters.each{|out| out.output(dumper, obj) }
  nil
end

.loggerObject

Returns a backward-compatibile Logger



46
47
48
49
50
51
# File 'lib/lines.rb', line 46

def logger
  @logger ||= (
    require 'lines/logger'
    Logger.new(self)
  )
end

.use(*outputs) ⇒ Object

Used to configure lines.

outputs - allows any kind of IO or Syslog

Usage:

Lines.use(Syslog, $stderr, at: proc{ Time.now })


60
61
62
63
64
65
66
67
# File 'lib/lines.rb', line 60

def use(*outputs)
  if outputs.last.kind_of?(Hash)
    @global = outputs.pop
  else
    @global = {}
  end
  @outputters = outputs.flatten.map{|o| to_outputter o}
end