diff options
author | Thom May <thom@may.lt> | 2017-04-06 21:46:37 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-06 21:46:37 +0100 |
commit | b8afdc5630ebe502beadf214b9ca0abc1c98c43b (patch) | |
tree | d15b1b2a25bf46820f1bc1e7a78f0ecbe2f1e81d | |
parent | 30e95f165f0b58464e4bf7df736d3fa4b21d1c4e (diff) | |
parent | 0029b194433491c163d40d4134d306d47c8d0632 (diff) | |
download | chef-b8afdc5630ebe502beadf214b9ca0abc1c98c43b.tar.gz |
Merge pull request #6035 from chef/lcg/restore-log-location
Chef-13: restore log_location in client.rb
-rw-r--r-- | RELEASE_NOTES.md | 13 | ||||
-rw-r--r-- | lib/chef/application.rb | 15 | ||||
-rw-r--r-- | lib/chef/application/windows_service.rb | 17 | ||||
-rw-r--r-- | spec/unit/application_spec.rb | 2 |
4 files changed, 33 insertions, 14 deletions
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index ac49fd7edc..fb6d4a8d97 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -280,21 +280,8 @@ is sent to a pipe. The logger needs to be specifically requested with `--force- The `--force-formatter` option does still exist, although it will probably be deprecated in the future. -Setting the `log_location` in the config.rb now applies to both daemonized and command-line invocation and there is -no magic which dups the logger in order to log to STDOUT on the command line in that case. - -Setting logger settings in config.rb is most likely no longer appropriate and those settings should be changed to -command line flags on the config script or cronjob that invokes chef-client daemonized. In other words: - -``` -chef-client -d -L /var/log/client.rb --force-logger -``` - If your logfiles switch to the formatter, you need to include `--force-logger` for your daemonized runs. -If your command line invocations have no output, delete the config.rb setting for the log_location and move that -to the command line that invokes your daemon process. - Redirecting output to a file with `chef-client > /tmp/chef.out` now captures the same output as invoking it directly on the command line with no redirection. diff --git a/lib/chef/application.rb b/lib/chef/application.rb index 005418d184..096ce9c392 100644 --- a/lib/chef/application.rb +++ b/lib/chef/application.rb @@ -150,6 +150,9 @@ class Chef def configure_logging configure_log_location Chef::Log.init(MonoLogger.new(Chef::Config[:log_location])) + if want_additional_logger? + configure_stdout_logger + end Chef::Log.level = resolve_log_level rescue StandardError => error Chef::Log.fatal("Failed to open or create log file at #{Chef::Config[:log_location]}: #{error.class} (#{error.message})") @@ -170,6 +173,18 @@ class Chef end end + # Based on config and whether or not STDOUT is a tty, should we setup a + # secondary logger for stdout? + def want_additional_logger? + ( Chef::Config[:log_location] != STDOUT ) && STDOUT.tty? && !Chef::Config[:daemonize] + end + + def configure_stdout_logger + stdout_logger = MonoLogger.new(STDOUT) + stdout_logger.formatter = Chef::Log.logger.formatter + Chef::Log.loggers << stdout_logger + end + # Use of output formatters is assumed if `force_formatter` is set or if `force_logger` is not set def using_output_formatter? Chef::Config[:force_formatter] || !Chef::Config[:force_logger] diff --git a/lib/chef/application/windows_service.rb b/lib/chef/application/windows_service.rb index 5e95c14095..fa4b3fd27e 100644 --- a/lib/chef/application/windows_service.rb +++ b/lib/chef/application/windows_service.rb @@ -241,11 +241,26 @@ class Chef def configure_logging Chef::Log.init(MonoLogger.new(resolve_log_location)) + if want_additional_logger? + configure_stdout_logger + end Chef::Log.level = resolve_log_level end + def configure_stdout_logger + stdout_logger = MonoLogger.new(STDOUT) + stdout_logger.formatter = Chef::Log.logger.formatter + Chef::Log.loggers << stdout_logger + end + + # Based on config and whether or not STDOUT is a tty, should we setup a + # secondary logger for stdout? + def want_additional_logger? + ( Chef::Config[:log_location] != STDOUT ) && STDOUT.tty? && !Chef::Config[:daemonize] + end + # Use of output formatters is assumed if `force_formatter` is set or if - # `force_logger` is not set and STDOUT is to a console (tty) + # `force_logger` is not set def using_output_formatter? Chef::Config[:force_formatter] || !Chef::Config[:force_logger] end diff --git a/spec/unit/application_spec.rb b/spec/unit/application_spec.rb index 72921c90ca..7981748962 100644 --- a/spec/unit/application_spec.rb +++ b/spec/unit/application_spec.rb @@ -187,6 +187,8 @@ describe Chef::Application do allow(Chef::Log).to receive(:level=) @monologger = double("Monologger") expect(MonoLogger).to receive(:new).with(Chef::Config[:log_location]).and_return(@monologger) + allow(MonoLogger).to receive(:new).with(STDOUT).and_return(@monologger) + allow(@monologger).to receive(:formatter=).with(Chef::Log.logger.formatter) expect(Chef::Log).to receive(:init).with(@monologger) @app.configure_logging end |