Skip to content

Commit 7540781

Browse files
committed
use slf4j instead of log4j
in a library this is better, then the application can decide which logging implmentation to use. the log4j2.properties file was causing trouble for library users since this is used as the default config when found on the classpath
1 parent f70486d commit 7540781

File tree

5 files changed

+54
-90
lines changed

5 files changed

+54
-90
lines changed

pom.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@
2929
<version>1.46</version>
3030
</dependency>
3131
<dependency>
32-
<groupId>org.apache.logging.log4j</groupId>
33-
<artifactId>log4j-core</artifactId>
34-
<version>2.25.2</version>
32+
<groupId>org.slf4j</groupId>
33+
<artifactId>slf4j-api</artifactId>
34+
<version>2.0.17</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.slf4j</groupId>
38+
<artifactId>slf4j-simple</artifactId>
39+
<version>2.0.17</version>
3540
</dependency>
3641
<dependency>
3742
<groupId>org.gnu</groupId>
@@ -46,7 +51,7 @@
4651
<dependency>
4752
<groupId>commons-cli</groupId>
4853
<artifactId>commons-cli</artifactId>
49-
<version>1.10.0</version>
54+
<version>1.11.0</version>
5055
</dependency>
5156
<dependency>
5257
<groupId>org.junit.jupiter</groupId>

src/main/java/org/kopi/ebics/client/EbicsClient.java

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@
3737
import org.apache.commons.cli.CommandLine;
3838
import org.apache.commons.cli.CommandLineParser;
3939
import org.apache.commons.cli.DefaultParser;
40-
import org.apache.commons.cli.HelpFormatter;
40+
4141
import org.apache.commons.cli.Options;
4242
import org.apache.commons.cli.ParseException;
43-
import org.apache.logging.log4j.LogManager;
44-
import org.apache.logging.log4j.Logger;
43+
import org.apache.commons.cli.help.HelpFormatter;
4544
import org.bouncycastle.jce.provider.BouncyCastleProvider;
4645
import org.kopi.ebics.exception.EbicsException;
4746
import org.kopi.ebics.exception.NoDownloadDataAvailableException;
@@ -60,6 +59,8 @@
6059
import org.kopi.ebics.session.OrderType;
6160
import org.kopi.ebics.session.Product;
6261
import org.kopi.ebics.utils.Constants;
62+
import org.slf4j.Logger;
63+
import org.slf4j.LoggerFactory;
6364

