Skip to content

Commit cf84a34

Browse files
committed
feat: allow --log-level to be specified in the CLI
Closes: #91
1 parent 3498160 commit cf84a34

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

lib/pact/mock_service/cli.rb

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class CLI < Thor
2222
method_option :pact_file_write_mode, aliases: "-m", desc: PACT_FILE_WRITE_MODE_DESC, type: :string, default: 'overwrite'
2323
method_option :pact_specification_version, aliases: "-i", desc: "The pact specification version to use when writing the pact. Note that only versions 1 and 2 are currently supported.", default: '2'
2424
method_option :log, aliases: "-l", desc: "File to which to log output"
25+
method_option :log_level, desc: "Log level. Options are DEBUG INFO WARN ERROR", default: "DEBUG"
2526
method_option :cors, aliases: "-o", desc: "Support browser security in tests by responding to OPTIONS requests and adding CORS headers to mocked responses"
2627
method_option :ssl, desc: "Use a self-signed SSL cert to run the service over HTTPS", type: :boolean, default: false
2728
method_option :sslcert, desc: "Specify the path to the SSL cert to use when running the service over HTTPS"
@@ -38,6 +39,7 @@ def service
3839
method_option :host, aliases: "-h", desc: "Host on which to bind the service", default: 'localhost'
3940
method_option :pact_dir, aliases: "-d", desc: "Directory to which the pacts will be written"
4041
method_option :log_dir, aliases: "-l", desc: "File to which to log output"
42+
method_option :log_level, desc: "Log level. Options are DEBUG INFO WARN ERROR", default: "DEBUG"
4143
method_option :pact_file_write_mode, aliases: "-m", desc: PACT_FILE_WRITE_MODE_DESC, type: :string, default: 'overwrite'
4244
method_option :pact_specification_version, aliases: "-i", desc: "The pact specification version to use when writing the pact. Note that only versions 1 and 2 are currently supported.", default: '2'
4345
method_option :cors, aliases: "-o", desc: "Support browser security in tests by responding to OPTIONS requests and adding CORS headers to mocked responses"
@@ -59,6 +61,7 @@ def control
5961
method_option :pact_file_write_mode, aliases: "-m", desc: PACT_FILE_WRITE_MODE_DESC, type: :string, default: 'overwrite'
6062
method_option :pid_dir, desc: "PID dir", default: 'tmp/pids'
6163
method_option :log, aliases: "-l", desc: "File to which to log output"
64+
method_option :log_level, desc: "Log level. Options are DEBUG INFO WARN ERROR", default: "DEBUG"
6265
method_option :pact_specification_version, aliases: "-i", desc: "The pact specification version to use when writing the pact. Note that only versions 1 and 2 are currently supported.", default: '2'
6366
method_option :cors, aliases: "-o", desc: "Support browser security in tests by responding to OPTIONS requests and adding CORS headers to mocked responses"
6467
method_option :ssl, desc: "Use a self-signed SSL cert to run the service over HTTPS", type: :boolean, default: false
@@ -89,6 +92,7 @@ def stop
8992
method_option :pact_file_write_mode, aliases: "-m", desc: PACT_FILE_WRITE_MODE_DESC, type: :string, default: 'overwrite'
9093
method_option :pid_dir, desc: "PID dir", default: 'tmp/pids'
9194
method_option :log, aliases: "-l", desc: "File to which to log output"
95+
method_option :log_level, desc: "Log level. Options are DEBUG INFO WARN ERROR", default: "DEBUG"
9296
method_option :pact_specification_version, aliases: "-i", desc: "The pact specification version to use when writing the pact. Note that only versions 1 and 2 are currently supported.", default: '2'
9397
method_option :cors, aliases: "-o", desc: "Support browser security in tests by responding to OPTIONS requests and adding CORS headers to mocked responses"
9498
method_option :ssl, desc: "Use a self-signed SSL cert to run the service over HTTPS", type: :boolean, default: false
@@ -104,6 +108,7 @@ def restart
104108
desc 'control-start', "Start a Pact mock service control server."
105109
method_option :port, aliases: "-p", desc: "Port on which to run the service", default: '1234'
106110
method_option :log_dir, aliases: "-l", desc: "File to which to log output", default: "log"
111+
method_option :log_level, desc: "Log level. Options are DEBUG INFO WARN ERROR", default: "DEBUG"
107112
method_option :pact_file_write_mode, aliases: "-m", desc: PACT_FILE_WRITE_MODE_DESC, type: :string, default: 'overwrite'
108113
method_option :pact_specification_version, aliases: "-i", desc: "The pact specification version to use when writing the pact", default: '2'
109114
method_option :pid_dir, desc: "PID dir", default: "tmp/pids"
@@ -130,6 +135,7 @@ def control_stop
130135
desc 'control-restart', "Start a Pact mock service control server."
131136
method_option :port, aliases: "-p", desc: "Port on which to run the service", default: '1234'
132137
method_option :log_dir, aliases: "-l", desc: "File to which to log output", default: "log"
138+
method_option :log_level, desc: "Log level. Options are DEBUG INFO WARN ERROR", default: "DEBUG"
133139
method_option :pact_dir, aliases: "-d", desc: "Directory to which the pacts will be written", default: "."
134140
method_option :pact_file_write_mode, aliases: "-m", desc: PACT_FILE_WRITE_MODE_DESC, type: :string, default: 'overwrite'
135141
method_option :pact_specification_version, aliases: "-i", desc: "The pact specification version to use when writing the pact. Note that only versions 1 and 2 are currently supported.", default: '2'

lib/pact/mock_service/logger.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,22 @@ def self.from_options options
1919
log_stream = options[:log_file] || $stdout
2020
logger = new log_stream
2121
logger.formatter = options[:log_formatter] if options[:log_formatter]
22-
logger.level = ::Logger::DEBUG
22+
logger.level = logger_level(options[:log_level])
2323
logger
2424
end
25+
26+
def self.logger_level log_level_string
27+
if log_level_string
28+
begin
29+
Kernel.const_get('Logger').const_get(log_level_string.upcase)
30+
rescue NameError
31+
$stderr.puts "WARN: Ignoring log level '#{log_level_string}' as it is not a valid value. Valid values are: DEBUG INFO WARN ERROR FATAL. Using DEBUG."
32+
Logger::DEBUG
33+
end
34+
else
35+
Logger::DEBUG
36+
end
37+
end
2538
end
2639
end
2740
end

lib/pact/mock_service/run.rb

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def call_shutdown_hooks
5252
def service_options
5353
service_options = {
5454
pact_dir: options[:pact_dir],
55+
log_level: options[:log_level],
5556
unique_pact_file_names: options[:unique_pact_file_names],
5657
consumer: options[:consumer],
5758
provider: options[:provider],

lib/pact/mock_service/spawn.rb

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def build_app
3737
def mock_service
3838
Pact::MockService.new(
3939
log_file: create_log_file,
40+
log_level: options[:log_level],
4041
name: name,
4142
consumer: consumer,
4243
provider: provider,

script/example.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
# BEFORE SUITE start mock service
44
# invoked by the pact framework
5-
bundle exec pact-mock-service service \
5+
bundle exec bin/pact-mock-service service \
66
--port 1234 \
77
--consumer Foo \
88
--provider Bar \
99
--pact-specification-version 2 \
1010
--pact-dir ./tmp/pacts \
11-
--log ./tmp/bar_mock_service.log &
11+
--log ./tmp/bar_mock_service.log \
12+
--log-level DEBUG &
1213
pid=$!
1314

1415
# BEFORE SUITE wait for mock service to start up

0 commit comments

Comments
 (0)