LaLogger: A general logger module wraps Fluentd-logger

Requirement

  • ruby 1.9.x,
  • activesupport
  • fluent-logger (which depends on yaji-ruby so will not working on JRuby without modification)

Concept

  • Singleton
  • Severity control
  • Centralized log storage by fluentd
  • Send to STDERR in debug mode

Synopsis

Setup fluentd configuration to forward specific tag:

<match la.**>
  # plugin type
  type mongo

  # mongodb host + port
  host localhost
  port 27017

  # mongodb db + collection
  database lalogger_test
  tag_mapped
  collection default

  remove_tag_prefix la.

  # interval
  flush_interval 1s
</match>

In your program:

  1. Define the host and ip where fluentd is running and a global tag matches <match la.**> above:
ENV[FLUENTD] = 'localhost:24224:la'

default port 24224 is optional: localhost::la is same as above.

Note: as logger object is wrapped by a singleton class, there is no effect to change this setting after logger object has created.

  1. Use the logger in your program:
def logger
  # initialize tag and log level, default level is 'WARN'
  LaLogger.new('la.logger.tag', LaLogger::ERROR)
end

Tag set here will becoming collection name if you use tag_mapped setting in your fluent.conf file

def logger_this
  # use warn, error, fatal, ... to log messages with optional
  # extra information in hash format:
  logger.error('oops! an error happend!', {extra: 'data'})
end

You can use raw command post too:

# send (post) message using fluent-logger object directly
logger.post('custom.tag', {custom: 'message'})

In this way you can specify an tag.

Severities

Imported from ruby's default Logger::Severity, namely: debug, info, warn, error, fatal, unknown.

Develop or Test

Run foreman start to start the fluentd server fro test.