onelinejson 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require "onelinejson/version"
2
3
  require 'json'
3
4
  require 'lograge'
@@ -6,6 +7,46 @@ require 'active_support/core_ext/class/attribute'
6
7
  require 'active_support/log_subscriber'
7
8
 
8
9
  module Onelinejson
10
+ REJECTED_HEADERS = [
11
+ /^HTTP_CACHE_.+/,
12
+ /^HTTP_CONNECTION$/,
13
+ /^HTTP_VERSION$/,
14
+ /^HTTP_PRAGMA$/,
15
+ /^HTTP_ACCEPT_LANGUAGE$/,
16
+ /^HTTP_REFERER$/,
17
+ /^HTTP_COOKIE$/,
18
+ /^HTTP_AUTHORIZATION$/,
19
+ /.*HIDDEN.*/,
20
+ ]
21
+ ELIP = "\xe2\x80\xa6"
22
+ LOG_MAX_LENGTH = 1900
23
+
24
+ def self.trim_values(hash, trim_to)
25
+ Hash[hash.map do |k, v|
26
+ if v.is_a? String
27
+ trimmed = if v.size > trim_to
28
+ v[0, trim_to] + ELIP
29
+ else
30
+ v
31
+ end
32
+ [k, trimmed]
33
+ else
34
+ [k, v]
35
+ end
36
+ end]
37
+ end
38
+
39
+ def self.enforce_max_json_length(hash)
40
+ return hash if JSON.dump(hash).size <= LOG_MAX_LENGTH
41
+
42
+ deleted = hash[:request].delete(:params) || hash[:request].delete(:headers)
43
+ if deleted
44
+ enforce_max_json_length(hash)
45
+ else
46
+ hash
47
+ end
48
+ end
49
+
9
50
  module AppControllerMethods
10
51
  def append_info_to_payload(payload)
11
52
  super
@@ -13,21 +54,23 @@ module Onelinejson
13
54
  request.headers.env
14
55
  elsif request.headers.respond_to?(:to_hash)
15
56
  request.headers.to_hash
16
- end.reject do |k, v|
17
- !k.starts_with?("HTTP_") || k == "HTTP_AUTHORIZATION" || k.include?("HIDDEN")
57
+ end.select do |k, v|
58
+ k =~ /^HTTP_/ && !REJECTED_HEADERS.any? {|regex| k =~ regex}
18
59
  end
19
60
  parameters = params.reject do |k,v|
20
61
  k == 'controller' ||
21
62
  k == 'action' ||
22
- v.is_a?(ActionDispatch::Http::UploadedFile)
63
+ v.is_a?(ActionDispatch::Http::UploadedFile) ||
64
+ v.is_a?(Hash)
23
65
  end
24
66
 
25
67
  payload[:request] = {
26
- params: parameters,
68
+ params: Onelinejson.trim_values(parameters, 128),
27
69
  headers: headers,
28
70
  ip: request.ip,
29
71
  uuid: request.env['action_dispatch.request_id'],
30
72
  controller: self.class.name,
73
+ action: action_name,
31
74
  date: Time.now.utc.iso8601,
32
75
  }
33
76
  u_id = @current_user_id || (@current_user && @current_user.id)
@@ -38,21 +81,23 @@ module Onelinejson
38
81
  end
39
82
 
40
83
  class Railtie < Rails::Railtie
84
+ config.log_tags = nil
41
85
  config.lograge = ActiveSupport::OrderedOptions.new
42
86
  config.lograge.formatter = ::Lograge::Formatters::Json.new
43
87
  config.lograge.enabled = true
44
88
  config.lograge.before_format = lambda do |data, payload|
45
89
  request = data.select{ |k,_|
46
- [:method, :path, :format, :action].include?(k)
90
+ [:method, :path, :format].include?(k)
47
91
  }.merge(payload[:request])
48
92
  response = data.select{ |k,_|
49
93
  [:status, :duration, :view, :view_runtime].include?(k)
50
94
  }
51
- {
52
- debug_info: payload[:debug_info] || {},
53
- request: request,
54
- response: response,
55
- }
95
+ Onelinejson.enforce_max_json_length(
96
+ {
97
+ debug_info: payload[:debug_info] || {},
98
+ request: request,
99
+ response: response,
100
+ })
56
101
  end
57
102
 
58
103
  ActiveSupport.on_load(:action_controller) do
@@ -1,3 +1,3 @@
1
1
  module Onelinejson
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onelinejson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-03 00:00:00.000000000 Z
12
+ date: 2013-12-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: lograge