Skip to content

Commit dbc25dc

Browse files
Release 1.3.0: Adds support for setting properties file via java.util.logging.config.file property
1 parent d2ab3f8 commit dbc25dc

7 files changed

Lines changed: 219 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea/**
22
target/**
33
.flattened-pom.xml
4+
jla_04.iml

src/main/java/com/teragrep/jla_04/RelpConfig.java

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@
1717

1818
package com.teragrep.jla_04;
1919

20+
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.FileNotFoundException;
23+
import java.io.IOException;
2024
import java.net.InetAddress;
2125
import java.net.UnknownHostException;
26+
import java.util.logging.Formatter;
27+
import java.util.logging.LogManager;
28+
import java.util.logging.LogRecord;
29+
import java.util.logging.SimpleFormatter;
2230

2331
public class RelpConfig {
2432
private int port; // Relp server port
@@ -32,11 +40,15 @@ public class RelpConfig {
3240
private int reconnectInterval; // sleep between relp connection reconnects
3341
private int readTimeout; // relp connection reading timeout
3442
private int writeTimeout; // relp connection writing timeout
43+
private LogManager manager;
44+
private Formatter formatter;
3545

36-
public RelpConfig(String name) throws NumberFormatException, IllegalArgumentException, NoSuchFieldException {
46+
public RelpConfig(String name) throws NumberFormatException, IllegalArgumentException, NoSuchFieldException, ClassNotFoundException, InstantiationException, IllegalAccessException {
47+
initLogger();
3748
// Name must be set first, others do not depend on the ordering
3849
initName(name);
3950
// The rest
51+
initFormatter();
4052
initPort();
4153
initAddress();
4254
initAppname();
@@ -49,15 +61,71 @@ public RelpConfig(String name) throws NumberFormatException, IllegalArgumentExce
4961
initWriteTimeout();
5062
}
5163

64+
private void initLogger() {
65+
this.manager = LogManager.getLogManager();
66+
String configpath = System.getProperty("java.util.logging.config.file");
67+
if(configpath != null) {
68+
File configfile = new File(configpath);
69+
if(!configfile.exists() || !configfile.isFile()) {
70+
System.out.println("Can't find properties file at " + configpath);
71+
return;
72+
}
73+
FileInputStream inputStream = null;
74+
try {
75+
inputStream = new FileInputStream(configpath);
76+
LogManager.getLogManager().readConfiguration(inputStream);
77+
} catch (Exception e) {
78+
e.printStackTrace();
79+
}
80+
}
81+
}
82+
83+
private void initFormatter() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
84+
// Get normal prop and fallback to manager prop; from that fallback to simple formatter
85+
String formatter_name = System.getProperty("java.util.logging.RelpHandler." + this.getName() + ".formatter", this.manager.getProperty("java.util.logging.RelpHandler." + this.getName() + ".formatter"));
86+
if(formatter_name != null) {
87+
ClassLoader classloader = ClassLoader.getSystemClassLoader();
88+
if (classloader == null) {
89+
System.out.println("Unable to initialize ClassLoader.getSystemClassLoader(), defaulting to SimpleFormatter");
90+
}
91+
if (classloader != null) {
92+
Object formatter_object = classloader.loadClass(formatter_name).newInstance();
93+
this.formatter = (Formatter) formatter_object;
94+
}
95+
}
96+
else {
97+
this.formatter = new SimpleFormatter() {
98+
@Override
99+
public synchronized String format(LogRecord logrecord) {
100+
return String.format("%1$s", logrecord.getMessage());
101+
}
102+
};
103+
}
104+
}
105+
106+
public Formatter getFormatter() {
107+
return this.formatter;
108+
}
109+
52110
private String getProperty(String name, String fallback) {
111+
// Overrides from props
53112
String prop = System.getProperty("java.util.logging.RelpHandler." + this.getName() + "." + name);
54-
if (prop == null) {
55-
return fallback;
113+
if (prop != null) {
114+
if(prop.equals("")) {
115+
throw new IllegalArgumentException("Field is set but has no value: java.util.logging.RelpHandler." + this.getName() + "." + name);
116+
}
117+
return prop;
56118
}
57-
if (prop.equals("")) {
58-
throw new IllegalArgumentException("Field is set but has no value: java.util.logging.RelpHandler." + this.getName() + "." + name);
119+
// Check if values are set in prop file
120+
String from_prop = this.manager.getProperty("java.util.logging.RelpHandler." + this.getName() + "." + name);
121+
if (from_prop != null) {
122+
if(from_prop.equals("")) {
123+
throw new IllegalArgumentException("Field is set but has no value: java.util.logging.RelpHandler." + this.getName() + "." + name);
124+
}
125+
return from_prop;
59126
}
60-
return prop;
127+
// Default
128+
return fallback;
61129
}
62130

63131
private void initName(String name) throws NoSuchFieldException, IllegalArgumentException {

src/main/java/com/teragrep/jla_04/RelpHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public synchronized void publish(LogRecord logRecord) {
102102
.withAppName(this.config.getAppname())
103103
.withHostname(this.config.getHostname())
104104
.withFacility(Facility.USER)
105-
.withMsg(this.formatter.format(logRecord));
105+
.withMsg(this.config.getFormatter().format(logRecord));
106106

107107
// Add SD if enabled
108108
if(this.config.getUseSD()) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Java Util Logging RELP Handler
3+
Copyright (C) 2021 Suomen Kanuuna Oy
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package com.teragrep.jla_04;
19+
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.DisplayName;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.io.FileInputStream;
25+
import java.io.FileNotFoundException;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
import java.util.logging.LogManager;
29+
import java.util.logging.Logger;
30+
31+
public class PrebuiltTest {
32+
static final Logger logger = Logger.getLogger(RelpHandler.class.getName());
33+
//@Test
34+
@DisplayName("Tests all from properties")
35+
public void testFullyFromProperties() {
36+
Assertions.assertDoesNotThrow(() -> {
37+
LogManager.getLogManager().reset();
38+
InputStream inputStream = null;
39+
try {
40+
inputStream = new FileInputStream(System.getProperty("user.dir") + "/src/test/java/com/teragrep/jla_04/fully.properties");
41+
} catch (FileNotFoundException e) {
42+
e.printStackTrace();
43+
}
44+
try {
45+
LogManager.getLogManager().readConfiguration(inputStream);
46+
}
47+
catch (IOException e) {
48+
System.out.println("Can't read conf/logging.properties: " + e.getMessage());
49+
}
50+
51+
logger.info("I should be from fully.properties");
52+
});
53+
}
54+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Java Util Logging RELP Handler
3+
Copyright (C) 2021 Suomen Kanuuna Oy
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package com.teragrep.jla_04;
19+
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.DisplayName;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.util.logging.LogManager;
25+
import java.util.logging.Logger;
26+
27+
public class PropertiesFileTest {
28+
static final Logger logger = Logger.getLogger(RelpHandler.class.getName());
29+
30+
//@Test
31+
@DisplayName("Tests reading properties file")
32+
public void testReadPropertiesFile() {
33+
Assertions.assertDoesNotThrow(() -> {
34+
System.setProperty("java.util.logging.config.file", System.getProperty("user.dir") + "/src/test/java/com/teragrep/jla_04//logging.properties");
35+
RelpHandler handler = new RelpHandler("fromprops");
36+
LogManager.getLogManager().reset();
37+
logger.addHandler(handler);
38+
logger.info("I am from properties file");
39+
});
40+
41+
}
42+
43+
//@Test
44+
@DisplayName("Tests reading properties file and overriding values")
45+
public void testOverridePropertiesFile() {
46+
Assertions.assertDoesNotThrow(() -> {
47+
System.setProperty("java.util.logging.config.file", System.getProperty("user.dir") + "/src/test/java/com/teragrep/jla_04//logging.properties");
48+
System.setProperty("java.util.logging.RelpHandler.fromprops-override.hostname", "fromprops-hostname-overridden");
49+
RelpHandler handler = new RelpHandler("fromprops-override");
50+
LogManager.getLogManager().reset();
51+
logger.addHandler(handler);
52+
logger.info("I am from properties file but overridden");
53+
});
54+
}
55+
56+
//@Test
57+
@DisplayName("Tests reading properties file and getting formatter")
58+
public void testGettingFormatter() {
59+
Assertions.assertDoesNotThrow(() -> {
60+
System.setProperty("java.util.logging.config.file", System.getProperty("user.dir") + "/src/test/java/com/teragrep/jla_04//logging.properties");
61+
RelpHandler handler = new RelpHandler("fromprops-formatter");
62+
LogManager.getLogManager().reset();
63+
logger.addHandler(handler);
64+
logger.info("My formatter should be from logging.properties");
65+
});
66+
}
67+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
java.util.logging.RelpHandler.default.server.address=127.0.0.1
2+
java.util.logging.RelpHandler.default.server.port=1666
3+
java.util.logging.RelpHandler.default.appname=from-fully-tag
4+
java.util.logging.RelpHandler.default.hostname=from-fully-host
5+
java.util.logging.RelpHandler.default.formatter=java.util.logging.SimpleFormatter
6+
java.util.logging.SimpleFormatter.format=from-fully: [%4$s]: %5$s [%1$tc]
7+
handlers = com.teragrep.jla_04.RelpHandler
8+
.level = ALL
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
java.util.logging.RelpHandler.fromprops.server.address=127.0.0.1
2+
java.util.logging.RelpHandler.fromprops.server.port=1666
3+
java.util.logging.RelpHandler.fromprops.appname=fromprops-tag
4+
java.util.logging.RelpHandler.fromprops.hostname=fromprops-host
5+
java.util.logging.RelpHandler.fromprops-override.server.address=127.0.0.1
6+
java.util.logging.RelpHandler.fromprops-override.server.port=1666
7+
java.util.logging.RelpHandler.fromprops-override.appname=fromprops-override-but-default-tag
8+
java.util.logging.RelpHandler.fromprops-override.hostname=fromprops-override-me-host
9+
java.util.logging.RelpHandler.fromprops-formatter.server.address=127.0.0.1
10+
java.util.logging.RelpHandler.fromprops-formatter.server.port=1666
11+
java.util.logging.RelpHandler.fromprops-formatter.appname=fromprops-formatter-tag
12+
java.util.logging.RelpHandler.fromprops-formatter.hostname=fromprops-formatter-host
13+
java.util.logging.RelpHandler.fromprops-formatter.formatter=java.util.logging.SimpleFormatter
14+
java.util.logging.SimpleFormatter.format=fromprops-formatter: [%4$s]: %5$s [%1$tc]

0 commit comments

Comments
 (0)