6
6
import java .net .http .HttpRequest ;
7
7
import java .net .http .HttpResponse ;
8
8
import java .time .Duration ;
9
- import java .util .logging .Level ;
10
- import java .util .logging .Logger ;
11
9
12
10
import com .influxdb .client .InfluxDBClient ;
13
11
import com .influxdb .client .InfluxDBClientFactory ;
14
12
import com .influxdb .client .WriteApi ;
15
13
import it .renvins .serverpulse .api .service .IDatabaseService ;
16
14
import it .renvins .serverpulse .common .config .DatabaseConfiguration ;
15
+ import it .renvins .serverpulse .common .logger .PulseLogger ;
17
16
import it .renvins .serverpulse .common .platform .Platform ;
18
17
import it .renvins .serverpulse .common .scheduler .Task ;
19
18
import it .renvins .serverpulse .common .scheduler .TaskScheduler ;
20
19
import lombok .Getter ;
21
20
22
21
public class DatabaseService implements IDatabaseService {
23
22
24
- private final Logger logger ;
23
+ private final PulseLogger logger ;
25
24
private final Platform platform ;
26
25
27
26
private final DatabaseConfiguration configuration ;
@@ -41,7 +40,7 @@ public class DatabaseService implements IDatabaseService {
41
40
private volatile boolean isConnected = false ;
42
41
private volatile int retryCount = 0 ;
43
42
44
- public DatabaseService (Logger logger , Platform platform , DatabaseConfiguration configuration , TaskScheduler scheduler ) {
43
+ public DatabaseService (PulseLogger logger , Platform platform , DatabaseConfiguration configuration , TaskScheduler scheduler ) {
45
44
this .logger = logger ;
46
45
this .platform = platform ;
47
46
@@ -57,7 +56,7 @@ public DatabaseService(Logger logger, Platform platform, DatabaseConfiguration c
57
56
@ Override
58
57
public void load () {
59
58
if (!checkConnectionData ()) {
60
- logger .severe ("InfluxDB connection data is missing or invalid. Disabling plugin..." );
59
+ logger .error ("InfluxDB connection data is missing or invalid. Disabling plugin..." );
61
60
platform .disable ();
62
61
return ;
63
62
}
@@ -78,13 +77,13 @@ public void unload() {
78
77
public boolean ping () {
79
78
String url = configuration .getHost ();
80
79
if (url == null || url .isEmpty ()) {
81
- logger .severe ("InfluxDB URL is missing for ping..." );
80
+ logger .error ("InfluxDB URL is missing for ping..." );
82
81
return false ;
83
82
}
84
83
85
84
// Ensure httpClient is initialized
86
85
if (this .httpClient == null ) {
87
- logger .severe ("HttpClient not initialized for ping..." );
86
+ logger .error ("HttpClient not initialized for ping..." );
88
87
this .httpClient = HttpClient .newBuilder ().connectTimeout (Duration .ofSeconds (10 )).build ();
89
88
}
90
89
@@ -97,7 +96,7 @@ public boolean ping() {
97
96
.timeout (Duration .ofSeconds (5 )) // Add timeout specific to ping
98
97
.build ();
99
98
} catch (IllegalArgumentException e ) {
100
- logger .log ( Level . SEVERE , "Invalid InfluxDB URL format for ping: " + url , e );
99
+ logger .error ( "Invalid InfluxDB URL format for ping: " + url , e );
101
100
return false ;
102
101
}
103
102
@@ -112,7 +111,7 @@ public boolean ping() {
112
111
logger .warning ("InfluxDB ping timed out: " + e .getMessage ());
113
112
return false ;
114
113
} catch (Exception e ) {
115
- logger .log ( Level . SEVERE , "Error during InfluxDB ping: " + e .getMessage (), e );
114
+ logger .error ( "Error during InfluxDB ping: " + e .getMessage (), e );
116
115
return false ;
117
116
}
118
117
}
@@ -170,7 +169,7 @@ private void connect() {
170
169
}
171
170
} catch (Exception e ) {
172
171
// Handle exceptions during InfluxDBClientFactory.create() or ping()
173
- logger .log ( Level . SEVERE , "Failed to connect or ping InfluxDB: " + e .getMessage ());
172
+ logger .error ( "Failed to connect or ping InfluxDB: " + e .getMessage ());
174
173
this .isConnected = false ;
175
174
if (client != null ) { // Ensure client is closed on exception
176
175
client .close ();
@@ -186,15 +185,15 @@ public void disconnect() {
186
185
try {
187
186
writeApi .close ();
188
187
} catch (Exception e ) {
189
- logger .log ( Level . WARNING , "Error closing InfluxDB WriteApi..." , e );
188
+ logger .error ( "Error closing InfluxDB WriteApi..." , e );
190
189
}
191
190
writeApi = null ;
192
191
}
193
192
if (client != null ) {
194
193
try {
195
194
client .close ();
196
195
} catch (Exception e ) {
197
- logger .log ( Level . WARNING , "Error closing InfluxDB Client..." , e );
196
+ logger .error ( "Error closing InfluxDB Client..." , e );
198
197
}
199
198
client = null ;
200
199
}
@@ -233,7 +232,7 @@ public synchronized void startRetryTaskIfNeeded() {
233
232
234
233
// Check retries *before* attempting connection
235
234
if (retryCount >= MAX_RETRIES ) {
236
- logger .severe ("Max connection retries (" + MAX_RETRIES + ") reached. Disabling ServerPulse metrics..." );
235
+ logger .error ("Max connection retries (" + MAX_RETRIES + ") reached. Disabling ServerPulse metrics..." );
237
236
stopRetryTask ();
238
237
disconnect (); // Clean up any partial connection
239
238
// Schedule plugin disable on main thread
@@ -275,19 +274,19 @@ private boolean checkConnectionData() {
275
274
276
275
boolean valid = true ;
277
276
if (url == null || url .isEmpty ()) {
278
- logger .severe ("Missing or empty 'metrics.influxdb.url' in config..." );
277
+ logger .error ("Missing or empty 'metrics.influxdb.url' in config..." );
279
278
valid = false ;
280
279
}
281
280
if (bucket == null || bucket .isEmpty ()) {
282
- logger .severe ("Missing or empty 'metrics.influxdb.bucket' in config..." );
281
+ logger .error ("Missing or empty 'metrics.influxdb.bucket' in config..." );
283
282
valid = false ;
284
283
}
285
284
if (org == null || org .isEmpty ()) {
286
- logger .severe ("Missing or empty 'metrics.influxdb.org' in config..." );
285
+ logger .error ("Missing or empty 'metrics.influxdb.org' in config..." );
287
286
valid = false ;
288
287
}
289
288
if (token == null || token .isEmpty () || token .equals ("my-token" )) {
290
- logger .severe ("Missing, empty, or default 'metrics.influxdb.token' in config..." );
289
+ logger .error ("Missing, empty, or default 'metrics.influxdb.token' in config..." );
291
290
valid = false ;
292
291
}
293
292
return valid ;
0 commit comments