Skip to content

Commit

Permalink
[PDI-16280] Job step Write To Log doesn't write errors on log level b…
Browse files Browse the repository at this point in the history
…asic

Filter out log messages by calling LogLevel.isVisible on the entry's log
level
  • Loading branch information
cjsonger committed Jun 29, 2017
1 parent 4dc5827 commit 707645f
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 19 deletions.
Expand Up @@ -2,7 +2,7 @@
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2016 by Pentaho : http://www.pentaho.com
* Copyright (C) 2002-2017 by Pentaho : http://www.pentaho.com
*
*******************************************************************************
*
Expand Down Expand Up @@ -250,47 +250,42 @@ public void setForcingSeparateLogging( boolean forcingSeparateLogging ) {
}
}

LogChannelInterface createLogChannel() {
LogWriterObject logWriterObject = new LogWriterObject( getRealLogSubject(), this, parentJob.getLogLevel() );
return logWriterObject.getLogChannel();
}

/**
* Output message to job log.
*/
public boolean evaluate( Result result ) {
LogWriterObject logWriterObject = new LogWriterObject( getRealLogSubject(), this, parentJob.getLogLevel() );
LogChannelInterface logChannel = logWriterObject.getLogChannel();
LogChannelInterface logChannel = createLogChannel();
String message = getRealLogMessage();

if ( Utils.isEmpty( message ) ) {
// Filter out empty messages and those that are not visible with the job's log level
if ( Utils.isEmpty( message ) || !getEntryLogLevel().isVisible( logChannel.getLogLevel() ) ) {
return true;
}

try {
switch ( getEntryLogLevel() ) {
case ERROR:
if ( logChannel.isError() ) {
logChannel.logError( message + Const.CR );
}
logChannel.logError( message + Const.CR );
break;
case MINIMAL:
logChannel.logMinimal( message + Const.CR );
break;
case BASIC:
if ( logChannel.isBasic() ) {
logChannel.logBasic( message + Const.CR );
}
logChannel.logBasic( message + Const.CR );
break;
case DETAILED:
if ( logChannel.isDetailed() ) {
logChannel.logDetailed( message + Const.CR );
}
logChannel.logDetailed( message + Const.CR );
break;
case DEBUG:
if ( logChannel.isDebug() ) {
logChannel.logDebug( message + Const.CR );
}
logChannel.logDebug( message + Const.CR );
break;
case ROWLEVEL:
if ( logChannel.isRowLevel() ) {
logChannel.logRowlevel( message + Const.CR );
}
logChannel.logRowlevel( message + Const.CR );
break;
default: // NOTHING
break;
Expand Down
@@ -0,0 +1,92 @@
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2017 by Pentaho : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package org.pentaho.di.job.entries.writetolog;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.verification.VerificationMode;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.Result;
import org.pentaho.di.core.logging.KettleLogStore;
import org.pentaho.di.core.logging.LogChannelInterface;
import org.pentaho.di.core.logging.LogLevel;
import org.pentaho.di.job.Job;

/**
* @author Christopher Songer
*/
public class JobEntryWriteToLogTest {

private Job parentJob;
private JobEntryWriteToLog jobEntry;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
KettleLogStore.init();
}

@Before
public void setUp() throws Exception {
parentJob = mock( Job.class );
doReturn( false ).when( parentJob ).isStopped();

jobEntry = new JobEntryWriteToLog();
jobEntry = spy( jobEntry );
}

@Test
public void errorMessageIsNotLoggedWhenParentJobLogLevelIsNothing() {
verifyErrorMessageForParentJobLogLevel( LogLevel.NOTHING, never() );
}

@Test
public void errorMessageIsLoggedWhenParentJobLogLevelIsError() {
verifyErrorMessageForParentJobLogLevel( LogLevel.ERROR, times( 1 ) );
}

@Test
public void errorMessageIsLoggedWhenParentJobLogLevelIsMinimal() {
verifyErrorMessageForParentJobLogLevel( LogLevel.MINIMAL, times( 1 ) );
}

private void verifyErrorMessageForParentJobLogLevel( LogLevel parentJobLogLevel, VerificationMode mode ) {
jobEntry.setLogMessage( "TEST" );
jobEntry.setEntryLogLevel( LogLevel.ERROR );

doReturn( parentJobLogLevel ).when( parentJob ).getLogLevel();
jobEntry.setParentJob( parentJob );

LogChannelInterface logChannel = spy( jobEntry.createLogChannel() );
doReturn( logChannel ).when( jobEntry ).createLogChannel();

jobEntry.evaluate( new Result() );
verify( logChannel, mode ).logError( "TEST" + Const.CR );
}
}

0 comments on commit 707645f

Please sign in to comment.