td-logger 0.3.15 → 0.3.16

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,8 @@
1
+ == 2012-11-17 version 0.3.16
2
+
3
+ * Disable logger when the key for the ennvironment is missing. issue #6
4
+
5
+
1
6
  == 2012-11-14 version 0.3.15
2
7
 
3
8
  * Removing InstanceMethods as it is deprecated in Rails 3.2 and
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.15
1
+ 0.3.16
@@ -3,8 +3,8 @@ module Logger
3
3
  module Agent::Rails
4
4
 
5
5
  CONFIG_PATH = ENV['TREASURE_DATA_YML'] || 'config/treasure_data.yml'
6
- CONFIG_PATH_EY_LOCAL = 'config/ey_services_config_local.yml'
7
- CONFIG_PATH_EY_DEPLOY = 'config/ey_services_config_deploy.yml'
6
+ CONFIG_PATH_EY_LOCAL = "#{::Rails.root}/config/ey_services_config_local.yml"
7
+ CONFIG_PATH_EY_DEPLOY = "#{::Rails.root}/config/ey_services_config_deploy.yml"
8
8
 
9
9
  require 'td/logger/agent/rack'
10
10
  require 'td/logger/agent/rails/config'
@@ -13,7 +13,8 @@ module Agent::Rails
13
13
 
14
14
  def self.init(rails)
15
15
  c = Config.init
16
- unless c
16
+ if c.disabled
17
+ warn 'Disabling Treasure Data event logger.'
17
18
  ::TreasureData::Logger.open_null
18
19
  return false
19
20
  end
@@ -3,7 +3,7 @@ module Logger
3
3
  module Agent::Rails
4
4
 
5
5
  class Config
6
- def initialize(conf)
6
+ def assign_conf(conf)
7
7
  if agent = conf['agent']
8
8
  host, port = agent.split(':',2)
9
9
  port = (port || 24224).to_i
@@ -36,6 +36,11 @@ module Agent::Rails
36
36
  attr_reader :agent_host, :agent_port, :tag
37
37
  attr_reader :apikey, :database, :auto_create_table
38
38
  attr_reader :access_log_table, :debug_mode
39
+ attr_accessor :disabled
40
+
41
+ def initialize
42
+ @disabled = false
43
+ end
39
44
 
40
45
  def agent_mode?
41
46
  @agent_host != nil
@@ -46,77 +51,47 @@ module Agent::Rails
46
51
  end
47
52
 
48
53
  def self.init
49
- logger = ::Logger.new(STDERR)
50
-
51
- if CONFIG_PATH[0] == ?/
52
- config_path = CONFIG_PATH
53
- else
54
- config_path = "#{::Rails.root}/#{CONFIG_PATH}"
55
- end
54
+ c = Config.new
55
+ config_path = CONFIG_PATH.start_with?('/') ? CONFIG_PATH : "#{::Rails.root}/#{CONFIG_PATH}"
56
56
 
57
57
  if File.exist?(config_path)
58
- load_file(logger, config_path)
59
- elsif File.exist?("#{::Rails.root}/#{CONFIG_PATH_EY_DEPLOY}")
60
- load_file_ey(logger, "#{::Rails.root}/#{CONFIG_PATH_EY_DEPLOY}")
61
- elsif File.exist?("#{::Rails.root}/#{CONFIG_PATH_EY_LOCAL}")
62
- load_file_ey(logger, "#{::Rails.root}/#{CONFIG_PATH_EY_LOCAL}")
58
+ c.load_file(config_path)
59
+ elsif File.exist?(CONFIG_PATH_EY_DEPLOY)
60
+ c.load_file_ey(CONFIG_PATH_EY_DEPLOY)
61
+ elsif File.exist?(CONFIG_PATH_EY_LOCAL)
62
+ c.load_file_ey(CONFIG_PATH_EY_LOCAL)
63
63
  else
64
- load_env(logger)
64
+ c.load_env
65
65
  end
66
- end
67
66
 
68
- def self.load_file(logger, path="#{::Rails.root}/#{CONFIG_PATH}")
69
- require 'yaml'
70
- require 'erb'
67
+ return c
68
+ rescue
69
+ warn "Disabling Treasure Data event logger: #{$!}"
70
+ c.disabled = true
71
+ return c
72
+ end
71
73
 
72
- begin
73
- src = File.read(path)
74
- yaml = ERB.new(src).result
75
- env_conf = YAML.load(yaml)
76
- rescue
77
- logger.warn "WARNING: Can't load #{path} file: #{$!}"
78
- logger.warn "WARNING: Disabling Treasure Data event logger."
79
- return nil
80
- end
74
+ def load_file(path)
75
+ conf = load_yaml(path)[::Rails.env]
81
76
 
82
- conf = env_conf[::Rails.env] if env_conf.is_a?(Hash)
83
77
  unless conf
84
- logger.warn "WARNING: #{path} doesn't include setting for current environment (#{::Rails.env})."
85
- logger.warn "WARNING: Disabling Treasure Data event logger."
86
- return nil
78
+ @disabled = true
79
+ return
87
80
  end
88
81
 
89
- begin
90
- return Config.new(conf)
91
- rescue
92
- logger.warn "WARNING: #{path}: #{$!}."
93
- logger.warn "WARNING: Disabling Treasure Data event logger."
94
- return nil
95
- end
82
+ assign_conf(conf)
96
83
  end
97
84
 
