From a469ad6b5bd72bdd4fca39f1b87149d622d1975f Mon Sep 17 00:00:00 2001 From: Michel Peterson Date: Thu, 22 Nov 2018 16:44:57 +0200 Subject: [PATCH] Parametrize the logging pattern to use Currently the packaging of opendaylight is being done with the current opendaylight default. This default does not always suits the needs of every deployment and it is good to have this configurable. This patch adds a `log_pattern` configuration that allows to specify the logging pattern that will be used. At the same time, it also defines a default that is non dependant on PaxOsgi on being enabled [1]. The new default removes the '%X{bundle.id} - %X{bundle.name} - %X{bundle.version}' bits that are PaxOsgin related, and to compensate for that information removed it adds more context to the class name and expands the clipping of the column to 60 characters. [1] https://bugzilla.redhat.com/show_bug.cgi?id=1650576 Change-Id: I923cd372c71ba5c04a27f6d4478ca7c511c14d2d Signed-off-by: Michel Peterson --- README.markdown | 8 +++++++ manifests/config.pp | 7 ++++++ manifests/init.pp | 3 +++ manifests/params.pp | 1 + spec/acceptance/class_spec.rb | 18 ++++++++++++++ spec/classes/opendaylight_spec.rb | 40 +++++++++++++++++++++++++++++++ spec/spec_helper.rb | 8 +++++++ spec/spec_helper_acceptance.rb | 6 +++++ 8 files changed, 91 insertions(+) diff --git a/README.markdown b/README.markdown index 184d700..070ca62 100644 --- a/README.markdown +++ b/README.markdown @@ -351,6 +351,14 @@ Default: `file` Valid options: `file`, `console`. +##### `log_pattern` + +String that controls the log pattern used for logging. + +Default: `%d{ISO8601} | %-5p | %-16t | %-60c{6} | %m%n` + +Valid options: A valid string that is a valid log4j2 pattern. + ##### `enable_ha` Enable or disable ODL High Availablity. diff --git a/manifests/config.pp b/manifests/config.pp index c834d5e..e024a30 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -298,6 +298,13 @@ class opendaylight::config { } } + file_line { 'logpattern': + ensure => present, + path => '/opt/opendaylight/etc/org.ops4j.pax.logging.cfg', + line => "log4j2.pattern = ${::opendaylight::log_pattern}", + match => '^log4j2.pattern.*$' + } + # Configure ODL HA if enabled if $::opendaylight::enable_ha { # Configure ODL OSVDB Clustering diff --git a/manifests/init.pp b/manifests/init.pp index 94c80bb..75c4c17 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -44,6 +44,8 @@ # Defaults to 'min'. # Other possible values: 'max', 'nomax' # see https://logging.apache.org/log4j/2.x/manual/appenders.html#FileAppender for more info +# [*log_pattern*] +# Pattern to use for logging # [*snat_mechanism*] # Sets the mechanism to be used for SNAT (conntrack, controller) # [*enable_tls*] @@ -101,6 +103,7 @@ class opendaylight ( $log_max_size = $::opendaylight::params::log_max_size, $log_max_rollover = $::opendaylight::params::log_max_rollover, $log_rollover_fileindex = $::opendaylight::params::log_rollover_fileindex, + $log_pattern = $::opendaylight::params::log_pattern, $snat_mechanism = $::opendaylight::params::snat_mechanism, $enable_tls = $::opendaylight::params::enable_tls, $tls_keystore_password = $::opendaylight::params::tls_keystore_password, diff --git a/manifests/params.pp b/manifests/params.pp index 7f1142b..5a9e86a 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -27,6 +27,7 @@ class opendaylight::params { $log_max_size = '10GB' $log_max_rollover = 2 $log_rollover_fileindex = 'min' + $log_pattern = '%d{ISO8601} | %-5p | %-16t | %-60c{6} | %m%n' $snat_mechanism = 'controller' $enable_tls = false $tls_keystore_password = undef diff --git a/spec/acceptance/class_spec.rb b/spec/acceptance/class_spec.rb index 7117fb7..6a1a0dd 100644 --- a/spec/acceptance/class_spec.rb +++ b/spec/acceptance/class_spec.rb @@ -111,6 +111,14 @@ describe 'opendaylight class' do log_rollover_fileIndex: 'min') end + context 'log to file customizing pattern' do + # Call specialized helper fn to install OpenDaylight + install_odl(log_pattern: '%d{ISO8601} | %-5p | %-16t | %m%n') + + # Call specialized helper fn for log settings validations + log_settings_validations(log_pattern: '%d{ISO8601} | %-5p | %-16t | %m%n') + end + context 'log to console' do # Call specialized helper fn to install OpenDaylight install_odl(log_mechanism: 'console') @@ -118,6 +126,16 @@ describe 'opendaylight class' do # Call specialized helper fn for log settings validations log_settings_validations(log_mechanism: 'console') end + + context 'log to console customizing pattern' do + # Call specialized helper fn to install OpenDaylight + install_odl(log_mechanism: 'console', + log_pattern: '%d{ISO8601} | %-5p | %-16t | %m%n') + + # Call specialized helper fn for log settings validations + log_settings_validations(log_mechanism: 'console', + log_pattern: '%d{ISO8601} | %-5p | %-16t | %m%n') + end end describe 'testing REST port config file' do diff --git a/spec/classes/opendaylight_spec.rb b/spec/classes/opendaylight_spec.rb index b9a2970..fb3dfdd 100644 --- a/spec/classes/opendaylight_spec.rb +++ b/spec/classes/opendaylight_spec.rb @@ -532,6 +532,25 @@ describe 'opendaylight' do log_rollover_fileindex: 'min') end + context 'log to file customizing pattern' do + let(:facts) {{ + :osfamily => osfamily, + :operatingsystem => operatingsystem, + :operatingsystemmajrelease => operatingsystemmajrelease, + }} + + let(:params) {{ + :log_pattern => '%d{ISO8601} | %-5p | %-16t | %m%n', + }} + + # Run shared tests applicable to all supported OSs + # Note that this function is defined in spec_helper + generic_tests + + # Run test specific to log settings + log_settings(log_pattern: '%d{ISO8601} | %-5p | %-16t | %m%n') + end + context 'log to console' do let(:facts) {{ :osfamily => osfamily, @@ -551,6 +570,27 @@ describe 'opendaylight' do log_settings(log_mechanism: 'console') end + context 'log to console customizing pattern' do + let(:facts) {{ + :osfamily => osfamily, + :operatingsystem => operatingsystem, + :operatingsystemmajrelease => operatingsystemmajrelease, + }} + + let(:params) {{ + :log_mechanism => 'console', + :log_pattern => '%d{ISO8601} | %-5p | %-16t | %m%n', + }} + + # Run shared tests applicable to all supported OSs + # Note that this function is defined in spec_helper + generic_tests + + # Run test specific to log settings + log_settings(log_mechanism: 'console', + log_pattern: '%d{ISO8601} | %-5p | %-16t | %m%n') + end + context 'setting inactivity probe' do let(:facts) {{ :osfamily => osfamily, diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index de4a251..69f2360 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -120,6 +120,7 @@ def log_settings(options = {}) log_max_size = options.fetch(:log_max_size, '10GB') log_max_rollover = options.fetch(:log_max_rollover, 2) log_rollover_fileindex = options.fetch(:log_rollover_fileindex, 'min') + log_pattern = options.fetch(:log_pattern, '%d{ISO8601} | %-5p | %-16t | %-60c{6} | %m%n') log_mechanism = options.fetch(:log_mechanism, 'file') if log_mechanism == 'console' @@ -169,6 +170,13 @@ def log_settings(options = {}) ) } end + it { + should contain_file_line('logpattern').with( + 'path' => '/opt/opendaylight/etc/org.ops4j.pax.logging.cfg', + 'line' => "log4j2.pattern = #{log_pattern}", + 'match' => '^log4j2.pattern.*$', + ) + } end # Shared tests that specialize in testing Karaf feature installs diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index 60d6250..33793a7 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -69,6 +69,7 @@ def install_odl(options = {}) password = options.fetch(:password, 'admin') log_max_size = options.fetch(:log_max_size, '10GB') log_max_rollover = options.fetch(:log_max_rollover, 2) + log_pattern = options.fetch(:log_pattern, '%d{ISO8601} | %-5p | %-16t | %-60c{6} | %m%n') log_rollover_fileindex = options.fetch(:log_rollover_fileindex, 'min') snat_mechanism = options.fetch(:snat_mechanism, 'controller') enable_tls = options.fetch(:enable_tls, false) @@ -98,6 +99,7 @@ def install_odl(options = {}) password => #{password}, log_max_size => '#{log_max_size}', log_max_rollover => #{log_max_rollover}, + log_pattern => '#{log_pattern}', log_rollover_fileindex => #{log_rollover_fileindex}, snat_mechanism => #{snat_mechanism}, enable_tls => #{enable_tls}, @@ -250,6 +252,7 @@ def log_settings_validations(options = {}) # Should contain log level config file with correct file size and rollover values log_max_size = options.fetch(:log_max_size, '10GB') log_max_rollover = options.fetch(:log_max_rollover, 2) + log_pattern = options.fetch(:log_pattern, '%d{ISO8601} | %-5p | %-16t | %-60c{6} | %m%n') log_rollover_fileindex = options.fetch(:log_rollover_fileindex, 'min') log_mechanism = options.fetch(:log_mechanism, 'file') @@ -272,6 +275,9 @@ def log_settings_validations(options = {}) its(:content) { should match /^log4j2.appender.rolling.strategy.fileIndex = #{log_rollover_fileindex}/ } end end + describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do + its(:content) { should match /^log4j2.pattern = #{log_pattern}/ } + end end # Shared function for validations related to the Karaf config file -- 2.36.6