tinylog-org / tinylog

@@ -77,11 +77,11 @@
Loading
77 77
	public RollingFileWriter(final Map<String, String> properties) throws IOException {
78 78
		super(properties);
79 79
80 -
		path = new DynamicPath(getFileName(properties));
81 -
		policies = createPolicies(properties.get("policies"));
82 -
		converter = createConverter(properties.get("convert"));
83 -
		backups = properties.containsKey("backups") ? Integer.parseInt(properties.get("backups")) : -1;
84 -
		linkToLatest = properties.containsKey("latest") ? new DynamicPath(properties.get("latest")) : null;
80 +
		path = new DynamicPath(getFileName());
81 +
		policies = createPolicies(getStringValue("policies"));
82 +
		converter = createConverter(getStringValue("convert"));
83 +
		backups = properties.containsKey("backups") ? Integer.parseInt(getStringValue("backups")) : -1;
84 +
		linkToLatest = properties.containsKey("latest") ? new DynamicPath(getStringValue("latest")) : null;
85 85
86 86
		List<FileTuple> files = getAllFileTuplesWithoutLinks(converter.getBackupSuffix());
87 87
		File latestFile = findLatestLogFile(files);
@@ -106,9 +106,9 @@
Loading
106 106
			append = false;
107 107
		}
108 108
109 -
		charset = getCharset(properties);
110 -
		buffered = Boolean.parseBoolean(properties.get("buffered"));
111 -
		writingThread = Boolean.parseBoolean(properties.get("writingthread"));
109 +
		charset = getCharset();
110 +
		buffered = getBooleanValue("buffered");
111 +
		writingThread = getBooleanValue("writingthread");
112 112
		writer = createByteArrayWriterAndLinkLatest(fileName, append, buffered, charset);
113 113
	}
114 114

@@ -30,7 +30,7 @@
Loading
30 30
/**
31 31
 * Writer for redirecting log entries to Android's logcat.
32 32
 */
