Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow logging of specified environment variables. #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

unixmonkey
Copy link

This PR allows users to log extra environment information to the oink.log, if environment keys are specified.

If an optional env_vars array is passed to the middleware, the logs will have an additional Environment: line with specified environment keys and values:

Rails.application.middleware.use( Oink::Middleware, env_vars: ['action_dispatch.request_id'] )
Jan 29 18:28:39 vocalhost rails[74025]: Oink Action: home#show
Jan 29 18:28:39 vocalhost rails[74025]: Environment: {"action_dispatch.request_id": "4cc822f8-0d85-4d80-bcae-d94a4567e06c"}
Jan 29 18:28:39 vocalhost rails[74025]: Memory usage: 4825792 | PID: 74025
Jan 29 18:28:39 vocalhost rails[74025]: Instantiation Breakdown: Total: 2 | Page: 1 | User: 1
Jan 29 18:28:39 vocalhost rails[74025]: Oink Log Entry Complete

This is especially useful for logging the request ID, since logs may be distributed among multiple app servers, and timestamps don't always line up exactly.

return if @env_vars.empty?
env_message = @env_vars.map { |key|
value = env[key]
"#{key.inspect}: #{value.inspect if value && value.respond_to?(:inspect)}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [82/80]

@@ -36,6 +38,15 @@ def log_routing(env)
end
end

def log_environment(env)
return if @env_vars.empty?
env_message = @env_vars.map { |key|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using {...} for multi-line blocks.


it "includes specified environment variables" do
get "/no_pigs", {}, {"action_dispatch.request_id" => "4cc822f8-0d85-4d80-bcae-d94a4567e06c"}
log_output.string.should include('Environment: {"action_dispatch.request_id": "4cc822f8-0d85-4d80-bcae-d94a4567e06c"}')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [125/80]

end

it "includes specified environment variables" do
get "/no_pigs", {}, {"action_dispatch.request_id" => "4cc822f8-0d85-4d80-bcae-d94a4567e06c"}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer single-quoted strings when you don't need string interpolation or special symbols.
Space inside { missing.
Redundant curly braces around a hash parameter.
Line is too long. [98/80]
Space inside } missing.

Oink::Middleware.new(SampleApplication.new, :logger => logger, :env_vars => ['action_dispatch.request_id'])
end

it "includes specified environment variables" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer single-quoted strings when you don't need string interpolation or special symbols.

@@ -58,6 +58,17 @@ def call(env)
end
end

context "include specified environment variables in the oink log" do
let(:app) do
Oink::Middleware.new(SampleApplication.new, :logger => logger, :env_vars => ['action_dispatch.request_id'])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the new Ruby 1.9 hash syntax.
Line is too long. [113/80]

@@ -58,6 +58,17 @@ def call(env)
end
end

context "include specified environment variables in the oink log" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer single-quoted strings when you don't need string interpolation or special symbols.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants