Skip to content

Switched to LoggingHelper.init() to clean up duplicate logging code #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 3 additions & 17 deletions JMS/com/ibm/mq/samples/jms/JmsGet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (c) Copyright IBM Corporation 2019, 2024
* (c) Copyright IBM Corporation 2019, 2025
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,6 +50,7 @@

import com.ibm.mq.samples.jms.SampleEnvSetter;
import com.ibm.mq.samples.jms.JwtHelper;
import com.ibm.mq.samples.jms.LoggingHelper;

public class JmsGet {

Expand Down Expand Up @@ -77,7 +78,7 @@ public class JmsGet {
public static void main(String[] args) {
logger.info("Get application is starting");

initialiseLogging();
LoggingHelper.init(logger);

SampleEnvSetter env = new SampleEnvSetter();
int limit = env.getCount();
Expand Down Expand Up @@ -256,21 +257,6 @@ private static void recordFailure(Exception ex) {
return;
}

private static void initialiseLogging() {
Logger defaultLogger = Logger.getLogger("");
Handler[] handlers = defaultLogger.getHandlers();
if (handlers != null && handlers.length > 0) {
defaultLogger.removeHandler(handlers[0]);
}

Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(LOGLEVEL);
logger.addHandler(consoleHandler);

logger.setLevel(LOGLEVEL);
logger.finest("Logging initialised");
}

private static void waitAWhile(int duration) {
try {
Thread.sleep(duration);
Expand Down
22 changes: 4 additions & 18 deletions JMS/com/ibm/mq/samples/jms/JmsPub.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (c) Copyright IBM Corporation 2019, 2024
* (c) Copyright IBM Corporation 2019, 2025
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,6 +50,7 @@

import com.ibm.mq.samples.jms.SampleEnvSetter;
import com.ibm.mq.samples.jms.JwtHelper;
import com.ibm.mq.samples.jms.LoggingHelper;

public class JmsPub {
private static final String DEFAULT_APP_NAME = "Dev Experience JmsPub";
Expand All @@ -73,7 +74,7 @@ public class JmsPub {
private static String accessToken = null;

public static void main(String[] args) {
initialiseLogging();
LoggingHelper.init(logger);
SampleEnvSetter env = new SampleEnvSetter();
jh = new JwtHelper(env);
if (jh.isJwtEnabled()) {
Expand Down Expand Up @@ -109,7 +110,7 @@ public static void main(String[] args) {
logger.info("Publishing messages.\n");

try {
publisher.send(destination, "this is a message");
publisher.send(destination, "this is a message " + i);
logger.info("message was sent");
Thread.sleep(2000);
} catch (JMSRuntimeException jmsex) {
Expand Down Expand Up @@ -218,21 +219,6 @@ private static void recordFailure(Exception ex) {
return;
}

private static void initialiseLogging() {
Logger defaultLogger = Logger.getLogger("");
Handler[] handlers = defaultLogger.getHandlers();
if (handlers != null && handlers.length > 0) {
defaultLogger.removeHandler(handlers[0]);
}

Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(LOGLEVEL);
logger.addHandler(consoleHandler);

logger.setLevel(LOGLEVEL);
logger.finest("Logging initialised");
}

private static void setUserCredentials(JmsConnectionFactory cf) {
try {
if (accessToken != null) {
Expand Down
20 changes: 3 additions & 17 deletions JMS/com/ibm/mq/samples/jms/JmsPut.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (c) Copyright IBM Corporation 2019, 2024
* (c) Copyright IBM Corporation 2019, 2025
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,6 +50,7 @@

import com.ibm.mq.samples.jms.SampleEnvSetter;
import com.ibm.mq.samples.jms.JwtHelper;
import com.ibm.mq.samples.jms.LoggingHelper;

public class JmsPut {

Expand All @@ -73,7 +74,7 @@ public class JmsPut {
private static String accessToken = null;

public static void main(String[] args) {
initialiseLogging();
LoggingHelper.init(logger);
SampleEnvSetter env = new SampleEnvSetter();
jh = new JwtHelper(env);
if (jh.isJwtEnabled()) {
Expand Down Expand Up @@ -206,21 +207,6 @@ private static void recordFailure(Exception ex) {
return;
}

private static void initialiseLogging() {
Logger defaultLogger = Logger.getLogger("");
Handler[] handlers = defaultLogger.getHandlers();
if (handlers != null && handlers.length > 0) {
defaultLogger.removeHandler(handlers[0]);
}

Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(LOGLEVEL);
logger.addHandler(consoleHandler);

logger.setLevel(LOGLEVEL);
logger.finest("Logging initialised");
}

private static void setUserCredentials(JmsConnectionFactory cf) {
try {
if (accessToken != null) {
Expand Down
20 changes: 3 additions & 17 deletions JMS/com/ibm/mq/samples/jms/JmsRequest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (c) Copyright IBM Corporation 2019, 2024
* (c) Copyright IBM Corporation 2019, 2025
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,6 +58,7 @@

import com.ibm.mq.samples.jms.SampleEnvSetter;
import com.ibm.mq.samples.jms.JwtHelper;
import com.ibm.mq.samples.jms.LoggingHelper;

public class JmsRequest {

Expand Down Expand Up @@ -91,7 +92,7 @@ public class JmsRequest {


public static void main(String[] args) {
initialiseLogging();
LoggingHelper.init(logger);
SampleEnvSetter env = new SampleEnvSetter();
jh = new JwtHelper(env);
if (jh.isJwtEnabled()) {
Expand Down Expand Up @@ -328,21 +329,6 @@ private static void recordFailure(Exception ex) {
return;
}

private static void initialiseLogging() {
Logger defaultLogger = Logger.getLogger("");
Handler[] handlers = defaultLogger.getHandlers();
if (handlers != null && handlers.length > 0) {
defaultLogger.removeHandler(handlers[0]);
}

Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(LOGLEVEL);
logger.addHandler(consoleHandler);

logger.setLevel(LOGLEVEL);
logger.finest("Logging initialised");
}

private static void setUserCredentials(JmsConnectionFactory cf) {
try {
if (accessToken != null) {
Expand Down
20 changes: 3 additions & 17 deletions JMS/com/ibm/mq/samples/jms/JmsResponse.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (c) Copyright IBM Corporation 2019, 2024
* (c) Copyright IBM Corporation 2019, 2025
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -62,6 +62,7 @@

import com.ibm.mq.samples.jms.SampleEnvSetter;
import com.ibm.mq.samples.jms.JwtHelper;
import com.ibm.mq.samples.jms.LoggingHelper;

public class JmsResponse {
private static final String DEFAULT_APP_NAME = "Dev Experience JmsResponse";
Expand All @@ -87,7 +88,7 @@ public class JmsResponse {
private static Long HOUR = 60 * 60 * SECOND;

public static void main(String[] args) {
initialiseLogging();
LoggingHelper.init(logger);
SampleEnvSetter env = new SampleEnvSetter();
jh = new JwtHelper(env);
if (jh.isJwtEnabled()) {
Expand Down Expand Up @@ -409,21 +410,6 @@ private static void recordFailure(Exception ex) {
return;
}

private static void initialiseLogging() {
Logger defaultLogger = Logger.getLogger("");
Handler[] handlers = defaultLogger.getHandlers();
if (handlers != null && handlers.length > 0) {
defaultLogger.removeHandler(handlers[0]);
}

Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(LOGLEVEL);
logger.addHandler(consoleHandler);

logger.setLevel(LOGLEVEL);
logger.finest("Logging initialised");
}

private static void setUserCredentials(JmsConnectionFactory cf) {
try {
if (accessToken != null) {
Expand Down
20 changes: 3 additions & 17 deletions JMS/com/ibm/mq/samples/jms/JmsSub.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (c) Copyright IBM Corporation 2019, 2024
* (c) Copyright IBM Corporation 2019, 2025
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,6 +47,7 @@

import com.ibm.mq.samples.jms.SampleEnvSetter;
import com.ibm.mq.samples.jms.JwtHelper;
import com.ibm.mq.samples.jms.LoggingHelper;

public class JmsSub {
private static final String DEFAULT_APP_NAME = "Dev Experience JmsSub";
Expand All @@ -69,7 +70,7 @@ public class JmsSub {
private static String accessToken = null;

public static void main(String[] args) {
initialiseLogging();
LoggingHelper.init(logger);
SampleEnvSetter env = new SampleEnvSetter();
jh = new JwtHelper(env);
if (jh.isJwtEnabled()) {
Expand Down Expand Up @@ -209,21 +210,6 @@ private static void recordFailure(Exception ex) {
return;
}

private static void initialiseLogging() {
Logger defaultLogger = Logger.getLogger("");
Handler[] handlers = defaultLogger.getHandlers();
if (handlers != null && handlers.length > 0) {
defaultLogger.removeHandler(handlers[0]);
}

Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(LOGLEVEL);
logger.addHandler(consoleHandler);

logger.setLevel(LOGLEVEL);
logger.finest("Logging initialised");
}

private static void setUserCredentials(JmsConnectionFactory cf) {
try {
if (accessToken != null) {
Expand Down
23 changes: 14 additions & 9 deletions JMS/com/ibm/mq/samples/jms/LoggingHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (c) Copyright IBM Corporation 2019
* (c) Copyright IBM Corporation 2019, 2025
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,20 +19,25 @@
import java.util.logging.*;

public class LoggingHelper {
private static boolean isLoggingInitialized = false;
private static final Level LOGLEVEL = Level.ALL;

public static void init(Logger logger) {
Logger defaultLogger = Logger.getLogger("");
Handler[] handlers = defaultLogger.getHandlers();
if (handlers != null && handlers.length > 0) {
defaultLogger.removeHandler(handlers[0]);
}
if (isLoggingInitialized)
return;
// avoid duplicate logs
logger.setUseParentHandlers(false);

Handler consoleHandler = new ConsoleHandler();
for (Handler handler : logger.getHandlers()) {
if (handler instanceof ConsoleHandler) {
logger.removeHandler(handler);
}
}
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(LOGLEVEL);
logger.addHandler(consoleHandler);

logger.setLevel(LOGLEVEL);
logger.finest("Logging initialised");
logger.finest("Logging initialized");
isLoggingInitialized = true;
}
}
4 changes: 2 additions & 2 deletions JMS/src/test/java/com/ibm/mq/samples/jms/JmsPubSubTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (c) Copyright IBM Corporation 2024
* (c) Copyright IBM Corporation 2024, 2025
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,7 +58,7 @@ public void testJmsPubSub(){
subThread.start();
//Wait for subscription to take place before publishing
try {
Thread.sleep(2000);
Thread.sleep(5000);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file seems not to fit into the logging update pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this file, we are not creating a console handler to output logs. Instead, we create a TestLogHandler to capture the logs generated by both the JMSSub and JMSPub samples, and we make assertions to verify that the expected messages were logged, it is uses custom testloghandler

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the only change in this file is extending the sleep from 2 seconds to 5 seconds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it was because after making changes in the logging method, we got rid of the unnecessary console handlers which were causing delay in some other processes. So after making the changes the JMSPub was sending messages a bit early before even the JMSSub starts even though there was sleep delay of 2s before the Pub threads starts, So Sub app wasn't getting the first message published by the JMSPub app, that was causing the test fail, So I extend the sleep time, now the Sub thread Strats before the Pub thread and gets all the messages

} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Loading