Search code examples
javachef-infrachef-solo

Logging Java Exceptions in Chef


I am invoking a jar using chef like :

execute "publish" do
  Chef::Log.info("About to published")
  command "java -jar myjar.jar"
  Chef::Log.info("Published")      
end

Now in case while executing myjar, it throws an exception, the same is not visible on the console. Is there some way by which the same can be viewed in the console itself ?


Solution

  • Chef does not provide any mechanism to view the output of a command diect. The workaround is to write to a file & then read from it.

    output  = "/tmp/output.tmp"
    
    
    execute "publish" do
      Chef::Log.info("About to published")
      command command "java -jar myjar.jar &> #{output}"
      action :run
      Chef::Log.info("Published")
    end
    
    # Outputting logs to console
    ruby_block "log" do
        block do
            print "\n"
            File.open(output).each do |line|
                print line
            end
        end
    end