98
- def self.load_file_ey(logger, path)
99
- require 'yaml'
100
- require 'erb'
101
-
102
- begin
103
- src = File.read(path)
104
- yaml = ERB.new(src).result
105
- env_conf = YAML.load(yaml)
106
- rescue
107
- logger.warn "WARNING: Can't load #{path} file: #{$!}"
108
- logger.warn "WARNING: Disabling Treasure Data event logger."
109
- return nil
110
- end
85
+ def load_file_ey(path)
86
+ conf = load_yaml(path)
87
+ apikey = conf['td']['TREASURE_DATA_API_KEY'] if conf.is_a?(Hash) and conf['td'].is_a?(Hash)
111
88
 
112
- apikey = env_conf['td']['TREASURE_DATA_API_KEY'] if env_conf.is_a?(Hash) and env_conf['td'].is_a?(Hash)
113
89
  unless apikey
114
- logger.warn "WARNING: #{path} does not have a configuration of API key."
115
- logger.warn "WARNING: Disabling Treasure Data event logger."
116
- return nil
90
+ @disabled = true
91
+ return
117
92
  end
118
93
 
119
- return Config.new({
94
+ assign_conf({
120
95
  'apikey' => apikey,
121
96
  'database' => ENV['TREASURE_DATA_DB'] || "rails_#{::Rails.env}",
122
97
  'access_log_table' => ENV['TREASURE_DATA_ACCESS_LOG_TABLE'],
@@ -124,22 +99,30 @@ module Agent::Rails
124
99
  })
125
100
  end
126
101
 
127
- def self.load_env(logger)
102
+ def load_env
128
103
  apikey = ENV['TREASURE_DATA_API_KEY'] || ENV['TD_API_KEY']
129
104
 
130
105
  unless apikey
131
- logger.warn "WARNING: #{CONFIG_PATH} does not exist."
132
- logger.warn "WARNING: Disabling Treasure Data event logger."
133
- return nil
106
+ @disabled = true
107
+ return
134
108
  end
135
109
 
136
- return Config.new({
110
+ assign_conf({
137
111
  'apikey' => apikey,
138
112
  'database' => ENV['TREASURE_DATA_DB'] || "rails_#{::Rails.env}",
139
113
  'access_log_table' => ENV['TREASURE_DATA_ACCESS_LOG_TABLE'],
140
114
  'auto_create_table' => true
141
115
  })
142
116
  end
117
+
118
+ def load_yaml(path)
119
+ require 'yaml'
120
+ require 'erb'
121
+
122
+ src = File.read(path)
123
+ yaml = ERB.new(src).result
124
+ YAML.load(yaml)
125
+ end
143
126
  end
144
127
 
145
128
  end
@@ -1,7 +1,7 @@
1
1
  module TreasureData
2
2
  module Logger
3
3
 
4
- VERSION = '0.3.15'
4
+ VERSION = '0.3.16'
5
5
 
6
6
  end
7
7
  end
@@ -56,6 +56,7 @@ describe TreasureData::Logger::Agent::Rails::Config do
56
56
  ENV['TREASURE_DATA_API_KEY'] = 'test1'
57
57
  ENV['TREASURE_DATA_DB'] = 'db1'
58
58
  c = TreasureData::Logger::Agent::Rails::Config.init
59
+ c.disabled.should == false
59
60
  c.agent_mode?.should == false
60
61
  c.apikey.should == 'test1'
61
62
  c.database.should == 'db1'
@@ -73,6 +74,7 @@ test:
73
74
  EOF
74
75
  }
75
76
  c = TreasureData::Logger::Agent::Rails::Config.init
77
+ c.disabled.should == false
76
78
  c.agent_mode?.should == false
77
79
  c.apikey.should == 'test2'
78
80
  c.database.should == 'db2'
@@ -80,6 +82,20 @@ EOF
80
82
  c.debug_mode.should == true
81
83
  end
82
84
 
85
+ it 'load_file without test' do
86
+ FileUtils.mkdir_p("#{TMP_DIR}/config")
87
+ File.open("#{TMP_DIR}/config/treasure_data.yml", "w") {|f|
88
+ f.write <<EOF
89
+ development:
90
+ apikey: test2
91
+ database: db2
92
+ debug_mode: true
93
+ EOF
94
+ }
95
+ c = TreasureData::Logger::Agent::Rails::Config.init
96
+ c.disabled.should == true
97
+ end
98
+
83
99
  it 'prefer file than env' do
84
100
  ENV['TREASURE_DATA_API_KEY'] = 'test3'
85
101
  ENV['TREASURE_DATA_DB'] = 'db3'
@@ -94,6 +110,7 @@ test:
94
110
  EOF
95
111
  }
96
112
  c = TreasureData::Logger::Agent::Rails::Config.init
113
+ c.disabled.should == false
97
114
  c.agent_mode?.should == false
98
115
  c.apikey.should == 'test4'
99
116
  c.database.should == 'db4'
@@ -111,6 +128,7 @@ test:
111
128
  EOF
112
129
  }
113
130
  c = TreasureData::Logger::Agent::Rails::Config.init
131
+ c.disabled.should == false
114
132
  c.agent_mode?.should == true
115
133
  c.tag.should == 'td.db5'
116
134
  c.agent_host.should == 'localhost'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: td-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.15
4
+ version: 0.3.16
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: 2012-11-14 00:00:00.000000000 Z
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: msgpack