33 -
public final class LogcatWriter implements Writer {
33 +
public final class LogcatWriter extends AbstractWriter {
34 34
35 35
	private static final String DEFAULT_TAG_FORMAT_PATTERN = "{class-name}";
36 36
	private static final String DEFAULT_MESSAGE_FORMAT_PATTERN = "{message}";
@@ -55,10 +55,12 @@
Loading
55 55
	 *            Configuration for writer
56 56
	 */
57 57
	public LogcatWriter(final Map<String, String> properties) {
58 -
		FormatPatternParser parser = new FormatPatternParser(properties.get("exception"));
59 -
		boolean hasWritingThread = Boolean.parseBoolean(properties.get("writingthread"));
58 +
		super(properties);
60 59
61 -
		String tagPattern = properties.get("tagname");
60 +
		FormatPatternParser parser = new FormatPatternParser(getStringValue("exception"));
61 +
		boolean hasWritingThread = getBooleanValue("writingthread");
62 +
63 +
		String tagPattern = getStringValue("tagname");
62 64
		if (tagPattern == null) {
63 65
			tagPattern = DEFAULT_TAG_FORMAT_PATTERN;
64 66
		}
@@ -130,7 +132,7 @@
Loading
130 132
	 *            Log entry for rendering tag
131 133
	 * @return Rendered tag
132 134
	 */
133 -
	protected String renderTag(final LogEntry logEntry) {
135 +
	private String renderTag(final LogEntry logEntry) {
134 136
		StringBuilder builder = reuseOrCreate(tagBuilder, TAG_MAX_LENGTH);
135 137
		tagToken.render(logEntry, builder);
136 138
@@ -148,7 +150,7 @@
Loading
148 150
	 *            Log entry for rendering log message
149 151
	 * @return Rendered log message
150 152
	 */
151 -
	protected String renderMessage(final LogEntry logEntry) {
153 +
	private String renderMessage(final LogEntry logEntry) {
152 154
		StringBuilder builder = reuseOrCreate(messageBuilder, MESSAGE_BUILDER_CAPACITY);
153 155
		messageToken.render(logEntry, builder);
154 156
		return builder.toString();

@@ -61,10 +61,10 @@
Loading
61 61
	public SharedFileWriter(final Map<String, String> properties) throws IOException {
62 62
		super(properties);
63 63
64 -
		String fileName = getFileName(properties);
65 -
		boolean append = Boolean.parseBoolean(properties.get("append"));
66 -
		boolean buffered = Boolean.parseBoolean(properties.get("buffered"));
67 -
		boolean writingThread = Boolean.parseBoolean(properties.get("writingthread"));
64 +
		String fileName = getFileName();
65 +
		boolean append = getBooleanValue("append");
66 +
		boolean buffered = getBooleanValue("buffered");
67 +
		boolean writingThread = getBooleanValue("writingthread");
68 68
69 69
		if (append) {
70 70
			lockFile = null;
@@ -87,7 +87,7 @@
Loading
87 87
			}
88 88
		}
89 89
90 -
		charset = getCharset(properties);
90 +
		charset = getCharset();
91 91
		writer = createByteArrayWriter(fileName, append, buffered, !writingThread, true, charset);
92 92
	}
93 93

@@ -38,13 +38,15 @@
Loading
38 38
	 *            Configuration for writer
39 39
	 */
40 40
	public AbstractFormatPatternWriter(final Map<String, String> properties) {
41 -
		String pattern = properties.get("format");
41 +
		super(properties);
42 +
43 +
		String pattern = getStringValue("format");
42 44
		if (pattern == null) {
43 45
			pattern = DEFAULT_FORMAT_PATTERN;
44 46
		}
45 47
46 -
		token = new FormatPatternParser(properties.get("exception")).parse(pattern + NEW_LINE);
47 -
		builder = Boolean.parseBoolean(properties.get("writingthread")) ? new StringBuilder(BUILDER_CAPACITY) : null;
48 +
		token = new FormatPatternParser(getStringValue("exception")).parse(pattern + NEW_LINE);
49 +
		builder = getBooleanValue("writingthread") ? new StringBuilder(BUILDER_CAPACITY) : null;
48 50
	}
49 51
50 52
	/**

@@ -63,12 +63,14 @@
Loading
63 63
	 * @throws IllegalArgumentException Log file is not defined in configuration
64 64
	 */
65 65
	public JsonWriter(final Map<String, String> properties) throws IOException {
66 -
		String fileName = getFileName(properties);
67 -
		boolean append = Boolean.parseBoolean(properties.get("append"));
68 -
		boolean buffered = Boolean.parseBoolean(properties.get("buffered"));
69 -
		boolean writingThread = Boolean.parseBoolean(properties.get("writingthread"));
66 +
		super(properties);
70 67
71 -
		charset = getCharset(properties);
68 +
		String fileName = getFileName();
69 +
		boolean append = getBooleanValue("append");
70 +
		boolean buffered = getBooleanValue("buffered");
71 +
		boolean writingThread = getBooleanValue("writingthread");
72 +
73 +
		charset = getCharset();
72 74
		writer = createByteArrayWriter(fileName, append, buffered, !writingThread, false, charset);
73 75
74 76
		byte[] charsetHeader = getCharsetHeader(charset);

@@ -52,12 +52,12 @@
Loading
52 52
	public FileWriter(final Map<String, String> properties) throws IOException {
53 53
		super(properties);
54 54
55 -
		String fileName = getFileName(properties);
56 -
		boolean append = Boolean.parseBoolean(properties.get("append"));
57 -
		boolean buffered = Boolean.parseBoolean(properties.get("buffered"));
58 -
		boolean writingThread = Boolean.parseBoolean(properties.get("writingthread"));
55 +
		String fileName = getFileName();
56 +
		boolean append = getBooleanValue("append");
57 +
		boolean buffered = getBooleanValue("buffered");
58 +
		boolean writingThread = getBooleanValue("writingthread");
59 59
60 -
		charset = getCharset(properties);
60 +
		charset = getCharset();
61 61
		writer = createByteArrayWriter(fileName, append, buffered, !writingThread, false, charset);
62 62
	}
63 63

@@ -0,0 +1,63 @@
Loading
1 +
/*
2 +
 * Copyright 2021 Martin Winandy
3 +
 *
4 +
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 +
 * the License. You may obtain a copy of the License at
6 +
 *
7 +
 * http://www.apache.org/licenses/LICENSE-2.0
8 +
 *
9 +
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 +
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 +
 * specific language governing permissions and limitations under the License.
12 +
 */
13 +
14 +
package org.tinylog.writers;
15 +
16 +
import java.util.Map;
17 +
18 +
public abstract class AbstractWriter implements Writer {
19 +
20 +
	private final Map<String, String> properties;
21 +
22 +
	/**
23 +
	 * @param properties
24 +
	 *            Configuration for writer
25 +
	 */
26 +
	public AbstractWriter(final Map<String, String> properties) {
27 +
		this.properties = properties;
28 +
	}
29 +
30 +
	/**
31 +
	 * Gets the trimmed value for the passed key from the configuration properties.
32 +
	 *
33 +
	 * <p>
34 +
	 *     Leading and trailing spaces of the found value will be removed.
35 +
	 * </p>
36 +
	 *
37 +
	 * @param key Case-sensitive property key
38 +
	 * @return Found value or {@code null}
39 +
	 */
40 +
	public String getStringValue(final String key) {
41 +
		String value = properties.get(key);
42 +
		if (value == null) {
43 +
			return null;
44 +
		} else {
45 +
			return value.trim();
46 +
		}
47 +
	}
48 +
49 +
	/**
50 +
	 * Gets the boolean value for the passed key from the configuration properties.
51 +
	 *
52 +
	 * <p>
53 +
	 *     Under the hood, {@link Boolean#parseBoolean(String)} is used with the trimmed string value.
54 +
	 * </p>
55 +
	 *
56 +
	 * @param key Case-sensitive property key
57 +
	 * @return Found boolean value
58 +
	 */
59 +
	public boolean getBooleanValue(final String key) {
60 +
		return Boolean.parseBoolean(getStringValue(key));
61 +
	}
62 +
63 +
}

@@ -33,23 +33,25 @@
Loading
33 33
/**
34 34
 * Base writer for outputting log entries into files.
35 35
 */
36 -
public abstract class AbstractFileBasedWriter implements Writer {
36 +
public abstract class AbstractFileBasedWriter extends AbstractWriter {
37 37
38 -
	/** */
39 -
	protected AbstractFileBasedWriter() {
38 +
	/**
39 +
	 * @param properties
40 +
	 *            Configuration for writer
41 +
	 */
42 +
	protected AbstractFileBasedWriter(final Map<String, String> properties) {
43 +
		super(properties);
40 44
	}
41 45
42 46
	/**
43 47
	 * Extracts the log file name from configuration.
44 48
	 *
45 -
	 * @param properties
46 -
	 *            Configuration for writer
47 49
	 * @return Log file name
48 50
	 * @throws IllegalArgumentException
49 51
	 *             Log file is not defined in configuration
50 52
	 */
51 -
	protected static String getFileName(final Map<String, String> properties) {
52 -
		String fileName = properties.get("file");
53 +
	protected String getFileName() {
54 +
		String fileName = getStringValue("file");
53 55
		if (fileName == null) {
54 56
			throw new IllegalArgumentException("File name is missing for writer");
55 57
		} else {
@@ -61,14 +63,12 @@
Loading
61 63
	 * Extracts the charset from configuration. The default charset will be returned, if no charset is defined or the
62 64
	 * defined charset doesn't exist.
63 65
	 *
64 -
	 * @param properties
65 -
	 *            Configuration for writer
66 66
	 * @return Configured charset
67 67
	 */
68 -
	protected static Charset getCharset(final Map<String, String> properties) {
69 -
		String charsetName = properties.get("charset");
68 +
	protected Charset getCharset() {
69 +
		String charsetName = getStringValue("charset");
70 70
		try {
71 -
			return charsetName == null ? Charset.defaultCharset() : Charset.forName(charsetName.trim());
71 +
			return charsetName == null ? Charset.defaultCharset() : Charset.forName(charsetName);
72 72
		} catch (IllegalArgumentException ex) {
73 73
			InternalLogger.log(Level.ERROR, "Invalid charset: " + charsetName);
74 74
			return Charset.defaultCharset();

@@ -51,7 +51,7 @@
Loading
51 51
		Level levelStream = Level.WARN;
52 52
		
53 53
		// Check stream property
54 -
		String stream = properties.get("stream");
54 +
		String stream = getStringValue("stream");
55 55
		if (stream != null) {
56 56
			// Check whether we have the err@LEVEL syntax
57 57
			String[] streams = stream.split("@", 2);

@@ -38,9 +38,9 @@
Loading
38 38
import org.tinylog.provider.InternalLogger;
39 39
40 40
/**
41 -
 * Writer for inserting log entries into a SQL database table.
41 +
 * Writer for inserting log entries into an SQL database table.
42 42
 */
43 -
public final class JdbcWriter implements Writer {
43 +
public final class JdbcWriter extends AbstractWriter {
44 44
45 45
	private static final String FIELD_PREFIX = "field.";
46 46
	private static final long MAX_BATCH_SIZE = 100;
@@ -82,13 +82,15 @@
Loading
82 82
	 *             Database connection cannot be established
83 83
	 */
84 84
	public JdbcWriter(final Map<String, String> properties) throws NamingException, SQLException {
85 -
		url = getUrl(properties);
86 -
		user = properties.get("user");
87 -
		password = properties.get("password");
88 -
		reconnect = Boolean.parseBoolean(properties.get("reconnect"));
89 -
		batch = Boolean.parseBoolean(properties.get("batch"));
85 +
		super(properties);
90 86
91 -
		mutex = Boolean.parseBoolean(properties.get("writingthread")) ? null : new Object();
87 +
		url = getUrl();
88 +
		user = getStringValue("user");
89 +
		password = getStringValue("password");
90 +
		reconnect = getBooleanValue("reconnect");
91 +
		batch = getBooleanValue("batch");
92 +
93 +
		mutex = getBooleanValue("writingthread") ? null : new Object();
92 94
93 95
		connection = connect(url, user, password);
94 96
		sql = renderSql(properties, connection.getMetaData().getIdentifierQuoteString());
@@ -323,15 +325,13 @@
Loading
323 325
	/**
324 326
	 * Extracts the URL to database or data source from configuration.
325 327
	 *
326 -
	 * @param properties
327 -
	 *            Configuration for writer
328 328
	 * @return Connection URL
329 329
	 *
330 330
	 * @throws IllegalArgumentException
331 331
	 *             URL is not defined in configuration
332 332
	 */
333 -
	private static String getUrl(final Map<String, String> properties) {
334 -
		String url = properties.get("url");
333 +
	private String getUrl() {
334 +
		String url = getStringValue("url");
335 335
		if (url == null) {
336 336
			throw new IllegalArgumentException("URL is missing for JDBC writer");
337 337
		} else {
Files Complexity Coverage
jboss-tinylog/src/main/java/org/tinylog/jboss 98.47% 98.84%
jcl-tinylog/src/main/java/org/tinylog/jcl 88.52% 89.55%
jul-tinylog/src/main/java/org/tinylog/jul 89.47% 91.18%
log4j1.2-api/src/main/java/org/apache/log4j 72.78% 70.50%
slf4j-tinylog/src/main/java/org 93.33% 94.96%
tinylog-api-kotlin/src/main/kotlin/org/tinylog/kotlin 96.08% 95.70%
tinylog-api-scala/src/main/scala/org/tinylog/scala 100.00% 90.00%
tinylog-api/src/main/java/org/tinylog 94.38% 94.06%
tinylog-impl/src/main/java/org/tinylog 93.95% 96.04%
tinylog-jboss/src/main/java/org/tinylog/adapter/jboss 89.66% 91.84%
tinylog1.3-api/src/main/java/org/pmw/tinylog 99.16% 98.91%
...ylog-jul/src/main/java/org/tinylog/adapter/jul/JavaUtilLoggingProvider.java 85.00% 88.24%
Project Totals (132 files) 93.48% 94.37%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading