Skip to content

Commit

Permalink
Trim all configuration values for writers
Browse files Browse the repository at this point in the history
  • Loading branch information
pmwmedia committed Sep 23, 2021
1 parent 09438e2 commit 413d961
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,25 @@
/**
* Base writer for outputting log entries into files.
*/
public abstract class AbstractFileBasedWriter implements Writer {
public abstract class AbstractFileBasedWriter extends AbstractWriter {

/** */
protected AbstractFileBasedWriter() {
/**
* @param properties
* Configuration for writer
*/
protected AbstractFileBasedWriter(final Map<String, String> properties) {
super(properties);
}

/**
* Extracts the log file name from configuration.
*
* @param properties
* Configuration for writer
* @return Log file name
* @throws IllegalArgumentException
* Log file is not defined in configuration
*/
protected static String getFileName(final Map<String, String> properties) {
String fileName = properties.get("file");
protected String getFileName() {
String fileName = getStringValue("file");
if (fileName == null) {
throw new IllegalArgumentException("File name is missing for writer");
} else {
Expand All @@ -61,14 +63,12 @@ protected static String getFileName(final Map<String, String> properties) {
* Extracts the charset from configuration. The default charset will be returned, if no charset is defined or the
* defined charset doesn't exist.
*
* @param properties
* Configuration for writer
* @return Configured charset
*/
protected static Charset getCharset(final Map<String, String> properties) {
String charsetName = properties.get("charset");
protected Charset getCharset() {
String charsetName = getStringValue("charset");
try {
return charsetName == null ? Charset.defaultCharset() : Charset.forName(charsetName.trim());
return charsetName == null ? Charset.defaultCharset() : Charset.forName(charsetName);
} catch (IllegalArgumentException ex) {
InternalLogger.log(Level.ERROR, "Invalid charset: " + charsetName);
return Charset.defaultCharset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public abstract class AbstractFormatPatternWriter extends AbstractFileBasedWrite
* Configuration for writer
*/
public AbstractFormatPatternWriter(final Map<String, String> properties) {
String pattern = properties.get("format");
super(properties);

String pattern = getStringValue("format");
if (pattern == null) {
pattern = DEFAULT_FORMAT_PATTERN;
}

token = new FormatPatternParser(properties.get("exception")).parse(pattern + NEW_LINE);
builder = Boolean.parseBoolean(properties.get("writingthread")) ? new StringBuilder(BUILDER_CAPACITY) : null;
token = new FormatPatternParser(getStringValue("exception")).parse(pattern + NEW_LINE);
builder = getBooleanValue("writingthread") ? new StringBuilder(BUILDER_CAPACITY) : null;
}

/**
Expand Down
63 changes: 63 additions & 0 deletions tinylog-impl/src/main/java/org/tinylog/writers/AbstractWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2021 Martin Winandy
*
* 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.tinylog.writers;

import java.util.Map;

public abstract class AbstractWriter implements Writer {

private final Map<String, String> properties;

/**
* @param properties
* Configuration for writer
*/
public AbstractWriter(final Map<String, String> properties) {
this.properties = properties;
}

/**
* Gets the trimmed value for the passed key from the configuration properties.
*
* <p>
* Leading and trailing spaces of the found value will be removed.
* </p>
*
* @param key Case-sensitive property key
* @return Found value or {@code null}
*/
public String getStringValue(final String key) {
String value = properties.get(key);
if (value == null) {
return null;
} else {
return value.trim();
}
}

/**
* Gets the boolean value for the passed key from the configuration properties.
*
* <p>
* Under the hood, {@link Boolean#parseBoolean(String)} is used with the trimmed string value.
* </p>
*
* @param key Case-sensitive property key
* @return Found boolean value
*/
public boolean getBooleanValue(final String key) {
return Boolean.parseBoolean(getStringValue(key));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ConsoleWriter(final Map<String, String> properties) {
Level levelStream = Level.WARN;

// Check stream property
String stream = properties.get("stream");
String stream = getStringValue("stream");
if (stream != null) {
// Check whether we have the err@LEVEL syntax
String[] streams = stream.split("@", 2);
Expand Down
10 changes: 5 additions & 5 deletions tinylog-impl/src/main/java/org/tinylog/writers/FileWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public FileWriter() throws IOException {
public FileWriter(final Map<String, String> properties) throws IOException {
super(properties);

String fileName = getFileName(properties);
boolean append = Boolean.parseBoolean(properties.get("append"));
boolean buffered = Boolean.parseBoolean(properties.get("buffered"));
boolean writingThread = Boolean.parseBoolean(properties.get("writingthread"));
String fileName = getFileName();
boolean append = getBooleanValue("append");
boolean buffered = getBooleanValue("buffered");
boolean writingThread = getBooleanValue("writingthread");

charset = getCharset(properties);
charset = getCharset();
writer = createByteArrayWriter(fileName, append, buffered, !writingThread, false, charset);
}

Expand Down
24 changes: 12 additions & 12 deletions tinylog-impl/src/main/java/org/tinylog/writers/JdbcWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
import org.tinylog.provider.InternalLogger;

/**
* Writer for inserting log entries into a SQL database table.
* Writer for inserting log entries into an SQL database table.
*/
public final class JdbcWriter implements Writer {
public final class JdbcWriter extends AbstractWriter {

private static final String FIELD_PREFIX = "field.";
private static final long MAX_BATCH_SIZE = 100;
Expand Down Expand Up @@ -82,13 +82,15 @@ public JdbcWriter() throws NamingException, SQLException {
* Database connection cannot be established
*/
public JdbcWriter(final Map<String, String> properties) throws NamingException, SQLException {
url = getUrl(properties);
user = properties.get("user");
password = properties.get("password");
reconnect = Boolean.parseBoolean(properties.get("reconnect"));
batch = Boolean.parseBoolean(properties.get("batch"));
super(properties);

mutex = Boolean.parseBoolean(properties.get("writingthread")) ? null : new Object();
url = getUrl();
user = getStringValue("user");
password = getStringValue("password");
reconnect = getBooleanValue("reconnect");
batch = getBooleanValue("batch");

mutex = getBooleanValue("writingthread") ? null : new Object();

connection = connect(url, user, password);
sql = renderSql(properties, connection.getMetaData().getIdentifierQuoteString());
Expand Down Expand Up @@ -323,15 +325,13 @@ private static Connection connect(final String url, final String user, final Str
/**
* Extracts the URL to database or data source from configuration.
*
* @param properties
* Configuration for writer
* @return Connection URL
*
* @throws IllegalArgumentException
* URL is not defined in configuration
*/
private static String getUrl(final Map<String, String> properties) {
String url = properties.get("url");
private String getUrl() {
String url = getStringValue("url");
if (url == null) {
throw new IllegalArgumentException("URL is missing for JDBC writer");
} else {
Expand Down
12 changes: 7 additions & 5 deletions tinylog-impl/src/main/java/org/tinylog/writers/JsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ public JsonWriter() throws IOException {
* @throws IllegalArgumentException Log file is not defined in configuration
*/
public JsonWriter(final Map<String, String> properties) throws IOException {
String fileName = getFileName(properties);
boolean append = Boolean.parseBoolean(properties.get("append"));
boolean buffered = Boolean.parseBoolean(properties.get("buffered"));
boolean writingThread = Boolean.parseBoolean(properties.get("writingthread"));
super(properties);

charset = getCharset(properties);
String fileName = getFileName();
boolean append = getBooleanValue("append");
boolean buffered = getBooleanValue("buffered");
boolean writingThread = getBooleanValue("writingthread");

charset = getCharset();
writer = createByteArrayWriter(fileName, append, buffered, !writingThread, false, charset);

byte[] charsetHeader = getCharsetHeader(charset);
Expand Down
14 changes: 8 additions & 6 deletions tinylog-impl/src/main/java/org/tinylog/writers/LogcatWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/**
* Writer for redirecting log entries to Android's logcat.
*/
public final class LogcatWriter implements Writer {
public final class LogcatWriter extends AbstractWriter {

private static final String DEFAULT_TAG_FORMAT_PATTERN = "{class-name}";
private static final String DEFAULT_MESSAGE_FORMAT_PATTERN = "{message}";
Expand All @@ -55,10 +55,12 @@ public LogcatWriter() {
* Configuration for writer
*/
public LogcatWriter(final Map<String, String> properties) {
FormatPatternParser parser = new FormatPatternParser(properties.get("exception"));
boolean hasWritingThread = Boolean.parseBoolean(properties.get("writingthread"));
super(properties);

String tagPattern = properties.get("tagname");
FormatPatternParser parser = new FormatPatternParser(getStringValue("exception"));
boolean hasWritingThread = getBooleanValue("writingthread");

String tagPattern = getStringValue("tagname");
if (tagPattern == null) {
tagPattern = DEFAULT_TAG_FORMAT_PATTERN;
}
Expand Down Expand Up @@ -130,7 +132,7 @@ public void close() {
* Log entry for rendering tag
* @return Rendered tag
*/
protected String renderTag(final LogEntry logEntry) {
private String renderTag(final LogEntry logEntry) {
StringBuilder builder = reuseOrCreate(tagBuilder, TAG_MAX_LENGTH);
tagToken.render(logEntry, builder);

Expand All @@ -148,7 +150,7 @@ protected String renderTag(final LogEntry logEntry) {
* Log entry for rendering log message
* @return Rendered log message
*/
protected String renderMessage(final LogEntry logEntry) {
private String renderMessage(final LogEntry logEntry) {
StringBuilder builder = reuseOrCreate(messageBuilder, MESSAGE_BUILDER_CAPACITY);
messageToken.render(logEntry, builder);
return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public RollingFileWriter() throws IOException {
public RollingFileWriter(final Map<String, String> properties) throws IOException {
super(properties);

path = new DynamicPath(getFileName(properties));
policies = createPolicies(properties.get("policies"));
converter = createConverter(properties.get("convert"));
backups = properties.containsKey("backups") ? Integer.parseInt(properties.get("backups")) : -1;
linkToLatest = properties.containsKey("latest") ? new DynamicPath(properties.get("latest")) : null;
path = new DynamicPath(getFileName());
policies = createPolicies(getStringValue("policies"));
converter = createConverter(getStringValue("convert"));
backups = properties.containsKey("backups") ? Integer.parseInt(getStringValue("backups")) : -1;
linkToLatest = properties.containsKey("latest") ? new DynamicPath(getStringValue("latest")) : null;

List<FileTuple> files = getAllFileTuplesWithoutLinks(converter.getBackupSuffix());
File latestFile = findLatestLogFile(files);
Expand All @@ -106,9 +106,9 @@ public RollingFileWriter(final Map<String, String> properties) throws IOExceptio
append = false;
}

charset = getCharset(properties);
buffered = Boolean.parseBoolean(properties.get("buffered"));
writingThread = Boolean.parseBoolean(properties.get("writingthread"));
charset = getCharset();
buffered = getBooleanValue("buffered");
writingThread = getBooleanValue("writingthread");
writer = createByteArrayWriterAndLinkLatest(fileName, append, buffered, charset);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public SharedFileWriter() throws IOException {
public SharedFileWriter(final Map<String, String> properties) throws IOException {
super(properties);

String fileName = getFileName(properties);
boolean append = Boolean.parseBoolean(properties.get("append"));
boolean buffered = Boolean.parseBoolean(properties.get("buffered"));
boolean writingThread = Boolean.parseBoolean(properties.get("writingthread"));
String fileName = getFileName();
boolean append = getBooleanValue("append");
boolean buffered = getBooleanValue("buffered");
boolean writingThread = getBooleanValue("writingthread");

if (append) {
lockFile = null;
Expand All @@ -87,7 +87,7 @@ public SharedFileWriter(final Map<String, String> properties) throws IOException
}
}

charset = getCharset(properties);
charset = getCharset();
writer = createByteArrayWriter(fileName, append, buffered, !writingThread, true, charset);
}

Expand Down

0 comments on commit 413d961

Please sign in to comment.