Skip to content

Commit 0e745bf

Browse files
committed
Clarify package name in debug log messages
In certain cases, it can be unclear which debug log message is associated with which dependency, where logs for multiple dependencies may appear in different order. These changes improve log clarity by explicitly including the package name in relevant messages.
1 parent e74e050 commit 0e745bf

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ 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 do
65+
Log.context.set package: dep.name
66+
dep.resolver.update_local_cache if dep.resolver.is_a? GitResolver
67+
end
6568
ch.send(nil)
6669
rescue ex : Exception
6770
ch.send(ex)

src/package.cr

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,20 @@ module Shards
5858
end
5959

6060
def install
61-
cleanup_install_directory
61+
Log.with_context do
62+
Log.context.set package: name
63+
Log.context.set version: report_version
64+
65+
cleanup_install_directory
6266

63-
# install the shard:
64-
resolver.install_sources(version, install_path)
67+
# install the shard:
68+
resolver.install_sources(version, install_path)
6569

66-
# link the project's lib path as the shard's lib path, so the dependency
67-
# can access transitive dependencies:
68-
unless resolver.is_a?(PathResolver)
69-
install_lib_path
70+
# link the project's lib path as the shard's lib path, so the dependency
71+
# can access transitive dependencies:
72+
unless resolver.is_a?(PathResolver)
73+
install_lib_path
74+
end
7075
end
7176

7277
Shards.info.installed[name] = self

src/resolvers/git.cr

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

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

368371
output = Process.run(command, shell: true, output: :pipe, chdir: local_path) do |process|
369372
process.output.gets_to_end
@@ -437,7 +440,10 @@ module Shards
437440
raise Error.new("Error missing git command line tool. Please install Git first!")
438441
end
439442

440-
Log.debug { command }
443+
Log.with_context do
444+
Log.context.set package: name
445+
Log.debug { command }
446+
end
441447

442448
output = capture ? IO::Memory.new : Process::Redirect::Close
443449
error = IO::Memory.new

src/resolvers/hg.cr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,10 @@ module Shards
441441
raise Error.new("Error missing hg command line tool. Please install Mercurial first!")
442442
end
443443

444-
Log.debug { command }
444+
Log.with_context do
445+
Log.context.set package: name
446+
Log.debug { command }
447+
end
445448

446449
output = capture ? IO::Memory.new : Process::Redirect::Close
447450
error = IO::Memory.new

0 commit comments

Comments
 (0)