Skip to content

Commit a2b8da7

Browse files
authored
Merge pull request #3 from GoodforGod/dev
[0.12.0]
2 parents aa0afb7 + f84b0d3 commit a2b8da7

File tree

7 files changed

+101
-39
lines changed

7 files changed

+101
-39
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ Java 11+ compatible.
2525

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

3131
[**Maven**](https://mvnrepository.com/artifact/io.goodforgod/slf4j-simple-logger)
3232
```xml
3333
<dependency>
3434
<groupId>io.goodforgod</groupId>
3535
<artifactId>slf4j-simple-logger</artifactId>
36-
<version>0.11.0</version>
36+
<version>0.12.0</version>
3737
</dependency>
3838
```
3939

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
groupId=io.goodforgod
22
artifactId=slf4j-simple-logger
3-
artifactVersion=0.11.0
3+
artifactVersion=0.12.0
44

55

66
##### GRADLE #####

src/main/java/io/goodforgod/slf4j/simplelogger/MessageFormatter.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
final class MessageFormatter {
1515

16+
private static final FormattingTuple EMPTY = new FormattingTuple(null, null, null);
17+
1618
private static class FormatBuilder {
1719

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

3335
static FormattingTuple format(String messagePattern, Object arg) {
3436
if (messagePattern == null) {
35-
return new FormattingTuple(null, null, null);
37+
return EMPTY;
3638
}
3739

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

5355
static FormattingTuple format(String messagePattern, Object arg1, Object arg2) {
5456
if (messagePattern == null) {
55-
return new FormattingTuple(null, null, null);
57+
return EMPTY;
5658
}
5759

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

9698
static FormattingTuple formatArray(String messagePattern, Object[] argArray, Throwable throwable) {
9799
if (messagePattern == null) {
98-
return new FormattingTuple(null, argArray, throwable);
100+
return new FormattingTuple(null, null, throwable);
99101
} else if (argArray == null) {
100-
return new FormattingTuple(messagePattern);
102+
return new FormattingTuple(messagePattern, null, throwable);
101103
} else {
102104
int firstArg = messagePattern.indexOf(DELIM_STR);
103105
if (firstArg == -1) {
104-
return new FormattingTuple(messagePattern, argArray, throwable);
106+
return new FormattingTuple(messagePattern, null, throwable);
105107
}
106108

107109
final int limit = argArray.length;
108-
final FormatBuilder fb = new FormatBuilder(messagePattern.length() + 50);
110+
final FormatBuilder fb = new FormatBuilder(messagePattern.length() + 15);
109111
fb.j = firstArg;
110112
while (fb.a < limit) {
111113
final Object arg = argArray[fb.a];
@@ -148,7 +150,7 @@ private static int predictArgumentSize(Object argument) {
148150
} else if (argument instanceof String) {
149151
return ((String) argument).length() + 1;
150152
} else {
151-
return 50;
153+
return 25;
152154
}
153155
}
154156

src/main/java/io/goodforgod/slf4j/simplelogger/SimpleLogger.java

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
import java.io.PrintWriter;
5+
import java.io.*;
66
import java.time.LocalDateTime;
77
import java.time.LocalTime;
88
import org.slf4j.Logger;
@@ -207,7 +207,7 @@ private void log(int level, String message, Throwable throwable) {
207207
? Thread.currentThread().getName()
208208
: null;
209209

210-
final int length = predictBuilderLength(message, threadName);
210+
final int length = predictBuilderLength(message, threadName, throwable);
211211
final StringBuilder builder = new StringBuilder(length);
212212

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

276276
private void logEnvironment(StringBuilder builder) {
277+
if (CONFIG.environments.isEmpty()) {
278+
return;
279+
}
280+
277281
if (CONFIG.environmentsOnStart != null) {
278282
builder.append(CONFIG.environmentsOnStart);
279283
} else {
@@ -305,11 +309,13 @@ private void logEnvironment(StringBuilder builder) {
305309
}
306310
}
307311

308-
private int predictBuilderLength(String message, String threadName) {
312+
private int predictBuilderLength(String message, String threadName, Throwable throwable) {
309313
int length = 14;
310314

311315
if (message != null)
312316
length += message.length();
317+
if (throwable != null)
318+
length += 2048;
313319
if (threadName != null)
314320
length += threadName.length();
315321
if (CONFIG.showDateTime)
@@ -320,8 +326,8 @@ private int predictBuilderLength(String message, String threadName) {
320326
} else {
321327
for (String env : CONFIG.environments) {
322328
length += (CONFIG.environmentShowName)
323-
? env.length() + 12
324-
: 12;
329+
? env.length() + 6
330+
: 10;
325331
}
326332
}
327333

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

339345
protected String renderLevel(int level) {
340346
switch (level) {
341-
case LOG_LEVEL_TRACE:
342-
return "TRACE ";
343-
case LOG_LEVEL_DEBUG:
344-
return "DEBUG ";
345347
case LOG_LEVEL_INFO:
346348
return "INFO ";
347349
case LOG_LEVEL_WARN:
348350
return "WARN ";
349351
case LOG_LEVEL_ERROR:
350352
return "ERROR ";
353+
case LOG_LEVEL_DEBUG:
354+
return "DEBUG ";
355+
case LOG_LEVEL_TRACE:
356+
return "TRACE ";
351357
default:
352358
throw new IllegalStateException("Unrecognized level [" + level + "]");
353359
}
354360
}
355361

356362
protected String renderLevelInBrackets(int level) {
357363
switch (level) {
358-
case LOG_LEVEL_TRACE:
359-
return "[TRACE] ";
360-
case LOG_LEVEL_DEBUG:
361-
return "[DEBUG] ";
362364
case LOG_LEVEL_INFO:
363365
return "[INFO] ";
364366
case LOG_LEVEL_WARN:
365367
return "[WARN] ";
366368
case LOG_LEVEL_ERROR:
367369
return "[ERROR] ";
370+
case LOG_LEVEL_DEBUG:
371+
return "[DEBUG] ";
372+
case LOG_LEVEL_TRACE:
373+
return "[TRACE] ";
368374
default:
369375
throw new IllegalStateException("Unrecognized level [" + level + "]");
370376
}
@@ -377,16 +383,33 @@ protected String renderLevelInBrackets(int level) {
377383
* @param builder of logging message
378384
*/
379385
void write(StringBuilder builder) {
386+
final String message = builder.toString();
387+
final byte[] bytes = (CONFIG.charset == null)
388+
? message.getBytes()
389+
: message.getBytes(CONFIG.charset);
390+
391+
final PrintStream printStream = getOutputStream();
392+
synchronized (printStream) {
393+
try {
394+
printStream.write(bytes);
395+
printStream.flush();
396+
} catch (IOException e) {
397+
// do nothing
398+
}
399+
}
400+
}
401+
402+
private PrintStream getOutputStream() {
403+
if (CONFIG.sameOutputChoice)
404+
return CONFIG.outputChoice.getTargetPrintStream();
405+
380406
switch (currentLogLevel) {
381407
case LOG_LEVEL_WARN:
382-
CONFIG.outputChoiceWarn.getTargetPrintStream().print(builder);
383-
break;
408+
return CONFIG.outputChoiceWarn.getTargetPrintStream();
384409
case LOG_LEVEL_ERROR:
385-
CONFIG.outputChoiceError.getTargetPrintStream().print(builder);
386-
break;
410+
return CONFIG.outputChoiceError.getTargetPrintStream();
387411
default:
388-
CONFIG.outputChoice.getTargetPrintStream().print(builder);
389-
break;
412+
return CONFIG.outputChoice.getTargetPrintStream();
390413
}
391414
}
392415

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

428+
/**
429+
* For formatted messages, first substitute arguments and then log.
430+
*
431+
* @param level to log
432+
* @param format to parse message
433+
* @param arg1 to format
434+
*/
435+
private void formatAndLog(int level, String format, Object arg1) {
436+
if (!isLevelEnabled(level)) {
437+
return;
438+
}
439+
440+
final FormattingTuple tp = MessageFormatter.format(format, arg1);
441+
log(level, tp.getMessage(), null);
442+
}
443+
405444
/**
406445
* For formatted messages, first substitute arguments and then log.
407446
*
@@ -416,7 +455,7 @@ private void formatAndLog(int level, String format, Object arg1, Object arg2) {
416455
}
417456

418457
final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
419-
log(level, tp.getMessage(), tp.getThrowable());
458+
log(level, tp.getMessage(), null);
420459
}
421460

422461
/**
@@ -466,7 +505,7 @@ public void trace(String msg) {
466505
* format outlined above.
467506
*/
468507
public void trace(String format, Object param1) {
469-
formatAndLog(LOG_LEVEL_TRACE, format, param1, null);
508+
formatAndLog(LOG_LEVEL_TRACE, format, param1);
470509
}
471510

472511
/**
@@ -512,7 +551,7 @@ public void debug(String msg) {
512551
* format outlined above.
513552
*/
514553
public void debug(String format, Object param1) {
515-
formatAndLog(LOG_LEVEL_DEBUG, format, param1, null);
554+
formatAndLog(LOG_LEVEL_DEBUG, format, param1);
516555
}
517556

518557
/**
@@ -557,7 +596,7 @@ public void info(String msg) {
557596
* format outlined above.
558597
*/
559598
public void info(String format, Object arg) {
560-
formatAndLog(LOG_LEVEL_INFO, format, arg, null);
599+
formatAndLog(LOG_LEVEL_INFO, format, arg);
561600
}
562601

563602
/**
@@ -603,7 +642,7 @@ public void warn(String msg) {
603642
* format outlined above.
604643
*/
605644
public void warn(String format, Object arg) {
606-
formatAndLog(LOG_LEVEL_WARN, format, arg, null);
645+
formatAndLog(LOG_LEVEL_WARN, format, arg);
607646
}
608647

609648
/**
@@ -649,7 +688,7 @@ public void error(String msg) {
649688
* format outlined above.
650689
*/
651690
public void error(String format, Object arg) {
652-
formatAndLog(LOG_LEVEL_ERROR, format, arg, null);
691+
formatAndLog(LOG_LEVEL_ERROR, format, arg);
653692
}
654693

655694
/**

src/main/java/io/goodforgod/slf4j/simplelogger/SimpleLoggerConfiguration.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import io.goodforgod.slf4j.simplelogger.OutputChoice.OutputChoiceType;
66
import java.io.*;
7+
import java.nio.charset.Charset;
8+
import java.nio.charset.StandardCharsets;
79
import java.security.AccessController;
810
import java.security.PrivilegedAction;
911
import java.time.LocalDateTime;
@@ -49,8 +51,10 @@ public class SimpleLoggerConfiguration {
4951

5052
private static final boolean SHOW_DATE_TIME_DEFAULT = true;
5153

54+
boolean sameOutputChoice = false;
5255
final long initializeTime = System.currentTimeMillis();
5356

57+
Charset charset = StandardCharsets.UTF_8;
5458
OutputChoice outputChoice = null;
5559
OutputChoice outputChoiceWarn = null;
5660
OutputChoice outputChoiceError = null;
@@ -97,6 +101,14 @@ void init() {
97101
final String logFile = getStringProperty(LOG_FILE, LOG_FILE_DEFAULT);
98102
final String logFileWarn = getStringProperty(LOG_FILE_WARN, LOG_FILE_DEFAULT);
99103
final String logFileError = getStringProperty(LOG_FILE_ERROR, LOG_FILE_DEFAULT);
104+
this.sameOutputChoice = logFile.equals(logFileWarn) && logFileWarn.equals(logFileError);
105+
106+
this.charset = Optional.ofNullable(getStringProperty(CHARSET, null))
107+
.map(charset -> ("null".equals(charset))
108+
? null
109+
: Charset.forName(charset))
110+
.orElse(StandardCharsets.UTF_8);
111+
100112
final boolean cacheOutputStream = getBooleanProperty(CACHE_OUTPUT_STREAM_STRING, CACHE_OUTPUT_STREAM_DEFAULT);
101113
this.outputChoice = computeOutputChoice(logFile, cacheOutputStream);
102114
this.outputChoiceWarn = (logFile.equals(logFileWarn))
@@ -178,16 +190,16 @@ private DateTimeFormatter getDateTimeFormatter(DateTimeOutputType dateTimeOutput
178190
final String dateTimeFormatStr = getStringProperty(DATE_TIME_FORMAT);
179191
if (dateTimeFormatStr != null) {
180192
try {
181-
final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateTimeFormatStr);
193+
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimeFormatStr);
182194

183195
// check formatting in initialization
184196
if (DateTimeOutputType.DATE_TIME.equals(dateTimeOutputType)) {
185-
dateTimeFormatter.format(LocalDateTime.now());
197+
formatter.format(LocalDateTime.now());
186198
} else if (DateTimeOutputType.TIME.equals(dateTimeOutputType)) {
187-
dateTimeFormatter.format(LocalTime.now());
199+
formatter.format(LocalTime.now());
188200
}
189201

190-
return dateTimeFormatter;
202+
return formatter;
191203
} catch (IllegalArgumentException e) {
192204
Util.report("Bad date format in " + CONFIGURATION_FILE + "; will output relative time", e);
193205
}

src/main/java/io/goodforgod/slf4j/simplelogger/SimpleLoggerProperties.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public enum DateTimeOutputType {
2626
public static final String LOG_FILE_WARN = PREFIX + "logFileWarn";
2727
public static final String LOG_FILE_ERROR = PREFIX + "logFileError";
2828

29+
public static final String CHARSET = PREFIX + "charset";
30+
2931
public static final String LEVEL_IN_BRACKETS = PREFIX + "levelInBrackets";
3032
public static final String SHOW_SHORT_LOG_NAME = PREFIX + "showShortLogName";
3133
public static final String SHOW_LOG_NAME = PREFIX + "showLogName";

src/test/java/io/goodforgod/slf4j/simplelogger/multiThreadedExecution/SimpleLoggerMultithreadedInitializationTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.goodforgod.slf4j.simplelogger.LoggerFactoryFriend;
44
import io.goodforgod.slf4j.simplelogger.SimpleLoggerProperties;
5+
import java.io.IOException;
56
import java.io.PrintStream;
67
import java.util.ArrayList;
78
import java.util.Collections;
@@ -54,6 +55,12 @@ public StringPrintStream(PrintStream ps, boolean duplicate) {
5455
this.duplicate = duplicate;
5556
}
5657

58+
@Override
59+
public void write(byte[] b) throws IOException {
60+
stringList.add(new String(b));
61+
super.write(b);
62+
}
63+
5764
@Override
5865
public void print(Object s) {
5966
if (duplicate)

0 commit comments

Comments
 (0)