Project

datalogger

0.0
No commit activity in last 3 years
No release in over 3 years
Easy to use structured data logger
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

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