Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

Commit

Permalink
Add ability to log from the Ruby Buildpack
Browse files Browse the repository at this point in the history
  • Loading branch information
schneems committed Mar 16, 2018
1 parent 716f4b6 commit 55dd3f2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ vendor/bundle/*
.ruby-version
buildpacks/*
.anvil/
tmp/*.log
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: ruby
rvm:
- 2.4.1
- 2.4.3
before_script: bundle exec rake hatchet:setup_travis
script: bundle exec parallel_rspec -n 11 spec/
env:
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ require "fileutils"
require "tmpdir"
require 'hatchet/tasks'

ENV["BUILDPACK_LOG_FILE"] ||= "tmp/buildpack.log"

S3_BUCKET_NAME = "heroku-buildpack-ruby"
VENDOR_URL = "https://s3.amazonaws.com/#{S3_BUCKET_NAME}"

Expand Down
1 change: 1 addition & 0 deletions lib/language_pack/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def compile
puts @deprecations.join("\n")
end
end
mcount "success.ruby"
end

def write_release_yaml
Expand Down
21 changes: 21 additions & 0 deletions lib/language_pack/shell_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ module LanguagePack
module ShellHelpers
@@user_env_hash = {}

def mcount(key, value = 1)
private_log("count", key => value)
end

def mmeasure(key, value)
private_log("measure", key => value)
end

def munique(key, value)
private_log("unique", key => value)
end

def self.user_env_hash
@@user_env_hash
end
Expand Down Expand Up @@ -149,5 +161,14 @@ def deprecate(message)
def noshellescape(string)
NoShellEscape.new(string)
end

private
def private_log(name, key_value_hash)
File.open(ENV["BUILDPACK_LOG_FILE"], "a+") do |f|
key_value_hash.each do |key, value|
f.puts "#{name}##{ENV["BPLOG_PREFIX"]}#{key}=#{value}"
end
end
end
end
end
15 changes: 15 additions & 0 deletions spec/helpers/shell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ class FakeShell
include LanguagePack::ShellHelpers
end

describe "mcount" do
it "logs to a file" do
begin
original = ENV["BUILDPACK_LOG_FILE"]
Tempfile.open("logfile.log") do |f|
ENV["BUILDPACK_LOG_FILE"] = f.path
FakeShell.new.mcount "foo"
expect(File.read(f.path)).to match("count#foo=1")
end
ensure
ENV["BUILDPACK_LOG_FILE"] = original
end
end
end

describe "#command_options_to_string" do
it "formats ugly keys correctly" do
env = {%Q{ un"matched } => "bad key"}
Expand Down
Empty file added tmp/.gitkeep
Empty file.

0 comments on commit 55dd3f2

Please sign in to comment.