Skip to content

Commit

Permalink
Merge pull request #3 from GoodforGod/dev
Browse files Browse the repository at this point in the history
[0.12.0]
  • Loading branch information
GoodforGod authored Mar 13, 2022
2 parents aa0afb7 + f84b0d3 commit a2b8da7
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 39 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ Java 11+ compatible.

[**Gradle**](https://mvnrepository.com/artifact/io.goodforgod/slf4j-simple-logger)
```groovy
implementation "io.goodforgod:slf4j-simple-logger:0.11.0"
implementation "io.goodforgod:slf4j-simple-logger:0.12.0"
```

[**Maven**](https://mvnrepository.com/artifact/io.goodforgod/slf4j-simple-logger)
```xml
<dependency>
<groupId>io.goodforgod</groupId>
<artifactId>slf4j-simple-logger</artifactId>
<version>0.11.0</version>
<version>0.12.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
groupId=io.goodforgod
artifactId=slf4j-simple-logger
artifactVersion=0.11.0
artifactVersion=0.12.0


##### GRADLE #####
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
final class MessageFormatter {

private static final FormattingTuple EMPTY = new FormattingTuple(null, null, null);

private static class FormatBuilder {

private final StringBuilder builder;
Expand All @@ -32,7 +34,7 @@ private MessageFormatter() {}

static FormattingTuple format(String messagePattern, Object arg) {
if (messagePattern == null) {
return new FormattingTuple(null, null, null);
return EMPTY;
}

final int j = messagePattern.lastIndexOf(DELIM_STR);
Expand All @@ -52,7 +54,7 @@ static FormattingTuple format(String messagePattern, Object arg) {

static FormattingTuple format(String messagePattern, Object arg1, Object arg2) {
if (messagePattern == null) {
return new FormattingTuple(null, null, null);
return EMPTY;
}

final int j2 = messagePattern.lastIndexOf(DELIM_STR);
Expand Down Expand Up @@ -95,17 +97,17 @@ static FormattingTuple formatArray(String messagePattern, Object[] argArray) {

static FormattingTuple formatArray(String messagePattern, Object[] argArray, Throwable throwable) {
if (messagePattern == null) {
return new FormattingTuple(null, argArray, throwable);
return new FormattingTuple(null, null, throwable);
} else if (argArray == null) {
return new FormattingTuple(messagePattern);
return new FormattingTuple(messagePattern, null, throwable);
} else {
int firstArg = messagePattern.indexOf(DELIM_STR);
if (firstArg == -1) {
return new FormattingTuple(messagePattern, argArray, throwable);
return new FormattingTuple(messagePattern, null, throwable);
}

final int limit = argArray.length;
final FormatBuilder fb = new FormatBuilder(messagePattern.length() + 50);
final FormatBuilder fb = new FormatBuilder(messagePattern.length() + 15);
fb.j = firstArg;
while (fb.a < limit) {
final Object arg = argArray[fb.a];
Expand Down Expand Up @@ -148,7 +150,7 @@ private static int predictArgumentSize(Object argument) {
} else if (argument instanceof String) {
return ((String) argument).length() + 1;
} else {
return 50;
return 25;
}
}

Expand Down
89 changes: 64 additions & 25 deletions src/main/java/io/goodforgod/slf4j/simplelogger/SimpleLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static io.goodforgod.slf4j.simplelogger.SimpleLoggerProperties.PREFIX_LOG;

import java.io.PrintWriter;
import java.io.*;
import java.time.LocalDateTime;
import java.time.LocalTime;
import org.slf4j.Logger;
Expand Down Expand Up @@ -207,7 +207,7 @@ private void log(int level, String message, Throwable throwable) {
? Thread.currentThread().getName()
: null;

final int length = predictBuilderLength(message, threadName);
final int length = predictBuilderLength(message, threadName, throwable);
final StringBuilder builder = new StringBuilder(length);

// Append date-time if so configured
Expand Down Expand Up @@ -274,6 +274,10 @@ private void log(int level, String message, Throwable throwable) {
}

private void logEnvironment(StringBuilder builder) {
if (CONFIG.environments.isEmpty()) {
return;
}

if (CONFIG.environmentsOnStart != null) {
builder.append(CONFIG.environmentsOnStart);
} else {
Expand Down Expand Up @@ -305,11 +309,13 @@ private void logEnvironment(StringBuilder builder) {
}
}

private int predictBuilderLength(String message, String threadName) {
private int predictBuilderLength(String message, String threadName, Throwable throwable) {
int length = 14;

if (message != null)
length += message.length();
if (throwable != null)
length += 2048;
if (threadName != null)
length += threadName.length();
if (CONFIG.showDateTime)
Expand All @@ -320,8 +326,8 @@ private int predictBuilderLength(String message, String threadName) {
} else {
for (String env : CONFIG.environments) {
length += (CONFIG.environmentShowName)
? env.length() + 12
: 12;
? env.length() + 6
: 10;
}
}

Expand All @@ -338,33 +344,33 @@ private int predictBuilderLength(String message, String threadName) {

protected String renderLevel(int level) {
switch (level) {
case LOG_LEVEL_TRACE:
return "TRACE ";
case LOG_LEVEL_DEBUG:
return "DEBUG ";
case LOG_LEVEL_INFO:
return "INFO ";
case LOG_LEVEL_WARN:
return "WARN ";
case LOG_LEVEL_ERROR:
return "ERROR ";
case LOG_LEVEL_DEBUG:
return "DEBUG ";
case LOG_LEVEL_TRACE:
return "TRACE ";
default:
throw new IllegalStateException("Unrecognized level [" + level + "]");
}
}

protected String renderLevelInBrackets(int level) {
switch (level) {
case LOG_LEVEL_TRACE:
return "[TRACE] ";
case LOG_LEVEL_DEBUG:
return "[DEBUG] ";
case LOG_LEVEL_INFO:
return "[INFO] ";
case LOG_LEVEL_WARN:
return "[WARN] ";
case LOG_LEVEL_ERROR:
return "[ERROR] ";
case LOG_LEVEL_DEBUG:
return "[DEBUG] ";
case LOG_LEVEL_TRACE:
return "[TRACE] ";
default:
throw new IllegalStateException("Unrecognized level [" + level + "]");
}
Expand All @@ -377,16 +383,33 @@ protected String renderLevelInBrackets(int level) {
* @param builder of logging message
*/
void write(StringBuilder builder) {
final String message = builder.toString();
final byte[] bytes = (CONFIG.charset == null)
? message.getBytes()
: message.getBytes(CONFIG.charset);

final PrintStream printStream = getOutputStream();
synchronized (printStream) {
try {
printStream.write(bytes);
printStream.flush();
} catch (IOException e) {
// do nothing
}
}
}

private PrintStream getOutputStream() {
if (CONFIG.sameOutputChoice)
return CONFIG.outputChoice.getTargetPrintStream();

switch (currentLogLevel) {
case LOG_LEVEL_WARN:
CONFIG.outputChoiceWarn.getTargetPrintStream().print(builder);
break;
return CONFIG.outputChoiceWarn.getTargetPrintStream();
case LOG_LEVEL_ERROR:
CONFIG.outputChoiceError.getTargetPrintStream().print(builder);
break;
return CONFIG.outputChoiceError.getTargetPrintStream();
default:
CONFIG.outputChoice.getTargetPrintStream().print(builder);
break;
return CONFIG.outputChoice.getTargetPrintStream();
}
}

Expand All @@ -402,6 +425,22 @@ private String computeShortName() {
return name.substring(name.lastIndexOf('.') + 1);
}

/**
* For formatted messages, first substitute arguments and then log.
*
* @param level to log
* @param format to parse message
* @param arg1 to format
*/
private void formatAndLog(int level, String format, Object arg1) {
if (!isLevelEnabled(level)) {
return;
}

final FormattingTuple tp = MessageFormatter.format(format, arg1);
log(level, tp.getMessage(), null);
}

/**
* For formatted messages, first substitute arguments and then log.
*
Expand All @@ -416,7 +455,7 @@ private void formatAndLog(int level, String format, Object arg1, Object arg2) {
}

final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
log(level, tp.getMessage(), tp.getThrowable());
log(level, tp.getMessage(), null);
}

/**
Expand Down Expand Up @@ -466,7 +505,7 @@ public void trace(String msg) {
* format outlined above.
*/
public void trace(String format, Object param1) {
formatAndLog(LOG_LEVEL_TRACE, format, param1, null);
formatAndLog(LOG_LEVEL_TRACE, format, param1);
}

/**
Expand Down Expand Up @@ -512,7 +551,7 @@ public void debug(String msg) {
* format outlined above.
*/
public void debug(String format, Object param1) {
formatAndLog(LOG_LEVEL_DEBUG, format, param1, null);
formatAndLog(LOG_LEVEL_DEBUG, format, param1);
}

/**
Expand Down Expand Up @@ -557,7 +596,7 @@ public void info(String msg) {
* format outlined above.
*/
public void info(String format, Object arg) {
formatAndLog(LOG_LEVEL_INFO, format, arg, null);
formatAndLog(LOG_LEVEL_INFO, format, arg);
}

/**
Expand Down Expand Up @@ -603,7 +642,7 @@ public void warn(String msg) {
* format outlined above.
*/
public void warn(String format, Object arg) {
formatAndLog(LOG_LEVEL_WARN, format, arg, null);
formatAndLog(LOG_LEVEL_WARN, format, arg);
}

/**
Expand Down Expand Up @@ -649,7 +688,7 @@ public void error(String msg) {
* format outlined above.
*/
public void error(String format, Object arg) {
formatAndLog(LOG_LEVEL_ERROR, format, arg, null);
formatAndLog(LOG_LEVEL_ERROR, format, arg);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import io.goodforgod.slf4j.simplelogger.OutputChoice.OutputChoiceType;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -49,8 +51,10 @@ public class SimpleLoggerConfiguration {

private static final boolean SHOW_DATE_TIME_DEFAULT = true;

boolean sameOutputChoice = false;
final long initializeTime = System.currentTimeMillis();

Charset charset = StandardCharsets.UTF_8;
OutputChoice outputChoice = null;
OutputChoice outputChoiceWarn = null;
OutputChoice outputChoiceError = null;
Expand Down Expand Up @@ -97,6 +101,14 @@ void init() {
final String logFile = getStringProperty(LOG_FILE, LOG_FILE_DEFAULT);
final String logFileWarn = getStringProperty(LOG_FILE_WARN, LOG_FILE_DEFAULT);
final String logFileError = getStringProperty(LOG_FILE_ERROR, LOG_FILE_DEFAULT);
this.sameOutputChoice = logFile.equals(logFileWarn) && logFileWarn.equals(logFileError);

this.charset = Optional.ofNullable(getStringProperty(CHARSET, null))
.map(charset -> ("null".equals(charset))
? null
: Charset.forName(charset))
.orElse(StandardCharsets.UTF_8);

final boolean cacheOutputStream = getBooleanProperty(CACHE_OUTPUT_STREAM_STRING, CACHE_OUTPUT_STREAM_DEFAULT);
this.outputChoice = computeOutputChoice(logFile, cacheOutputStream);
this.outputChoiceWarn = (logFile.equals(logFileWarn))
Expand Down Expand Up @@ -178,16 +190,16 @@ private DateTimeFormatter getDateTimeFormatter(DateTimeOutputType dateTimeOutput
final String dateTimeFormatStr = getStringProperty(DATE_TIME_FORMAT);
if (dateTimeFormatStr != null) {
try {
final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateTimeFormatStr);
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimeFormatStr);

// check formatting in initialization
if (DateTimeOutputType.DATE_TIME.equals(dateTimeOutputType)) {
dateTimeFormatter.format(LocalDateTime.now());
formatter.format(LocalDateTime.now());
} else if (DateTimeOutputType.TIME.equals(dateTimeOutputType)) {
dateTimeFormatter.format(LocalTime.now());
formatter.format(LocalTime.now());
}

return dateTimeFormatter;
return formatter;
} catch (IllegalArgumentException e) {
Util.report("Bad date format in " + CONFIGURATION_FILE + "; will output relative time", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public enum DateTimeOutputType {
public static final String LOG_FILE_WARN = PREFIX + "logFileWarn";
public static final String LOG_FILE_ERROR = PREFIX + "logFileError";

public static final String CHARSET = PREFIX + "charset";

public static final String LEVEL_IN_BRACKETS = PREFIX + "levelInBrackets";
public static final String SHOW_SHORT_LOG_NAME = PREFIX + "showShortLogName";
public static final String SHOW_LOG_NAME = PREFIX + "showLogName";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.goodforgod.slf4j.simplelogger.LoggerFactoryFriend;
import io.goodforgod.slf4j.simplelogger.SimpleLoggerProperties;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -54,6 +55,12 @@ public StringPrintStream(PrintStream ps, boolean duplicate) {
this.duplicate = duplicate;
}

@Override
public void write(byte[] b) throws IOException {
stringList.add(new String(b));
super.write(b);
}

@Override
public void print(Object s) {
if (duplicate)
Expand Down

0 comments on commit a2b8da7

Please sign in to comment.