How to write knapsack_pro logs to a file?
set directory where to write log file (option 1 - recommended)
Set KNAPSACK_PRO_LOG_DIR=log
environment variable in order to notify knapsack_pro
gem to write logs to log
directory instead of stdout.
If you have Rails project then this should work for you.
knapsack_pro
will create a file with CI node index in name. For instance if you run tests on 2 CI nodes:
log/knapsack_pro_node_0.log
log/knapsack_pro_node_1.log
KNAPSACK_PRO_LOG_DIR
has higher priority than custom log set in rails_helper.rb
as shown below (option 2).
You can change log level with KNAPSACK_PRO_LOG_LEVEL
environment variable.
set custom logger config (option 2)
In your rails_helper.rb
you can set custom Knapsack Pro logger and write to custom log file.
# Ensure you load Rails before using Rails const below.
# This line should be already in your rails_helper.rb
require File.expand_path('../../config/environment', __FILE__)
require 'logger'
KnapsackPro.logger = Logger.new(Rails.root.join('log', "knapsack_pro_node_#{KnapsackPro::Config::Env.ci_node_index}.log"))
KnapsackPro.logger.level = Logger::DEBUG
Note if you run knapsack_pro
then the very first request to Knapsack Pro API still will be shown to stdout because we need to have set of test files needed to run RSpec before we load rails_helper.rb
where the configuration of logger actually is loaded for the first time.
That is why you may prefer to use option 1 instead of this.
How to preserve logs on my CI after CI build finished?
Follow this tip if you use one of above options to write knapsack_pro
log to the file.
If you would like to keep knapsack_pro
logs after your CI build finished then you could use artifacts or some cache mechanize for your CI provider.
For instance, for CircleCI 2.0 artifacts you can specify log directory:
- run:
name: RSpec via knapsack_pro Queue Mode
command: |
# export word is important here!
export RAILS_ENV=test
bundle exec rake "knapsack_pro:queue:rspec[--format documentation]"
- store_artifacts:
path: log
Now you can preview logs in Artifacts
tab in the Circle CI build view.