Sha256: 048c84ec12417923190fba6d70655ad4432954d4d9034cd0ef0e4410c6510624

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'syslog/logger'
require 'json'

module TangaServices
  # Logs json to syslog.
  # Automatically delegates stuff to the syslog logger.
  # Use like:
  #    TangaServices.logger.open('my_application_name')
  #    TangaServices.logger.info({message: "I'm interesting data"})
  #    TangaServices.logger.error({message: "i crashed"})
  class Logger < Syslog::Logger
    def self.application_name=(application_name)
      @logger ||= Syslog::Logger.new(application_name, Syslog::LOG_LOCAL7)
    end

    def self.logger
      @logger
    end

    def self.debug(hash)
      log(:debug, hash)
    end

    def self.info(hash)
      log(:info, hash)
    end

    def self.write(message)
      log(:info, message: message)
    end
    class << self
      alias :<< :write
    end

    def self.warn(hash)
      log(:warn, hash)
    end

    def self.error(hash)
      log(:error, hash)
    end

    def self.fatal(hash)
      log(:fatal, hash)
    end

    def self.log(level, hash)
      fail ArgumentError, 'must have application_name set' unless @logger
      unless hash.is_a?(Hash)
        hash = { object: hash }
      end

      fail ArgumentError, 'we just log hashes' unless hash.is_a?(Hash)
      data = { level: level, object: hash }
      @logger.send(level, data.to_json)
    end

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

  # Convience access to logger
  def self.logger
    Logger
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tanga_services-0.0.8 lib/tanga_services/logger.rb