Skip to content

Commit 43526fa

Browse files
committed
Update logger to use context of the package name
1 parent 48f7760 commit 43526fa

File tree

6 files changed

+36
-27
lines changed

6 files changed

+36
-27
lines changed

src/cli.cr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,18 @@ begin
177177
Shards.run
178178
rescue ex : OptionParser::InvalidOption
179179
Shards::Log.fatal(exception: ex) { ex.message }
180+
Shards::Log.trace { ex.inspect_with_backtrace }
180181
exit 1
181182
rescue ex : Shards::ParseError
182183
ex.to_s(STDERR)
183184
exit 1
184185
rescue ex : Shards::Package::Error
185186
package = ex.package
186187
Shards::Log.error(exception: ex) { "Failed to install `#{package.name}`: #{ex.message}" }
187-
Shards::Log.debug { ex.inspect_with_backtrace }
188+
Shards::Log.trace { ex.inspect_with_backtrace }
188189
exit 1
189190
rescue ex : Shards::Error
190191
Shards::Log.error(exception: ex) { ex.message }
191-
Shards::Log.debug { ex.inspect_with_backtrace }
192+
Shards::Log.trace { ex.inspect_with_backtrace }
192193
exit 1
193194
end

src/commands/install.cr

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,16 @@ module Shards
6565
# packages are returned by the solver in reverse topological order,
6666
# so transitive dependencies are installed first
6767
packages.each do |package|
68-
Log.with_context do
69-
Log.context.set package: package.name
70-
# first install the dependency:
71-
next unless install(package)
72-
73-
# then execute the postinstall script
74-
# (with access to all transitive dependencies):
75-
package.postinstall
76-
77-
# always install executables because the path resolver never actually
78-
# installs dependencies:
79-
package.install_executables
80-
end
68+
# first install the dependency:
69+
next unless install(package)
70+
71+
# then execute the postinstall script
72+
# (with access to all transitive dependencies):
73+
package.postinstall
74+
75+
# always install executables because the path resolver never actually
76+
# installs dependencies:
77+
package.install_executables
8178
end
8279
end
8380

src/logger.cr

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ module Shards
3030

3131
FORMATTER = ::Log::Formatter.new do |entry, io|
3232
message = entry.message
33-
33+
package_name = entry.context[:package]?
3434
if @@colors
35+
io << "[" << package_name.colorize(:blue).to_s << "] " if package_name && entry.severity <= ::Log::Severity::Debug
3536
io << if color = LOGGER_COLORS[entry.severity]?
3637
if idx = message.index(' ')
3738
message[0...idx].colorize(color).to_s + message[idx..-1]
@@ -42,7 +43,9 @@ module Shards
4243
message
4344
end
4445
else
45-
io << entry.severity.label[0] << ": " << message
46+
io << entry.severity.label[0] << ": "
47+
io << "[" << package_name << "] " if package_name && entry.severity <= ::Log::Severity::Debug
48+
io << message
4649
end
4750
end
4851
end

src/molinillo_solver.cr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ module Shards
6161
end
6262
spawn do
6363
begin
64-
dep.resolver.update_local_cache if dep.resolver.is_a? GitResolver
64+
Log.with_context(package: dep.name) do
65+
dep.resolver.update_local_cache if dep.resolver.is_a? GitResolver
66+
end
6567
ch.send(nil)
6668
rescue ex : Exception
6769
ch.send(ex)

src/package.cr

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,17 @@ module Shards
6666
end
6767

6868
def install
69-
cleanup_install_directory
69+
Log.with_context(package: name, version: report_version) do
70+
cleanup_install_directory
7071

71-
# install the shard:
72-
resolver.install_sources(version, install_path)
72+
# install the shard:
73+
resolver.install_sources(version, install_path)
7374

74-
# link the project's lib path as the shard's lib path, so the dependency
75-
# can access transitive dependencies:
76-
unless resolver.is_a?(PathResolver)
77-
install_lib_path
75+
# link the project's lib path as the shard's lib path, so the dependency
76+
# can access transitive dependencies:
77+
unless resolver.is_a?(PathResolver)
78+
install_lib_path
79+
end
7880
end
7981

8082
Shards.info.installed[name] = self

src/resolvers/git.cr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,9 @@ module Shards
363363

364364
private def valid_repository?
365365
command = "git config --get remote.origin.mirror"
366-
Log.debug { command }
366+
Log.with_context(package: name) do
367+
Log.debug { command }
368+
end
367369

368370
output = Process.run(command, shell: true, output: :pipe, chdir: local_path) do |process|
369371
process.output.gets_to_end
@@ -437,7 +439,9 @@ module Shards
437439
raise Error.new("Error missing git command line tool. Please install Git first!")
438440
end
439441

440-
Log.debug { command }
442+
Log.with_context(package: name) do
443+
Log.debug { command }
444+
end
441445

442446
output = capture ? IO::Memory.new : Process::Redirect::Close
443447
error = IO::Memory.new

0 commit comments

Comments
 (0)