Class: SequelOutputter

Inherits:
Log4r::Outputter
  • Object
show all
Defined in:
lib/log4r/outputter/sequeloutputter.rb

Constant Summary collapse

KNOWN_ENGINES =
[
  :postgres,
  :sqlite,
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, hash) ⇒ SequelOutputter

Returns a new instance of SequelOutputter.



16
17
18
19
20
# File 'lib/log4r/outputter/sequeloutputter.rb', line 16

def initialize(name, hash)
  super(name, hash) # make us a real Log4r::Outputter object
  dbh = configure(hash) # validate settings passed
  connect(dbh) # actually connect to the DBH
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



13
14
15
# File 'lib/log4r/outputter/sequeloutputter.rb', line 13

def database
  @database
end

#dbhObject

Returns the value of attribute dbh.



14
15
16
# File 'lib/log4r/outputter/sequeloutputter.rb', line 14

def dbh
  @dbh
end

#delimiterObject (readonly)

Returns the value of attribute delimiter.



13
14
15
# File 'lib/log4r/outputter/sequeloutputter.rb', line 13

def delimiter
  @delimiter
end

#engineObject (readonly)

Returns the value of attribute engine.



13
14
15
# File 'lib/log4r/outputter/sequeloutputter.rb', line 13

def engine
  @engine
end

#fileObject (readonly)

Returns the value of attribute file.



13
14
15
# File 'lib/log4r/outputter/sequeloutputter.rb', line 13

def file
  @file
end

#mapObject (readonly)

Returns the value of attribute map.



13
14
15
# File 'lib/log4r/outputter/sequeloutputter.rb', line 13

def map
  @map
end

#tableObject (readonly)

Returns the value of attribute table.



13
14
15
# File 'lib/log4r/outputter/sequeloutputter.rb', line 13

def table
  @table
end

Instance Method Details

#configure(input) ⇒ Object

input Hash of configuration options



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/log4r/outputter/sequeloutputter.rb', line 23

def configure(input)
  config = input

  # convert all keys to symbols
  new_config = Hash.new
  config.keys.each do |key|
    new_config[key.to_sym] = config[key]
  end
  config = new_config

  @engine = config[:engine].to_sym

  # error checking on table/column settings
  @table     = Time.now.strftime(config[:table]).to_sym
  @map       = config[:map]
  @delimiter = config[:delimiter]

  {
    :delimiter => @delimiter,
    :engine    => @engine,
    :formatter => config[:formatter], # feels a bit weird mixing instance variables and referencing the hash directly
    :map       => @map,
    :table     => @table,
  }.each_pair do |key, value|
    raise Log4r::ConfigError.new(sprintf('required key[%s] missing from configuration', key)) if value.nil?
  end

  if @engine.eql?(:postgres)
    @database = Time.now.strftime(config[:database])
    @file     = @table # this is technically the.. table, but maintaining interface cleanliness
    server    = config[:server]
    port      = config[:port]
    username  = config[:username]
    password  = config[:password]
    @dbh = Sequel.connect(sprintf('postgres://%s:%s@%s:%s/%s', username, password, server, port, @database))
  elsif @engine.eql?(:sqlite)
    @database = nil # sqlite has one DB per file
    @file = Time.now.strftime(config[:file])
    @dbh = Sequel.connect(sprintf('sqlite://%s', @file))
  else
    raise Log4r::ConfigError.new(sprintf('unable to use engine[%s], allowed[%s]', @engine, KNOWN_ENGINES))
  end

  @dbh
end

#connect(dbh) ⇒ Object

dbh a Sequel::Database handle while not really supported, if you take care of the database/table creation, you could hack it in here by <your_logger>.outputters[<sequel outputter>].connect(<dbh>)

Raises:

  • (StandardError)


71
72
73
74
75
76
77
78
# File 'lib/log4r/outputter/sequeloutputter.rb', line 71

def connect(dbh)
  raise StandardError.new(sprintf('invalid parameter class[%s] expecting[Sequel::Database]', dbh.class)) unless dbh.is_a?(Sequel::Database)

  @dbh = dbh

  # idempotently create database/table
  initialize_db
end

#connected?Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/log4r/outputter/sequeloutputter.rb', line 80

def connected?
  # this is sufficient since we throw an exception during #connect if we don't get a good handle
  ! @dbh.nil?
end