Class: Bunyan::Logger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/bunyan.rb,
lib/bunyan/config.rb

Defined Under Namespace

Classes: Config, InvalidConfigurationError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bunyan.rb', line 48

def method_missing(method, *args, &block)
  begin
    collection.send(method, *args) if database_is_usable?
  rescue Mongo::ConnectionFailure
    # At this point, the problem may be that the server was restarted
    # and we have stale connection object. The mongo ruby driver will
    # handle automatically handling a reconnect, and will issue a fresh
    # connection object if it can obtain one. In which case, let's try
    # the query again.
    begin
      collection.send(method, *args, &block) if database_is_usable?
    rescue Mongo::ConnectionFailure => e
      # Ok, we're having real connection issues. The mongod server is likely
      # down. We still may want to fail silently, because bunyan is mostly a support
      # library, and we wouldn't want exceptions to bubble up just b/c the
      # mongod server is down. If it were the core datastore, then we probably
      # would want it to bubble up.
      #
      # If you for some reason you do want error to bubble up, set the
      # `abort_on_failed_reconnect` config option to true.

      raise e if config.abort_on_failed_reconnect?
    end
  end
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



15
16
17
# File 'lib/bunyan.rb', line 15

def collection
  @collection
end

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/bunyan.rb', line 15

def config
  @config
end

#connectionObject (readonly)

Returns the value of attribute connection.



15
16
17
# File 'lib/bunyan.rb', line 15

def connection
  @connection
end

#dbObject (readonly)

Returns the value of attribute db.



15
16
17
# File 'lib/bunyan.rb', line 15

def db
  @db
end

Class Method Details

.method_missing(method, *args, &block) ⇒ Object

Pass all missing class methods to the singleton instance



75
76
77
# File 'lib/bunyan.rb', line 75

def self.method_missing(method, *args, &block)
  Logger.instance.send(method, *args, &block)
end

Instance Method Details

#configure(&block) ⇒ Object

Examples:

Configuring bunyan

Bunyan::Logger.configure do
  # required options
  database   'bunyan_logger'
  collection 'development_log'

  # optional options
  disabled true
  size 52428800 # 50.megabytes in Rails
end


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bunyan.rb', line 27

def configure(&block)
  @config = Logger::Config.new
  @config.abort_on_failed_reconnect = false

  # provide legacy support for old configuration syntax
  (block.arity > 0) ? yield(@config) : @config.instance_eval(&block)

  ensure_required_options_exist
  initialize_connection unless disabled?
  @configured = true
end

#configured?Boolean

Returns:

  • (Boolean)


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

def configured?
  !!@configured
end

#disabled?Boolean

Returns:

  • (Boolean)


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

def disabled?
  # @TODO: Refactor this. Yuck.
  config.nil? || (!config.nil? && config.disabled?)
end