Skip to content

Commit 3f0256a

Browse files
committed
Add Eval.toEnvPropertyKey()
Supports the `${my.prop}` -> MY_PROP environment variable case
1 parent d6f025f commit 3f0256a

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,60 @@ logger.timestampPattern=ISO_OFFSET_DATE_TIME
8989

9090
```
9191

92+
## Structured JSON - logger.format=json
93+
94+
By default, the log format is JSON. Example:
95+
96+
```json
97+
{
98+
"component":"my-application",
99+
"env":"dev",
100+
"timestamp":"2025-07-14T13:44:44.230959+12:00",
101+
"level":"TRACE",
102+
"logger":"io.avaje.config",
103+
"message":"load from [resource:application-test.properties]",
104+
"thread":"main"
105+
}
106+
```
107+
108+
#### component
109+
110+
A `component` key value is added if:
111+
- There is a `logger.component` property set in `avaje-logger.properties`
112+
- There is a `COMPONENT` environment variable set
113+
- K8s is detected, it will be derived from the `HOSTNAME` environment variable
114+
115+
This key value is expected to represent the application component that is the source of
116+
the logs.
117+
118+
Examples:
119+
```properties
120+
## a literal value
121+
logger.component=my-application
122+
123+
## uses system property `SERVICE_NAME` or environment variable `SERVICE_NAME`
124+
logger.component=${SERVICE_NAME}
125+
126+
## uses system property `service.name` or environment variable `SERVICE_NAME`
127+
logger.component=${service.name}
128+
129+
```
130+
131+
#### env - Environment
132+
133+
An `env` key value is added automatically if:
134+
- There is a `logger.environment` property set in `avaje-logger.properties`
135+
- There is an `ENVIRONMENT` environment variable set
136+
137+
Examples:
138+
```properties
139+
## uses system property `app.env` or environment variable `APP_ENV`, defaults to `localdev`
140+
logger.environment=${app.env:localdev}
141+
142+
## literal value
143+
logger.environment=DEV
144+
```
145+
92146

93147
## Dynamic log level configuration
94148

avaje-simple-json-logger/src/main/java/io/avaje/simplelogger/encoder/Eval.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,18 @@ static String eval(String value) {
6969
if (val != null) {
7070
return val;
7171
}
72+
val = System.getenv(toEnvPropertyKey(key));
73+
if (val != null) {
74+
return val;
75+
}
7276
return split.length == 2 ? split[1] : value;
7377
}
7478

7579
static String toSystemPropertyKey(String key) {
7680
return key.replace('_', '.').toLowerCase();
7781
}
82+
83+
static String toEnvPropertyKey(String key) {
84+
return key.replace('.', '_').toUpperCase();
85+
}
7886
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.avaje.simplelogger.encoder;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class EvalTest {
8+
9+
@Test
10+
void k8sComponent() {
11+
assertEquals("my-component", Eval.k8sComponent("my-component-part0-part1"));
12+
}
13+
14+
@Test
15+
void eval() {
16+
String expected = System.getProperty("user.home");
17+
assertEquals(expected, Eval.eval("${user.home:someDefault}"));
18+
assertEquals(expected, Eval.eval("${user.home}"));
19+
}
20+
21+
@Test
22+
void eval_expect_defaultValue() {
23+
assertEquals("someDefault", Eval.eval("${user.homeDoesNotExist:someDefault}"));
24+
}
25+
26+
@Test
27+
void toSystemPropertyKey() {
28+
assertEquals("my.prop", Eval.toSystemPropertyKey("MY_PROP"));
29+
assertEquals("my.prop", Eval.toSystemPropertyKey("my_prop"));
30+
assertEquals("my.prop", Eval.toSystemPropertyKey("my.prop"));
31+
32+
}
33+
34+
@Test
35+
void toEnvPropertyKey() {
36+
assertEquals("MY_PROP", Eval.toEnvPropertyKey("MY_PROP"));
37+
assertEquals("MY_PROP", Eval.toEnvPropertyKey("my_prop"));
38+
assertEquals("MY_PROP", Eval.toEnvPropertyKey("my.prop"));
39+
}
40+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## can set config including log levels here
22

3+
logger.environment=${env:dev}
4+
logger.component=${my.prop:my-app}
5+
36
log.level.org.foo=trace
47
log.level.io.avaje.config=trace
58
log.level.io.avaje.simplelogger=debug

0 commit comments

Comments
 (0)