Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A simple structured data logger.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Rakefile
Octocat-spinner-32 Readme.md
Octocat-spinner-32 datalogger.gemspec
Readme.md

DataLogger (alpha - API should be considered unstable)

A simple structured data logger. Pass it a hash, and it takes care of emitting to stdout.

Inspiration largely comes from "The Twelve-Factor App" by Adam Wiggins, Mark McGranaghan's "Logs as Data" talk, and various experiments at Heroku.

Usage

The most obvious way:

require 'datalogger'

logger = DataLogger::Logger.new('myapp')
logger.log a: 1, b: 2, c: 3

This will generate a log message to stdout that looks like so:

myapp a=1 b=2 c=3

Or, perhaps you'd like to log how long it takes to perform a given action:

require 'datalogger'

logger = DataLogger::Logger.new('myapp')
logger.log(event: 'create_user') do
  # code to create a user 
end

This will generate a message that looks something like so:

myapp action=create_user at=start
myapp action=create_user at=finish elapsed=0.003116

As a singleton:

require 'datalogger'

DataLogger::Logger.component = 'myapp'
DataLogger::Logger.log a: 1, b: 2, c: 3

Mixing in instance methods

require 'datalogger'

DataLogger::Logger.component = 'myapp'

class MyThing
  include DataLogger

  def do_something
    log(action: 'do_something') do
      # code goes here
    end  
  end
end

Mixing in class methods

require 'datalogger'

DataLogger::Logger.component = 'myapp'

class MyThing
  extend DataLogger

  def self.do_something
    log(action: 'do_something') do
      # code goes here
    end  
  end
end
Something went wrong with that request. Please try again.