Skip to content

Commit 55dd3f2

Browse files
committed
Add ability to log from the Ruby Buildpack
1 parent 716f4b6 commit 55dd3f2

File tree

7 files changed

+41
-1
lines changed

7 files changed

+41
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ vendor/bundle/*
66
.ruby-version
77
buildpacks/*
88
.anvil/
9+
tmp/*.log

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: ruby
22
rvm:
3-
- 2.4.1
3+
- 2.4.3
44
before_script: bundle exec rake hatchet:setup_travis
55
script: bundle exec parallel_rspec -n 11 spec/
66
env:

Rakefile

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ require "fileutils"
22
require "tmpdir"
33
require 'hatchet/tasks'
44

5+
ENV["BUILDPACK_LOG_FILE"] ||= "tmp/buildpack.log"
6+
57
S3_BUCKET_NAME = "heroku-buildpack-ruby"
68
VENDOR_URL = "https://s3.amazonaws.com/#{S3_BUCKET_NAME}"
79

lib/language_pack/base.rb

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def compile
9393
puts @deprecations.join("\n")
9494
end
9595
end
96+
mcount "success.ruby"
9697
end
9798

9899
def write_release_yaml

lib/language_pack/shell_helpers.rb

+21
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ module LanguagePack
1414
module ShellHelpers
1515
@@user_env_hash = {}
1616

17+
def mcount(key, value = 1)
18+
private_log("count", key => value)
19+
end
20+
21+
def mmeasure(key, value)
22+
private_log("measure", key => value)
23+
end
24+
25+
def munique(key, value)
26+
private_log("unique", key => value)
27+
end
28+
1729
def self.user_env_hash
1830
@@user_env_hash
1931
end
@@ -149,5 +161,14 @@ def deprecate(message)
149161
def noshellescape(string)
150162
NoShellEscape.new(string)
151163
end
164+
165+
private
166+
def private_log(name, key_value_hash)
167+
File.open(ENV["BUILDPACK_LOG_FILE"], "a+") do |f|
168+
key_value_hash.each do |key, value|
169+
f.puts "#{name}##{ENV["BPLOG_PREFIX"]}#{key}=#{value}"
170+
end
171+
end
172+
end
152173
end
153174
end

spec/helpers/shell_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ class FakeShell
1414
include LanguagePack::ShellHelpers
1515
end
1616

17+
describe "mcount" do
18+
it "logs to a file" do
19+
begin
20+
original = ENV["BUILDPACK_LOG_FILE"]
21+
Tempfile.open("logfile.log") do |f|
22+
ENV["BUILDPACK_LOG_FILE"] = f.path
23+
FakeShell.new.mcount "foo"
24+
expect(File.read(f.path)).to match("count#foo=1")
25+
end
26+
ensure
27+
ENV["BUILDPACK_LOG_FILE"] = original
28+
end
29+
end
30+
end
31+
1732
describe "#command_options_to_string" do
1833
it "formats ugly keys correctly" do
1934
env = {%Q{ un"matched } => "bad key"}

tmp/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)