timber 2.1.0.rc3 → 2.1.0.rc4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,5 @@
1
- begin
2
- require "lograge"
3
- rescue Exception
4
- end
5
-
6
- require "timber/cli/config_file"
7
- require "timber/cli/file_helper"
8
1
  require "timber/cli/installer"
2
+ require "timber/cli/installers/config_file"
9
3
  require "timber/cli/io/messages"
10
4
 
11
5
  module Timber
@@ -14,106 +8,113 @@ module Timber
14
8
  class Rails < Installer
15
9
  # Runs the installer.
16
10
  def run(app)
17
- # Ask all of the questions up front. This allows us to to apply the
18
- # changes as a neat task list when done.
19
- development_preference = get_development_preference
11
+ install_initializer(app)
12
+ install_development_environment(app)
13
+ install_test_environment(app)
20
14
 
21
- api_key_storage_preference = if get_delivery_strategy(app) == :http
22
- get_api_key_storage_preference
23
- else
24
- nil
15
+ if !app.development? && !app.test?
16
+ install_app_environment(app)
25
17
  end
18
+ end
26
19
 
27
- should_logrageify = logrageify?
28
-
29
- io.puts ""
30
- io.puts IO::Messages.separator
31
- io.puts ""
32
-
33
- # Create the initializer
34
- initializer
35
-
36
- if should_logrageify
37
- logrageify!
20
+ private
21
+ def install_initializer(app)
22
+ initializer_path = File.join("config", "initializers", "timber.rb")
23
+ installer = ConfigFile.new(io, api)
24
+ installer.run(app, initializer_path)
38
25
  end
39
26
 
40
- environment_file_paths.each do |environment_file_path|
41
- environment = File.split(environment_file_path).last.gsub(/\.rb$/, "").to_sym
42
-
43
- case environment
44
- when :development
45
- setup_development_environment(environment_file_path, development_preference)
46
- when :test
47
- setup_test_environment(environment_file_path)
27
+ # Determines the development preference
28
+ def get_development_preference(app)
29
+ if app.development?
30
+ return :send
48
31
  else
49
- setup_other_environment(app, environment_file_path, api_key_storage_preference)
50
- end
51
- end
52
-
53
- true
54
- end
55
-
56
- private
57
- def logrageify?
58
- if defined?(::Lograge)
59
32
  io.puts ""
60
33
  io.puts IO::Messages.separator
61
34
  io.puts ""
62
- io.puts "We noticed you have lograge installed. Would you like to configure "
63
- io.puts "Timber to function similarly?"
64
- io.puts "(This silences template renders, sql queries, and controller calls."
65
- io.puts "You can always do this later in config/initialzers/timber.rb)"
35
+ io.puts "Would you like to temporarily send development logs to this Timber app?"
36
+ io.puts "(Logs will still go to STDOUT, but this provides an easy way to kick the "
37
+ io.puts "tires. Once you're done testing, you can disable this in "
38
+ io.puts "#{IO::ANSI.colorize("config/environments/development.rb", :yellow)})"
66
39
  io.puts ""
67
- io.puts "y) Yes, configure Timber like lograge", :blue
68
- io.puts "n) No, use the Rails logging defaults", :blue
40
+ io.puts "y) Yes, send development logs to Timber", :blue
41
+ io.puts "n) No, just print development logs to STDOUT", :blue
69
42
  io.puts ""
70
43
 
71
- case io.ask_yes_no("Enter your choice:", event_prompt: "Logrageify?")
44
+ case io.ask_yes_no("Enter your choice:", event_prompt: "Send dev logs to Timber?")
72
45
  when :yes
73
- true
46
+ :send
74
47
  when :no
75
- false
48
+ :dont_send
76
49
  end
77
- else
78
- false
79
50
  end
80
51
  end
81
52
 