6465
/**
6566
* The ebics client application. Performs necessary tasks to contact the ebics
@@ -76,8 +77,8 @@ private static File getRootDir() {
7677
// this is for the logging config
7778
System.setProperty("ebicsBasePath", getRootDir().getAbsolutePath());
7879
}
80+
private static final Logger log = LoggerFactory.getLogger(EbicsClient.class);
7981

80-
private static final Logger logger = LogManager.getLogger(EbicsClient.class);
8182
private final Configuration configuration;
8283
private final Map<String, User> users = new HashMap<>();
8384
private final Map<String, Partner> partners = new HashMap<>();
@@ -104,7 +105,7 @@ public EbicsClient(Configuration configuration, ConfigProperties properties) {
104105
this.properties = properties;
105106
Messages.setLocale(configuration.getLocale());
106107
this.messages = new Messages(Constants.APPLICATION_BUNDLE_NAME, configuration.getLocale());
107-
logger.info(messages.getString("init.configuration"));
108+
log.info(messages.getString("init.configuration"));
108109
configuration.init();
109110
}
110111

@@ -121,7 +122,7 @@ private EbicsSession createSession(User user, Product product) {
121122
* the concerned user
122123
*/
123124
public void createUserDirectories(EbicsUser user) {
124-
logger.info(
125+
log.info(
125126
messages.getString("user.create.directories", user.getUserId()));
126127
IOUtils.createDirectories(configuration.getUserDirectory(user));
127128
IOUtils.createDirectories(configuration.getTransferTraceDirectory(user));
@@ -198,7 +199,7 @@ public User createUser(URL url, String bankName, String hostId, String partnerId
198199
String userId, String name, String email, String country, String organization,
199200
boolean useCertificates, boolean saveCertificates, PasswordCallback passwordCallback)
200201
throws Exception {
201-
logger.info(messages.getString("user.create.info", userId));
202+
log.info(messages.getString("user.create.info", userId));
202203

203204
Bank bank = createBank(url, bankName, hostId, useCertificates);
204205
Partner partner = createPartner(bank, partnerId);
@@ -217,10 +218,10 @@ public User createUser(URL url, String bankName, String hostId, String partnerId
217218
partners.put(partner.getPartnerId(), partner);
218219
banks.put(bank.getHostId(), bank);
219220

220-
logger.info(messages.getString("user.create.success", userId));
221+
log.info(messages.getString("user.create.success", userId));
221222
return user;
222223
} catch (Exception e) {
223-
logger.error(messages.getString("user.create.error"), e);
224+
log.error(messages.getString("user.create.error"), e);
224225
throw e;
225226
}
226227
}
@@ -245,7 +246,7 @@ private void createLetters(EbicsUser user, boolean useCertificates)
245246
*/
246247
public User loadUser(String hostId, String partnerId, String userId,
247248
PasswordCallback passwordCallback) throws Exception {
248-
logger.info(messages.getString("user.load.info", userId));
249+
log.info(messages.getString("user.load.info", userId));
249250

250251
try {
251252
Bank bank;
@@ -266,10 +267,10 @@ public User loadUser(String hostId, String partnerId, String userId,
266267
users.put(userId, user);
267268
partners.put(partner.getPartnerId(), partner);
268269
banks.put(bank.getHostId(), bank);
269-
logger.info(messages.getString("user.load.success", userId));
270+
log.info(messages.getString("user.load.success", userId));
270271
return user;
271272
} catch (Exception e) {
272-
logger.error(messages.getString("user.load.error"), e);
273+
log.error(messages.getString("user.load.error"), e);
273274
throw e;
274275
}
275276
}
@@ -282,9 +283,9 @@ public User loadUser(String hostId, String partnerId, String userId,
282283
*/
283284
public void sendINIRequest(User user, Product product) throws Exception {
284285
String userId = user.getUserId();
285-
logger.info(messages.getString("ini.request.send", userId));
286+
log.info(messages.getString("ini.request.send", userId));
286287
// if (user.isInitialized()) {
287-
// logger.info(messages.getString("user.already.initialized", userId));
288+
// log.info(messages.getString("user.already.initialized", userId));
288289
// return;
289290
// }
290291
EbicsSession session = createSession(user, product);
@@ -294,9 +295,9 @@ public void sendINIRequest(User user, Product product) throws Exception {
294295
try {
295296
keyManager.sendINI(null);
296297
user.setInitialized(true);
297-
logger.info(messages.getString("ini.send.success", userId));
298+
log.info(messages.getString("ini.send.success", userId));
298299
} catch (Exception e) {
299-
logger.error(messages.getString("ini.send.error", userId), e);
300+
log.error(messages.getString("ini.send.error", userId), e);
300301
throw e;
301302
}
302303
}
@@ -311,7 +312,7 @@ public void sendINIRequest(User user, Product product) throws Exception {
311312
*/
312313
public void sendHIARequest(User user, Product product) throws Exception {
313314
String userId = user.getUserId();
314-
logger.info(messages.getString("hia.request.send", userId));
315+
log.info(messages.getString("hia.request.send", userId));
315316
// if (user.isInitializedHIA()) {
316317
// logger
317318
// .info(messages.getString("user.already.hia.initialized", userId));
@@ -325,18 +326,18 @@ public void sendHIARequest(User user, Product product) throws Exception {
325326
keyManager.sendHIA(null);
326327
user.setInitializedHIA(true);
327328
} catch (Exception e) {
328-
logger.error(messages.getString("hia.send.error", userId), e);
329+
log.error(messages.getString("hia.send.error", userId), e);
329330
throw e;
330331
}
331-
logger.info(messages.getString("hia.send.success", userId));
332+
log.info(messages.getString("hia.send.success", userId));
332333
}
333334

334335
/**
335336
* Sends a HPB request to the ebics server.
336337
*/
337338
public void sendHPBRequest(User user, Product product) throws Exception {
338339
String userId = user.getUserId();
339-
logger.info(messages.getString("hpb.request.send", userId));
340+
log.info(messages.getString("hpb.request.send", userId));
340341

341342
EbicsSession session = createSession(user, product);
342343
KeyManagement keyManager = new KeyManagement(session);
@@ -346,9 +347,9 @@ public void sendHPBRequest(User user, Product product) throws Exception {
346347

347348
try {
348349
keyManager.sendHPB();
349-
logger.info(messages.getString("hpb.send.success", userId));
350+
log.info(messages.getString("hpb.send.success", userId));
350351
} catch (Exception e) {
351-
logger.error(messages.getString("hpb.send.error", userId), e);
352+
log.error(messages.getString("hpb.send.error", userId), e);
352353
throw e;
353354
}
354355
}
@@ -365,7 +366,7 @@ public void sendHPBRequest(User user, Product product) throws Exception {
365366
public void revokeSubscriber(User user, Product product) throws Exception {
366367
String userId = user.getUserId();
367368

368-
logger.info(messages.getString("spr.request.send", userId));
369+
log.info(messages.getString("spr.request.send", userId));
369370

370371
EbicsSession session = createSession(user, product);
371372
KeyManagement keyManager = new KeyManagement(session);
@@ -376,11 +377,11 @@ public void revokeSubscriber(User user, Product product) throws Exception {
376377
try {
377378
keyManager.lockAccess();
378379
} catch (Exception e) {
379-
logger.error(messages.getString("spr.send.error", userId), e);
380+
log.error(messages.getString("spr.send.error", userId), e);
380381
throw e;
381382
}
382383

383-
logger.info(messages.getString("spr.send.success", userId));
384+
log.info(messages.getString("spr.send.success", userId));
384385
}
385386

386387
/**
@@ -399,7 +400,7 @@ public void sendFile(File file, User user, Product product, EbicsOrderType order
399400
try {
400401
transferManager.sendFile(IOUtils.getFileContent(file), orderType, orderAttribute);
401402
} catch (IOException | EbicsException e) {
402-
logger
403+
log
403404
.error(messages.getString("upload.file.error", file.getAbsolutePath()), e);
404405
throw e;
405406
}
@@ -428,7 +429,7 @@ public void fetchFile(File file, User user, Product product, EbicsOrderType orde
428429
// don't log this exception as an error, caller can decide how to handle
429430
throw e;
430431
} catch (Exception e) {
431-
logger.error("{} {}", messages.getString("download.file.error"), e.getMessage(), e);
432+
log.error("{} {}", messages.getString("download.file.error"), e.getMessage(), e);
432433
throw e;
433434
}
434435
}
@@ -445,36 +446,36 @@ public void quit() {
445446
try {
446447
for (User user : users.values()) {
447448
if (user.needsSave()) {
448-
logger
449+
log
449450
.info(messages.getString("app.quit.users", user.getUserId()));
450451
configuration.getSerializationManager().serialize(user);
451452
}
452453
}
453454

454455
for (Partner partner : partners.values()) {
455456
if (partner.needsSave()) {
456-
logger
457+
log
457458
.info(messages.getString("app.quit.partners", partner.getPartnerId()));
458459
configuration.getSerializationManager().serialize(partner);
459460
}
460461
}
461462

462463
for (Bank bank : banks.values()) {
463464
if (bank.needsSave()) {
464-
logger
465+
log
465466
.info(messages.getString("app.quit.banks", bank.getHostId()));
466467
configuration.getSerializationManager().serialize(bank);
467468
}
468469
}
469470
} catch (EbicsException e) {
470-
logger.info(messages.getString("app.quit.error"));
471+
log.info(messages.getString("app.quit.error"));
471472
}
472473

473474
clearTraces();
474475
}
475476

476477
public void clearTraces() {
477-
logger.info(messages.getString("app.cache.clear"));
478+
log.info(messages.getString("app.cache.clear"));
478479
configuration.getTraceManager().clear();
479480
}
480481

@@ -511,14 +512,15 @@ private User createUser(ConfigProperties properties, PasswordCallback pwdHandler
511512
userCountry, userOrg, useCertificates, saveCertificates, pwdHandler);
512513
}
513514

514-
private static CommandLine parseArguments(Options options, String[] args) throws ParseException {
515+
private static CommandLine parseArguments(Options options, String[] args)
516+
throws ParseException, IOException {
515517
CommandLineParser parser = new DefaultParser();
516518
options.addOption(null, "help", false, "Print this help text");
517519
CommandLine line = parser.parse(options, args);
518520
if (line.hasOption("help")) {
519-
HelpFormatter formatter = new HelpFormatter();
521+
HelpFormatter formatter = HelpFormatter.builder().get();
520522
System.out.println();
521-
formatter.printHelp(EbicsClient.class.getSimpleName(), options);
523+
formatter.printHelp(EbicsClient.class.getSimpleName(), "",options, "", true);
522524
System.out.println();
523525
System.exit(0);
524526
}

src/main/java/org/kopi/ebics/client/FileTransfer.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import java.io.IOException;
2525
import java.util.Date;
2626

27-
import org.apache.logging.log4j.LogManager;
28-
import org.apache.logging.log4j.Logger;
2927
import org.kopi.ebics.exception.EbicsException;
3028
import org.kopi.ebics.interfaces.ContentFactory;
3129
import org.kopi.ebics.interfaces.EbicsOrderType;
@@ -47,7 +45,8 @@
4745
import org.kopi.ebics.xml.TransferResponseElement;
4846
import org.kopi.ebics.xml.UploadInitializationRequestElement;
4947
import org.kopi.ebics.xml.UploadTransferRequestElement;
50-
48+
import org.slf4j.Logger;
49+
import org.slf4j.LoggerFactory;
5150

5251
/**
5352
* Handling of file transfers.
@@ -80,7 +79,7 @@
8079
*
8180
*/
8281
public class FileTransfer {
83-
private static final Logger logger = LogManager.getLogger(EbicsClient.class);
82+
private static final Logger log = LoggerFactory.getLogger(FileTransfer.class);
8483
/**
8584
* Constructs a new FileTransfer session
8685
* @param session the user session
@@ -148,7 +147,7 @@ public void sendFile(ContentFactory factory,
148147
int httpCode;
149148

150149
Messages messages = new Messages(Constants.APPLICATION_BUNDLE_NAME);
151-
logger.info(messages.getString("upload.segment", segmentNumber));
150+
log.info(messages.getString("upload.segment", segmentNumber));
152151
uploader = new UploadTransferRequestElement(session,
153152
orderType,
154153
segmentNumber,

src/main/java/org/kopi/ebics/exception/ReturnCode.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
import java.util.Map;
2525
import java.util.MissingResourceException;
2626

27-
import org.apache.logging.log4j.LogManager;
28-
import org.apache.logging.log4j.Logger;
2927
import org.kopi.ebics.messages.Messages;
30-
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
3130

3231
/**
3332
* Representation of EBICS return codes.
@@ -38,7 +37,7 @@
3837
*
3938
*/
4039
public class ReturnCode implements Serializable {
41-
private static final Logger logger = LogManager.getLogger();
40+
private static final Logger log = LoggerFactory.getLogger(ReturnCode.class);
4241
/**
4342
* Constructs a new <code>ReturnCode</code> with a given
4443
* standard code, symbolic name and text
@@ -103,7 +102,7 @@ public static ReturnCode toReturnCode(String code, String text) {
103102
if (returnCode != null) {
104103
return returnCode;
105104
}
106-
logger.warn("Unknown return code: {}, text: {}", code, text);
105+
log.warn("Unknown return code: {}, text: {}", code, text);
107106
return new ReturnCode(code, text, text);
108107
}
109108

0 commit comments

Comments
 (0)