Commit 755917c5 authored by Tobias Jeger's avatar Tobias Jeger

CMS-10484 Add LoggingUtils to reduce duplication of a common logging pattern

parent 10e59174
/*
* Copyright 2016 Hippo B.V. (http://www.onehippo.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.onehippo.cms7.util;
import java.util.Arrays;
import org.slf4j.Logger;
/**
* Utility methods to avoid duplication of a common pattern for logging an exception.
*
* We currently only support the WARN logging level.
*/
public class LoggingUtils {
/**
* Log a warning for an unexpected exception.
*
* @param logger logger to use
* @param t the exception
* @param msg a plain message
*/
public static void warnException(final Logger logger, final Throwable t, final String msg) {
if (logger.isDebugEnabled()) {
logger.warn(msg, t);
} else {
logger.warn(msg + ": {}", t.getMessage());
}
}
/**
* Log a warning for an unexpected exception.
*
* @param logger logger to use
* @param t the exception
* @param format message format, taking a single argument
* @param arg argument to interpolate into formatted message
*/
public static void warnException(final Logger logger, final Throwable t, final String format, final Object arg) {
if (logger.isDebugEnabled()) {
logger.warn(format, arg, t);
} else {
logger.warn(format + ": {}", arg, t.getMessage());
}
}
/**
* Log a warning for an unexpected exception.
*
* @param logger logger to use
* @param t the exception
* @param format message format, taking two arguments
* @param arg1 first argument to interpolate into formatted message
* @param arg2 second argument to interpolate into formatted message
*/
public static void warnException(final Logger logger, final Throwable t, final String format, final Object arg1, final Object arg2) {
if (logger.isDebugEnabled()) {
logger.warn(format, arg1, arg2, t);
} else {
logger.warn(format + ": {}", arg1, arg2, t.getMessage());
}
}
/**
* Log a warning for an unexpected exception.
*
* @param logger logger to use
* @param t the exception
* @param format message format, taking three or more arguments
* @param arguments arguments to interpolate into formatted message
*/
public static void warnException(final Logger logger, final Throwable t, final String format, final Object... arguments) {
final Object[] moreArguments = Arrays.copyOf(arguments, arguments.length + 1);
if (logger.isDebugEnabled()) {
moreArguments[arguments.length] = t;
logger.warn(format, moreArguments);
} else {
moreArguments[arguments.length] = t.getMessage();
logger.warn(format + ": {}", moreArguments);
}
}
}
/*
* Copyright 2016 Hippo B.V. (http://www.onehippo.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.onehippo.cms7.util;
/**
* Dummy class used in LoggingUtilsTest to create a logger for which debugging is enabled
* See also the log4j.xml configuration.
*/
public class DebugLogger { }
/*
* Copyright 2016 Hippo B.V. (http://www.onehippo.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.onehippo.cms7.util;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
public class LoggingAppender extends AppenderSkeleton {
public static String message;
public static Throwable throwable;
protected void append(LoggingEvent event) {
message = (String)event.getMessage();
throwable = event.getThrowableInformation() != null ? event.getThrowableInformation().getThrowable() : null;
}
public void close() {}
public boolean requiresLayout() { return false; }
}
/*
* Copyright 2016 Hippo B.V. (http://www.onehippo.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.onehippo.cms7.util;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Check if the mapping to the exposed #warn() methods is correct.
*/
public class LoggingUtilsTest {
private static final Logger log = LoggerFactory.getLogger(LoggingUtilsTest.class);
private static final Logger dbg = LoggerFactory.getLogger(DebugLogger.class);
@Test
public void variations() {
final Exception e = new IllegalStateException("Exception message");
LoggingUtils.warnException(log, e, "Simple");
assertThat(LoggingAppender.message, equalTo("Simple: Exception message"));
assertThat(LoggingAppender.throwable, equalTo(null));
LoggingUtils.warnException(dbg, e, "Simple");
assertThat(LoggingAppender.message, equalTo("Simple"));
assertThat(LoggingAppender.throwable, equalTo(e));
LoggingUtils.warnException(log, e, "Simple {}", "arg1");
assertThat(LoggingAppender.message, equalTo("Simple arg1: Exception message"));
assertThat(LoggingAppender.throwable, equalTo(null));
LoggingUtils.warnException(dbg, e, "Simple {}", "arg1");
assertThat(LoggingAppender.message, equalTo("Simple arg1"));
assertThat(LoggingAppender.throwable, equalTo(e));
LoggingUtils.warnException(log, e, "Simple {} {}", "arg1", "arg2");
assertThat(LoggingAppender.message, equalTo("Simple arg1 arg2: Exception message"));
assertThat(LoggingAppender.throwable, equalTo(null));
LoggingUtils.warnException(dbg, e, "Simple {} {}", "arg1", "arg2");
assertThat(LoggingAppender.message, equalTo("Simple arg1 arg2"));
assertThat(LoggingAppender.throwable, equalTo(e));
LoggingUtils.warnException(log, e, "Simple {} {} {}", "arg1", "arg2", "arg3");
assertThat(LoggingAppender.message, equalTo("Simple arg1 arg2 arg3: Exception message"));
assertThat(LoggingAppender.throwable, equalTo(null));
LoggingUtils.warnException(dbg, e, "Simple {} {} {}", "arg1", "arg2", "arg3");
assertThat(LoggingAppender.message, equalTo("Simple arg1 arg2 arg3"));
assertThat(LoggingAppender.throwable, equalTo(e));
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2016 Hippo B.V. (http://www.onehippo.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.
-->
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- ====================================================================== -->
<!-- A P P E N D E R S -->
<!-- ====================================================================== -->
<!-- console -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd.MM.yyyy HH:mm:ss} %-5p [%C.%M():%L] %m%n"/>
</layout>
</appender>
<!--log-appender for unit testing-->
<appender name="unittest" class="org.onehippo.cms7.util.LoggingAppender"></appender>
<!-- ====================================================================== -->
<!-- L O G G E R S -->
<!-- ====================================================================== -->
<category additivity="false" name="org.onehippo.cms7.util.DebugLogger">
<level value="debug"/>
<appender-ref ref="unittest"/>
</category>
<category additivity="false" name="org.onehippo.cms7.util.LoggingUtilsTest">
<level value="warn"/>
<appender-ref ref="unittest"/>
</category>
<root>
<level value="warn"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment