Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import org.apache.maven.plugin.logging.AbstractLog;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.logging.Logger;

Expand All @@ -28,7 +29,7 @@
* @deprecated Use SLF4J directly
*/
@Deprecated
public class DefaultLog
public class DefaultLog extends AbstractLog
implements Log
{

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.apache.maven.plugin.logging;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

import java.util.function.Supplier;

/**
* Abstract class providing some defaults shareable implementation of some Log methods.
*/
public abstract class AbstractLog implements Log
{
/**
* @see org.apache.maven.plugin.logging.Log#debug(Supplier)
*/
public void debug( Supplier<String> messageSupplier )
{
if ( isDebugEnabled() )
{
debug( messageSupplier.get() );
}
}

/**
* @see org.apache.maven.plugin.logging.Log#debug(Supplier, Throwable)
*/
public void debug( Supplier<String> messageSupplier, Throwable error )
{
if ( isDebugEnabled() )
{
debug( messageSupplier.get(), error );
}
}

/**
* @see org.apache.maven.plugin.logging.Log#info(Supplier)
*/
public void info( Supplier<String> messageSupplier )
{
if ( isInfoEnabled() )
{
info( messageSupplier.get() );
}
}

/**
* @see org.apache.maven.plugin.logging.Log#info(Supplier, Throwable)
*/
public void info( Supplier<String> messageSupplier, Throwable error )
{
if ( isInfoEnabled() )
{
info( messageSupplier.get(), error );
}
}

/**
* @see org.apache.maven.plugin.logging.Log#warn(Supplier)
*/
public void warn( Supplier<String> messageSupplier )
{
if ( isWarnEnabled() )
{
warn( messageSupplier.get() );
}
}

/**
* @see org.apache.maven.plugin.logging.Log#warn(Supplier, Throwable)
*/
public void warn( Supplier<String> messageSupplier, Throwable error )
{
if ( isWarnEnabled() )
{
warn( messageSupplier.get(), error );
}
}

/**
* @see org.apache.maven.plugin.logging.Log#error(Supplier)
*/
public void error( Supplier<String> messageSupplier )
{
if ( isErrorEnabled() )
{
error( messageSupplier.get() );
}
}

/**
* @see org.apache.maven.plugin.logging.Log#error(Supplier, Throwable)
*/
public void error( Supplier<String> messageSupplier, Throwable error )
{
if ( isErrorEnabled() )
{
error( messageSupplier.get(), error );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* under the License.
*/

import java.util.function.Supplier;

/**
* This interface supplies the API for providing feedback to the user from the <code>Mojo</code>, using standard
* <code>Maven</code> channels.<br>
Expand Down Expand Up @@ -63,6 +65,23 @@ public interface Log
*/
void debug( Throwable error );

/**
* Send a message to the user in the <b>debug</b> error level by computing the message
* only when needed. The supplier will be called only if @see #isDebugEnabled() is <b>true</b>.
*
* @param messageSupplier a non null Supplier of the message to use
*/
void debug( Supplier<String> messageSupplier );

/**
* Send a message to the user in the <b>debug</b> error level by computing the message
* only when needed. The supplier will be called only if @see #isDebugEnabled() is <b>true</b>.
*
* @param messageSupplier a non null Supplier of the message to use
* @param error the error that occurred and for which the log applies
*/
void debug( Supplier<String> messageSupplier, Throwable error );

/**
* @return true if the <b>info</b> error level is enabled
*/
Expand Down Expand Up @@ -92,6 +111,23 @@ public interface Log
*/
void info( Throwable error );

/**
* Send a message to the user in the <b>info</b> error level by computing the message
* only when needed. The supplier will be called only if @see #isInfoEnabled() is <b>true</b>.
*
* @param messageSupplier a non null Supplier of the message to use
*/
void info( Supplier<String> messageSupplier );

/**
* Send a message to the user in the <b>info</b> error level by computing the message
* only when needed. The supplier will be called only if @see #isInfoEnabled() is <b>true</b>.
*
* @param messageSupplier a non null Supplier of the message to use
* @param error the error that occurred and for which the log applies
*/
void info( Supplier<String> messageSupplier, Throwable error );

/**
* @return true if the <b>warn</b> error level is enabled
*/
Expand Down Expand Up @@ -121,6 +157,23 @@ public interface Log
*/
void warn( Throwable error );

/**
* Send a message to the user in the <b>warn</b> error level by computing the message
* only when needed. The supplier will be called only if @see #isWarnEnabled() is <b>true</b>.
*
* @param messageSupplier a non null Supplier of the message to use
*/
void warn( Supplier<String> messageSupplier );

/**
* Send a message to the user in the <b>warn</b> error level by computing the message
* only when needed. The supplier will be called only if @see #isWarnEnabled() is <b>true</b>.
*
* @param messageSupplier a non null Supplier of the message to use
* @param error the error that occurred and for which the log applies
*/
void warn( Supplier<String> messageSupplier, Throwable error );

/**
* @return true if the <b>error</b> error level is enabled
*/
Expand Down Expand Up @@ -149,4 +202,21 @@ public interface Log
* @param error
*/
void error( Throwable error );

/**
* Send a message to the user in the <b>error</b> error level by computing the message
* only when needed. The supplier will be called only if @see #isErrorEnabled() is <b>true</b>.
*
* @param messageSupplier a non null Supplier of the message to use
*/
void error( Supplier<String> messageSupplier );

/**
* Send a message to the user in the <b>error</b> error level by computing the message
* only when needed. The supplier will be called only if @see #isErrorEnabled() is <b>true</b>.
*
* @param messageSupplier a non null Supplier of the message to use
* @param error the error that occurred and for which the log applies
*/
void error( Supplier<String> messageSupplier, Throwable error );
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @deprecated Use SLF4J directly
*/
@Deprecated
public class SystemStreamLog
public class SystemStreamLog extends AbstractLog
implements Log
{
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.apache.maven.monitor.logging;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

import org.apache.maven.plugin.logging.CountingCallsSupplier;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.logging.Logger;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DefaultLogTest
{
private static final String EXPECTED_MESSAGE = "expected message";

@Test
public void testLazyMessageIsEvaluatedForActiveLogLevels()
{
CountingCallsSupplier<String> messageSupplier = CountingCallsSupplier.of(EXPECTED_MESSAGE);

Throwable fakeError = new RuntimeException();

Log logger = new DefaultLog(new NoOpLogger(Logger.LEVEL_DEBUG));

logger.debug(messageSupplier);
logger.info(messageSupplier);
logger.warn(messageSupplier);
logger.error(messageSupplier);

logger.debug(messageSupplier, fakeError);
logger.info(messageSupplier, fakeError);
logger.warn(messageSupplier, fakeError);
logger.error(messageSupplier, fakeError);

// all log calls should have lead to a supplier call
assertEquals(8, messageSupplier.getNumberOfCalls(), "wrong number of calls to the message supplier");
}

@Test
public void testLazyMessageIsNotEvaluatedForNonActiveLogLevels()
{
CountingCallsSupplier<String> messageSupplier = CountingCallsSupplier.of(EXPECTED_MESSAGE);

Throwable fakeError = new RuntimeException();

Log logger = new DefaultLog(new NoOpLogger(Logger.LEVEL_DISABLED));

logger.debug(messageSupplier);
logger.info(messageSupplier);
logger.warn(messageSupplier);
logger.error(messageSupplier);

logger.debug(messageSupplier, fakeError);
logger.info(messageSupplier, fakeError);
logger.warn(messageSupplier, fakeError);
logger.error(messageSupplier, fakeError);

// no log calls should have lead to any supplier call
assertEquals(0, messageSupplier.getNumberOfCalls(), "wrong number of calls to the message supplier");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.apache.maven.monitor.logging;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

import org.codehaus.plexus.logging.AbstractLogger;
import org.codehaus.plexus.logging.Logger;

/**
* Custom implementation for tests purpose.
*/
public class NoOpLogger extends AbstractLogger
implements Logger {

public NoOpLogger(int threshold)
{
super(threshold, "NoOp");
}

@Override
public void debug(String message, Throwable throwable)
{
// no-op
}

@Override
public void info(String message, Throwable throwable)
{
// no-op
}

@Override
public void warn(String message, Throwable throwable)
{
// no-op
}

@Override
public void error(String message, Throwable throwable)
{
// no-op
}

@Override
public void fatalError(String message, Throwable throwable)
{
// no-op
}

@Override
public Logger getChildLogger(String name) {
return this;
}
}
Loading