summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2021-11-01 23:52:50 -0600
committerGitHub <noreply@github.com>2021-11-01 23:52:50 -0600
commitf375d179a528f8c3450c44c8f040892fbe70463b (patch)
treed5004ed0ed75146d63d2d578807a12c46473afb3
parentaa4227c5b273182e3267f7fb63aa486575de83f8 (diff)
resolves #3868 add --log-level option to CLI to control log level (PR #4198)
-rw-r--r--CHANGELOG.adoc2
-rw-r--r--lib/asciidoctor/cli/invoker.rb5
-rw-r--r--lib/asciidoctor/cli/options.rb5
-rw-r--r--man/asciidoctor.adoc3
-rw-r--r--test/invoker_test.rb48
-rw-r--r--test/options_test.rb12
6 files changed, 75 insertions, 0 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index f74ba88d..ceddb3ed 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -19,6 +19,8 @@ This project utilizes semantic versioning.
Enhancements::
+ * Allow the CLI to control the logging level using the `--log-level` option (#3868)
+ * Ignore `--log-level` CLI option if `-q` is specified; ignore `-v` CLI option if `--log-level` is specified (#3868)
* Add support for inline linenums mode when using Rouge as source highlighter (`rouge-linenums-mode=inline`) (#3641)
* Omit meta generator tag in HTML output if `reproducible` document attribute is set (#4143) *@rpavlik*
* Add `--sourcemap` option to the CLI to enable the `:sourcemap` option on the parser (#3569) *@hackingotter*
diff --git a/lib/asciidoctor/cli/invoker.rb b/lib/asciidoctor/cli/invoker.rb
index 3c2f758e..af0e97d2 100644
--- a/lib/asciidoctor/cli/invoker.rb
+++ b/lib/asciidoctor/cli/invoker.rb
@@ -79,6 +79,11 @@ module Asciidoctor
end
end
+ if (logger_level = opts.delete :log_level) && !old_logger
+ old_logger_level ||= logger.level
+ logger.level = logger_level
+ end
+
if infiles.size == 1
if (infile0 = infiles[0]) == '-'
outfile ||= infile0
diff --git a/lib/asciidoctor/cli/options.rb b/lib/asciidoctor/cli/options.rb
index 53f8f7a3..2ec055a3 100644
--- a/lib/asciidoctor/cli/options.rb
+++ b/lib/asciidoctor/cli/options.rb
@@ -25,6 +25,7 @@ module Asciidoctor
self[:base_dir] = options[:base_dir]
self[:source_dir] = options[:source_dir]
self[:destination_dir] = options[:destination_dir]
+ self[:log_level] = options[:log_level]
self[:failure_level] = ::Logger::Severity::FATAL
self[:trace] = false
self[:timings] = false
@@ -122,6 +123,10 @@ module Asciidoctor
'may be specified more than once' do |path|
(self[:requires] ||= []).concat path.split ','
end
+ opts.on '--log-level LEVEL', %w(debug DEBUG info INFO warning WARNING error ERROR fatal FATAL), 'set minimum level of log messages that get logged: [DEBUG, INFO, WARN, ERROR, FATAL] (default: WARN)' do |level|
+ level = 'WARN' if (level = level.upcase) == 'WARNING'
+ self[:log_level] = ::Logger::Severity.const_get level
+ end
opts.on '--failure-level LEVEL', %w(info INFO warning WARNING error ERROR fatal FATAL), 'set minimum log level that yields a non-zero exit code: [INFO, WARN, ERROR, FATAL] (default: FATAL)' do |level|
level = 'WARN' if (level = level.upcase) == 'WARNING'
self[:failure_level] = ::Logger::Severity.const_get level
diff --git a/man/asciidoctor.adoc b/man/asciidoctor.adoc
index d32a214e..091ce14f 100644
--- a/man/asciidoctor.adoc
+++ b/man/asciidoctor.adoc
@@ -129,6 +129,9 @@ Matching templates found in subsequent directories override ones previously disc
=== Processing Information
+*--log-level*=_LEVEL_::
+ Set the minimum severity level (default: WARN) of application log messages that get reported.
+
*--failure-level*=_LEVEL_::
Set the minimum logging level (default: FATAL) that yields a non-zero exit code (i.e., failure).
If this option is not set, the program exits with a zero exit code even if warnings or errors have been logged.
diff --git a/test/invoker_test.rb b/test/invoker_test.rb
index 4f9832b4..43fb48cf 100644
--- a/test/invoker_test.rb
+++ b/test/invoker_test.rb
@@ -163,6 +163,54 @@ context 'Invoker' do
assert_match(/WARNING/, warnings)
end
+ test 'should change level on logger when --log-level is specified' do
+ input = <<~'EOS'
+ skip to <<install>>
+
+ . download
+ . install[[install]]
+ . run
+ EOS
+ output = nil
+ redirect_streams do |_, err|
+ invoke_cli(%w(--log-level info), '-') { input }
+ output = err.string
+ end
+ assert_equal 'asciidoctor: INFO: possible invalid reference: install', output.chomp
+ end
+
+ test 'should not log when --log-level and -q are both specified' do
+ input = <<~'EOS'
+ skip to <<install>>
+
+ . download
+ . install[[install]]
+ . run
+ EOS
+ output = nil
+ redirect_streams do |_, err|
+ invoke_cli(%w(--log-level info -q), '-') { input }
+ output = err.string
+ end
+ assert_empty output
+ end
+
+ test 'should use specified log level when --log-level and -v are both specified' do
+ input = <<~'EOS'
+ skip to <<install>>
+
+ . download
+ . install[[install]]
+ . run
+ EOS
+ output = nil
+ redirect_streams do |_, err|
+ invoke_cli(%w(--log-level warn -v), '-') { input }
+ output = err.string
+ end
+ assert_empty output
+ end
+
test 'should enable script warnings if -w flag is specified' do
old_verbose, $VERBOSE = $VERBOSE, false
begin
diff --git a/test/options_test.rb b/test/options_test.rb
index c57e43c0..636b0f6b 100644
--- a/test/options_test.rb
+++ b/test/options_test.rb
@@ -271,6 +271,18 @@ context 'Options' do
assert_includes messages, 'invalid argument: --failure-level=foobar'
end
+ test 'should set log level to DEBUG when --log-level option is specified' do
+ options = Asciidoctor::Cli::Options.parse! %w(--log-level debug test/fixtures/sample.adoc)
+ assert_equal Logger::Severity::DEBUG, options[:log_level]
+ end
+
+ test 'should allow log level to be set to WARN using any recognized abbreviation' do
+ %w(w warn WARN warning WARNING).each do |val|
+ options = Asciidoctor::Cli::Options.parse! %W(--log-level=#{val} test/fixtures/sample.adoc)
+ assert_equal ::Logger::Severity::WARN, options[:log_level]
+ end
+ end
+
test 'should set verbose to 2 when -v flag is specified' do
options = Asciidoctor::Cli::Options.parse! %w(-v test/fixtures/sample.adoc)
assert_equal 2, options[:verbose]