82
- def logrageify!
83
- task_message = "Logrageifying in #{initializer.path}"
84
- io.task(task_message) do
85
- initializer.logrageify!
53
+ def install_development_environment(app)
54
+ environment_file_path = get_environment_file_path("development")
55
+ if environment_file_path
56
+ if already_installed?(environment_file_path)
57
+ io.task_complete("Timber::Logger already installed #{environment_file_path}")
58
+ return :already_installed
59
+ end
60
+
61
+ development_preference = get_development_preference(app)
62
+
63
+ case development_preference
64
+ when :send
65
+ api_key_code = get_api_key_code(:inline)
66
+
67
+ logger_code = <<-CODE
68
+ # Install the Timber.io logger
69
+ send_logs_to_timber = true # <---- set to false to stop sending dev logs to Timber.io
70
+
71
+ log_device = send_logs_to_timber ? Timber::LogDevices::HTTP.new(#{api_key_code}) : STDOUT
72
+ logger = Timber::Logger.new(log_device)
73
+ logger.level = config.log_level
74
+ config.logger = #{config_set_logger_code}
75
+ CODE
76
+
77
+ install_logger(environment_file_path, logger_code)
78
+ return :http
79
+
80
+ else
81
+ install_stdout(environment_file_path)
82
+ return :stdout
83
+ end
86
84
  end
87
- true
88
85
  end
89
86
 
90
- # Determines the development preference
91
- def get_development_preference
92
- io.puts ""
93
- io.puts IO::Messages.separator
94
- io.puts ""
95
- io.puts "Would you like to temporarily send development logs to Timber?"
96
- io.puts "(Logs will still go to STDOUT, but this provides an easy way to kick the "
97
- io.puts "tires. Once you're done testing, you can disable this in "
98
- io.puts "#{IO::ANSI.colorize("config/environments/development.rb", :yellow)})"
99
- io.puts ""
100
- io.puts "y) Yes, send development logs to Timber", :blue
101
- io.puts "n) No, just print development logs to STDOUT", :blue
102
- io.puts ""
87
+ def install_test_environment(app)
88
+ environment_file_path = get_environment_file_path("test")
89
+ if environment_file_path
90
+ if already_installed?(environment_file_path)
91
+ io.task_complete("Timber::Logger already installed #{environment_file_path}")
92
+ return :already_installed
93
+ end
103
94
 
104
- case io.ask_yes_no("Enter your choice:", event_prompt: "Send dev logs to Timber?")
105
- when :yes
106
- :send
107
- when :no
108
- :dont_send
95
+ # Tests should not be logged by default.
96
+ install_nil(environment_file_path)
97
+ :nil
109
98
  end
110
99
  end
111
100
 
112
- def initializer
113
- @initializer ||= begin
114
- initializer_path = File.join("config", "initializers", "timber.rb")
115
- task_message = "Creating #{initializer_path}"
116
- io.task(task_message) { ConfigFile.new(initializer_path) }
101
+ def install_app_environment(app)
102
+ environment_file_path = get_environment_file_path(app.environment) || get_environment_file_path("production")
103
+ if environment_file_path
104
+ if already_installed?(environment_file_path)
105
+ io.task_complete("Timber::Logger already installed #{environment_file_path}")
106
+ return :already_installed
107
+ end
108
+
109
+ case get_delivery_strategy(app)
110
+ when :http
111
+ api_key_storage_preference = get_api_key_storage_preference
112
+ install_http(environment_file_path, api_key_storage_preference)
113
+ :http
114
+ when :stdout
115
+ install_stdout(environment_file_path)
116
+ :stdout
117
+ end
117
118
  end
118
119
  end
119
120
 
@@ -124,66 +125,14 @@ module Timber
124
125
  "ActiveSupport::TaggedLogging.new(logger)" : "logger"
125
126
  end
126
127
 
127
- # Traverses the config/environments directory and returns an array of
128
- # symbols representing the various environments.
129
- def environment_file_paths
130
- path = File.join("config", "environments", "*.rb")
131
- Dir[path]
132
- end
133
-
134
- def setup_development_environment(environment_file_path, development_preference)
135
- if already_configured?(environment_file_path)
136
- message = "Installing the Timber::Logger in #{environment_file_path}"
137
- io.puts IO::Messages.task_complete(message), :green
138
- return true
139
- end
140
-
141
- case development_preference
142
- when :send
143
- extra_comment = <<-NOTE
144
- # Note: When you are done testing, simply instantiate the logger like this:
145
- #
146
- # logger = Timber::Logger.new(STDOUT)
147
- #
148
- # Be sure to remove the "log_device =" and "logger =" lines below.
149
- NOTE
150
- extra_comment = extra_comment.rstrip
151
- install_http(environment_file_path, :inline, extra_comment: extra_comment)
152
- when :dont_send
153
- install_stdout(environment_file_path)
154
- end
155
- end
156
-
157
- def setup_test_environment(environment_file_path)
158
- if already_configured?(environment_file_path)
159
- message = "Installing the Timber::Logger in #{environment_file_path}"
160
- io.puts IO::Messages.task_complete(message), :green
161
- return true
162
- end
163
-
164
- # Tests should not be logged by default.
165
- install_nil(environment_file_path)
166
- end
167
-
168
- def setup_other_environment(app, environment_file_path, api_key_storage_preference)
169
- if already_configured?(environment_file_path)
170
- message = "Installing the Timber::Logger in #{environment_file_path}"
171
- io.puts IO::Messages.task_complete(message), :green
172
- return true
173
- end
174
-
175
- case get_delivery_strategy(app)
176
- when :http
177
- install_http(environment_file_path, api_key_storage_preference)
178
- when :stdout
179
- install_stdout(environment_file_path)
180
- end
128
+ def get_environment_file_path(environment)
129
+ path = File.join("config", "environments", "#{environment}.rb")
130
+ file_helper.exists?(path) ? path : nil
181
131
  end
182
132
 
183
133
  def install_nil(environment_file_path)
184
134
  logger_code = <<-CODE
185
- # Install the Timber.io logger but silence all logs (log to nil). We install the
186
- # logger to ensure the Rails.logger object exposes the proper API.
135
+ # Install the Timber.io logger, but do not send logs.
187
136
  logger = Timber::Logger.new(nil)
188
137
  logger.level = config.log_level
189
138
  config.logger = #{config_set_logger_code}
@@ -194,12 +143,11 @@ CODE
194
143
 
195
144
  # Installs the Timber logger using the HTTP transport strategy in the
196
145
  # specified environment file.
197
- def install_http(environment_file_path, api_key_storage_type, options = {})
146
+ def install_http(environment_file_path, api_key_storage_type)
198
147
  api_key_code = get_api_key_code(api_key_storage_type)
199
- extra_comment = options[:extra_comment] ? "\n #{options[:extra_comment]}" : nil
200
148
 
201
149
  logger_code = <<-CODE
202
- # Install the Timber.io logger, send logs over HTTP.#{extra_comment}
150
+ # Install the Timber.io logger, send logs over HTTP.
203
151
  log_device = Timber::LogDevices::HTTP.new(#{api_key_code})
204
152
  logger = Timber::Logger.new(log_device)
205
153
  logger.level = config.log_level
@@ -223,15 +171,15 @@ CODE
223
171
  install_logger(environment_file_path, logger_code)
224
172
  end
225
173
 
226
- # Determines if the environment is already configured.
227
- def already_configured?(environment_file_path)
174
+ # Determines if the environment is already installed.
175
+ def already_installed?(environment_file_path)
228
176
  environment_file_contents = get_environment_file_contents(environment_file_path)
229
177
  logger_installed?(environment_file_contents)
230
178
  end
231
179
 
232
180
  # Convenience method for getting the current environment file contents.
233
181
  def get_environment_file_contents(environment_file_path)
234
- FileHelper.read(environment_file_path)
182
+ file_helper.read(environment_file_path)
235
183
  end
236
184
 
237
185
  # Determines if the Timber logger is already installed in the environment
@@ -249,8 +197,7 @@ CODE
249
197
  io.task(task_message) do
250
198
  if !logger_installed?(current_contents)
251
199
  new_contents = current_contents.sub(/\nend/, "\n\n#{logger_code}\nend")
252
- FileHelper.write(environment_file_path, new_contents)
253
- api.event(:file_written, path: environment_file_path)
200
+ file_helper.write(environment_file_path, new_contents)
254
201
  end
255
202
  end
256
203
 
@@ -33,20 +33,10 @@ module Timber
33
33
  run_sub_installer(app)
34
34
  send_test_messages
35
35
  confirm_log_delivery
36
-
37
- assist_with_git
38
-
36
+ wrap_up(app)
39
37
  api.event(:success)
40
-
41
38
  collect_feedback
42
-
43
- io.puts ""
44
- io.puts IO::Messages.separator
45
- io.puts ""
46
- io.puts IO::Messages.free_data
47
- io.puts ""
48
-
49
- true
39
+ free_data
50
40
 
51
41
  when :no
52
42
  io.puts ""
@@ -55,8 +45,6 @@ module Timber
55
45
  io.puts " #{IO::Messages.edit_app_url(app)}", :blue
56
46
  io.puts ""
57
47
  io.puts "exiting..."
58
-
59
- false
60
48
  end
61
49
  end
62
50
 
@@ -111,48 +99,68 @@ module Timber
111
99
  end
112
100
  end
113
101
 
114
- def assist_with_git
102
+ def wrap_up(app)
103
+ if app.development? || app.test?
104
+ development_note
105
+ else
106
+ assist_with_commit_and_deploy
107
+ end
108
+ end
109
+
110
+ def development_note
115
111
  io.puts ""
116
112
  io.puts IO::Messages.separator
117
113
  io.puts ""
118
- io.puts "Last step! We need to commit these changes via git:"
114
+ io.puts "All done! Simply run your application locally and you'll see logs"
115
+ io.puts "show up in Timber. Enjoy!"
119
116
  io.puts ""
120
- io.puts IO::Messages.git_commands
117
+ io.puts "When you're ready to move to production/staging, create a"
118
+ io.puts "production/staging app in Timber and follow the instructions shown."
119
+ io.puts ""
120
+ io.ask_to_proceed
121
+ end
122
+
123
+ def assist_with_commit_and_deploy
124
+ io.puts ""
125
+ io.puts IO::Messages.separator
121
126
  io.puts ""
122
127
 
123
128
  if OSHelper.has_git?
124
- case io.ask_yes_no("We can run these commands for you. Shall we?", event_prompt: "Run git commands?")
129
+ case io.ask_yes_no("All done! Would you like to commit these changes?", event_prompt: "Run git commands?")
125
130
  when :yes
126
131
  io.puts ""
127
132
 
128
133
  task_message = "Committing changes via git"
129
- io.write IO::Messages.task_start(task_message)
134
+ io.task_start(task_message)
130
135
 
131
136
  committed = OSHelper.git_commit_changes
132
137
 
133
138
  if committed
134
- io.puts IO::Messages.task_complete(task_message), :green
139
+ io.task_complete(task_message)
135
140
  else
136
- io.puts IO::Messages.task_failed(task_message), :red
141
+ io.task_failed(task_message)
137
142
 
138
143
  io.puts ""
139
144
  io.puts "Bummer, it looks like we couldn't access the git command.", :yellow
140
- io.puts "No problem though, just copy and paste the above commands to", :yellow
141
- io.puts "run them manually.", :yellow
145
+ io.puts "No problem though, just run these commands yourself:", :yellow
146
+ io.puts ""
147
+ io.puts IO::Messages.git_commands
142
148
  end
143
149
  when :no
144
150
  io.puts ""
145
- io.puts "No problem. Just copy and paste the above commands to run them manually."
151
+ io.puts "No problem. Here's the commands for reference when you're ready:"
152
+ io.puts ""
153
+ io.puts IO::Messages.git_commands
146
154
  end
147
155
  else
148
156
  io.puts ""
149
- io.puts "Finally, commit your changes:"
157
+ io.puts "All done! Commit your changes:"
150
158
  io.puts ""
151
159
  io.puts IO::Messages.git_commands
152
160
  end
153
161
 
154
162
  io.puts ""
155
- io.puts "=> Reminder: git push and deploy 🚀 to see logs in staging/production", :yellow
163
+ io.puts "=> Reminder: remember to deploy 🚀 to see logs in staging/production", :yellow
156
164
  end
157
165
 
158
166
  def collect_feedback
@@ -182,7 +190,15 @@ module Timber
182
190
  io.puts ""
183
191
  io.puts "Thank you! We take feedback seriously and will work to improve this."
184
192
  end
185
- end
193
+ end
194
+
195
+ def free_data
196
+ io.puts ""
197
+ io.puts IO::Messages.separator
198
+ io.puts ""
199
+ io.puts IO::Messages.free_data
200
+ io.puts ""
201
+ end
186
202
  end
187
203
  end
188
204
  end
@@ -76,15 +76,27 @@ module Timber
76
76
  end
77
77
 
78
78
  def task(message, &block)
79
- write IO::Messages.task_start(message), :blue
79
+ task_start(message)
80
80
  result = yield
81
- puts IO::Messages.task_complete(message), :green
81
+ task_complete(message)
82
82
  result
83
83
  rescue Exception => e
84
- puts IO::Messages.task_failed(message), :red
84
+ task_failed(message)
85
85
  raise e
86
86
  end
87
87
 
88
+ def task_start(message)
89
+ write IO::Messages.task_start(message), :blue
90
+ end
91
+
92
+ def task_complete(message)
93
+ puts IO::Messages.task_complete(message), :green
94
+ end
95
+
96
+ def task_failed(message)
97
+ puts IO::Messages.task_failed(message), :red
98
+ end
99
+
88
100
  def write(message, color = nil)
89
101
  if color
90
102
  message = ANSI.colorize(message, color)