timber 2.1.0.rc3 → 2.1.0.rc4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -17
- data/lib/timber/cli/api.rb +6 -3
- data/lib/timber/cli/api/application.rb +7 -2
- data/lib/timber/cli/config_file.rb +14 -17
- data/lib/timber/cli/file_helper.rb +21 -9
- data/lib/timber/cli/installer.rb +3 -1
- data/lib/timber/cli/installers.rb +47 -39
- data/lib/timber/cli/installers/config_file.rb +60 -0
- data/lib/timber/cli/installers/other.rb +11 -7
- data/lib/timber/cli/installers/rails.rb +93 -146
- data/lib/timber/cli/installers/root.rb +43 -27
- data/lib/timber/cli/io.rb +15 -3
- data/lib/timber/cli/io/messages.rb +3 -4
- data/lib/timber/current_context.rb +15 -3
- data/lib/timber/log_entry.rb +0 -10
- data/lib/timber/logger.rb +7 -3
- data/lib/timber/util.rb +0 -1
- data/lib/timber/util/http_event.rb +6 -18
- data/lib/timber/util/request.rb +32 -11
- data/lib/timber/version.rb +1 -1
- data/spec/timber/cli/config_file_spec.rb +23 -0
- data/spec/timber/cli/installers/config_file_spec.rb +58 -0
- data/spec/timber/cli/installers/other_spec.rb +50 -0
- data/spec/timber/cli/installers/rails_spec.rb +290 -95
- data/spec/timber/cli/installers/root_spec.rb +8 -7
- data/spec/timber/current_context_spec.rb +15 -15
- data/spec/timber/events/http_server_request_spec.rb +1 -1
- data/spec/timber/log_devices/http_spec.rb +2 -2
- data/spec/timber/logger_spec.rb +5 -5
- data/spec/timber/util/http_event_spec.rb +2 -2
- metadata +9 -3
- data/lib/timber/util/string.rb +0 -21
@@ -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
|
-
|
18
|
-
|
19
|
-
|
11
|
+
install_initializer(app)
|
12
|
+
install_development_environment(app)
|
13
|
+
install_test_environment(app)
|
20
14
|
|
21
|
-
|
22
|
-
|
23
|
-
else
|
24
|
-
nil
|
15
|
+
if !app.development? && !app.test?
|
16
|
+
install_app_environment(app)
|
25
17
|
end
|
18
|
+
end
|
26
19
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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 "
|
63
|
-
io.puts "
|
64
|
-
io.puts "
|
65
|
-
io.puts "
|
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,
|
68
|
-
io.puts "n) No,
|
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: "
|
44
|
+
case io.ask_yes_no("Enter your choice:", event_prompt: "Send dev logs to Timber?")
|
72
45
|
when :yes
|
73
|
-
|
46
|
+
:send
|
74
47
|
when :no
|
75
|
-
|
48
|
+
:dont_send
|
76
49
|
end
|
77
|
-
else
|
78
|
-
false
|
79
50
|
end
|
80
51
|
end
|
81
52
|
|
82
|
-
def
|
83
|
-
|
84
|
-
|
85
|
-
|
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
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
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
|
-
|
105
|
-
|
106
|
-
:
|
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
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
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
|
-
|
128
|
-
|
129
|
-
|
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
|
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
|
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
|
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
|
227
|
-
def
|
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
|
-
|
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
|
-
|
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
|
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 "
|
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
|
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("
|
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.
|
134
|
+
io.task_start(task_message)
|
130
135
|
|
131
136
|
committed = OSHelper.git_commit_changes
|
132
137
|
|
133
138
|
if committed
|
134
|
-
io.
|
139
|
+
io.task_complete(task_message)
|
135
140
|
else
|
136
|
-
io.
|
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
|
141
|
-
io.puts "
|
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.
|
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 "
|
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:
|
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
|
-
|
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
|
data/lib/timber/cli/io.rb
CHANGED
@@ -76,15 +76,27 @@ module Timber
|
|
76
76
|
end
|
77
77
|
|
78
78
|
def task(message, &block)
|
79
|
-
|
79
|
+
task_start(message)
|
80
80
|
result = yield
|
81
|
-
|
81
|
+
task_complete(message)
|
82
82
|
result
|
83
83
|
rescue Exception => e
|
84
|
-
|
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)
|