Skip to content

Commit

Permalink
Extend abstract SLF4J logger instead of deprecated MarkerIgnoringBase
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Heid committed Aug 31, 2023
1 parent ac220aa commit c30b616
Showing 1 changed file with 20 additions and 177 deletions.
197 changes: 20 additions & 177 deletions src/main/java/org/jenkinsci/plugins/postbuildscript/logging/Logger.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package org.jenkinsci.plugins.postbuildscript.logging;

import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MarkerIgnoringBase;
import org.slf4j.Marker;
import org.slf4j.event.Level;
import org.slf4j.helpers.LegacyAbstractLogger;
import org.slf4j.helpers.MessageFormatter;

import java.io.PrintStream;

import hudson.model.AbstractBuild;
import hudson.model.TaskListener;

/**
* @author Daniel Heid
*/
public class Logger extends MarkerIgnoringBase {
public class Logger extends LegacyAbstractLogger {

private static final long serialVersionUID = 1083402096308867767L;

private static final String ERROR_PREFIX = "ERROR"; //NON-NLS
private static final String WARN_PREFIX = "WARN"; //NON-NLS
private static final String INFO_PREFIX = "INFO"; //NON-NLS
private static final String DEBUG_PREFIX = "DEBUG"; //NON-NLS
private static final String TRACE_PREFIX = "TRACE"; //NON-NLS
private static final String FULLY_QUALIFIED_CALLER_NAME = Logger.class.getName();

private final TaskListener listener;
private final boolean verbose;
Expand All @@ -41,59 +38,21 @@ public TaskListener getListener() {
}

@Override
public void error(String msg) {
printToLogger(ERROR_PREFIX, msg, null);
}

@Override
public void error(String format, Object arg) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arg);
printToLogger(ERROR_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}

@Override
public void error(String format, Object arg1, Object arg2) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arg1, arg2);
printToLogger(ERROR_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}

@Override
public void error(String format, Object... arguments) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arguments);
printToLogger(ERROR_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}

@Override
public void error(String msg, Throwable t) {
printToLogger(ERROR_PREFIX, msg, t);
}

@Override
public void warn(String msg) {
printToLogger(WARN_PREFIX, msg, null);
}

@Override
public void warn(String format, Object arg) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arg);
printToLogger(WARN_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}

@Override
public void warn(String format, Object arg1, Object arg2) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arg1, arg2);
printToLogger(WARN_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}

@Override
public void warn(String format, Object... arguments) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arguments);
printToLogger(WARN_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
protected String getFullyQualifiedCallerName() {
return FULLY_QUALIFIED_CALLER_NAME;

Check warning on line 42 in src/main/java/org/jenkinsci/plugins/postbuildscript/logging/Logger.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 42 is not covered by tests
}

@Override
public void warn(String msg, Throwable t) {
printToLogger(WARN_PREFIX, msg, t);
protected void handleNormalizedLoggingCall(Level level, Marker marker, String messagePattern, Object[] arguments, Throwable throwable) {
PrintStream logger = listener.getLogger();
logger.print("[PostBuildScript] - "); //NON-NLS
logger.print('[');
logger.print(level.name());
logger.print("] ");
logger.println(MessageFormatter.basicArrayFormat(messagePattern, arguments));
if (throwable != null) {

Check warning on line 53 in src/main/java/org/jenkinsci/plugins/postbuildscript/logging/Logger.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 53 is only partially covered, one branch is missing
throwable.printStackTrace(logger);

Check warning on line 54 in src/main/java/org/jenkinsci/plugins/postbuildscript/logging/Logger.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 54 is not covered by tests
}
}

@Override
Expand All @@ -106,135 +65,19 @@ public boolean isTraceEnabled() {
return verbose;
}

@Override
public void trace(String msg) {
if (verbose) {
printToLogger(TRACE_PREFIX, msg, null);
}
}

@Override
public void trace(String format, Object arg) {
if (verbose) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arg);
printToLogger(TRACE_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}
}

@Override
public void trace(String format, Object arg1, Object arg2) {
if (verbose) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arg1, arg2);
printToLogger(TRACE_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}
}

@Override
public void trace(String format, Object... arguments) {
if (verbose) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arguments);
printToLogger(TRACE_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}
}

@Override
public void trace(String msg, Throwable t) {
if (verbose) {
printToLogger(TRACE_PREFIX, msg, t);
}
}

@Override
public boolean isDebugEnabled() {
return verbose;
}

@Override
public void debug(String msg) {
if (verbose) {
printToLogger(DEBUG_PREFIX, msg, null);
}
}

@Override
public void debug(String format, Object arg) {
if (verbose) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arg);
printToLogger(DEBUG_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}
}

@Override
public void debug(String format, Object arg1, Object arg2) {
if (verbose) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arg1, arg2);
printToLogger(DEBUG_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}
}

@Override
public void debug(String format, Object... arguments) {
if (verbose) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arguments);
printToLogger(DEBUG_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}
}

@Override
public void debug(String msg, Throwable t) {
if (verbose) {
printToLogger(DEBUG_PREFIX, msg, t);
}
}

@Override
public boolean isInfoEnabled() {
return true;
}

@Override
public void info(String msg) {
printToLogger(INFO_PREFIX, msg, null);
}

@Override
public void info(String format, Object arg) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arg);
printToLogger(INFO_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}

@Override
public void info(String format, Object arg1, Object arg2) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arg1, arg2);
printToLogger(INFO_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}

@Override
public void info(String format, Object... arguments) {
FormattingTuple formattingTuple = MessageFormatter.format(format, arguments);
printToLogger(INFO_PREFIX, formattingTuple.getMessage(), formattingTuple.getThrowable());
}

@Override
public void info(String msg, Throwable t) {
printToLogger(INFO_PREFIX, msg, t);
}

@Override
public boolean isWarnEnabled() {
return true;
}

private void printToLogger(String prefix, @Nullable String message, @Nullable Throwable t) {
PrintStream logger = listener.getLogger();
logger.print("[PostBuildScript] - "); //NON-NLS
logger.print('[');
logger.print(prefix);
logger.print("] ");
logger.println(message);
if (t != null) {
t.printStackTrace(logger);
}
}

}

0 comments on commit c30b616

Please sign in to comment.