Skip to content

Commit bf1d13c

Browse files
Merge pull request #27 from azmigproject/master
Logging applied to more areas
2 parents 4c2d57c + 35056d4 commit bf1d13c

File tree

6 files changed

+64
-20
lines changed

6 files changed

+64
-20
lines changed

device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/DeviceClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ protected enum IotHubClientState
8686
protected ScheduledExecutorService taskScheduler;
8787
protected IotHubClientState state;
8888
protected long RECEIVE_PERIOD_MILLIS;
89+
protected CustomLogger logger;
8990

9091
/**
9192
* Constructor that takes a connection string as an argument.
@@ -153,6 +154,8 @@ else if (attr.startsWith(SHARED_ACCESS_TOKEN_ATTRIBUTE))
153154
}
154155

155156
initIotHubClient(hostname, deviceId, sharedAccessKey, sharedAccessToken, protocol);
157+
this.logger = new CustomLogger(this.getClass());
158+
logger.LogInfo("DeviceClient object is created successfully, method name is %s ", logger.getMethodName());
156159
}
157160

158161
/**
@@ -251,6 +254,7 @@ public void sendEventAsync(Message message,
251254
throw new IllegalArgumentException("Cannot send message 'null'.");
252255
}
253256

257+
logger.LogInfo("Message with messageid %s along with callback and callbackcontext is added to the queue, method name is %s ", message.getMessageId(), logger.getMethodName());
254258
// Codes_SRS_DEVICECLIENT_11_006: [The function shall add the message, with its associated callback and callback context, to the transport.]
255259
transport.addMessage(message, callback, callbackContext);
256260
}
@@ -376,6 +380,7 @@ protected DeviceClient()
376380

377381
private void setOption_SetMinimumPollingInterval(Object value)
378382
{
383+
logger.LogInfo("Setting MinimumPollingInterval as %s milliseconds, method name is %s ", value, logger.getMethodName());
379384
if (!value.equals(null)) {
380385
if (this.state != IotHubClientState.CLOSED) {
381386
throw new IllegalStateException("setOption " + SET_MINIMUM_POLLING_INTERVAL +
@@ -396,6 +401,7 @@ private void setOption_SetMinimumPollingInterval(Object value)
396401

397402
private void setOption_SetCertificatePath(Object value)
398403
{
404+
logger.LogInfo("Setting CertificatePath as %s, method name is %s ", value, logger.getMethodName());
399405
if (this.state != IotHubClientState.CLOSED) {
400406
throw new IllegalStateException("setOption " + SET_CERTIFICATE_PATH +
401407
"only works when the transport is closed");
@@ -410,6 +416,7 @@ private void setOption_SetCertificatePath(Object value)
410416

411417
private void setOption_SetSASTokenExpiryTime(Object value)
412418
{
419+
logger.LogInfo("Setting SASTokenExpiryTime as %s seconds, method name is %s ", value, logger.getMethodName());
413420
if (!value.equals(null)) {
414421
//**Codes_SRS_DEVICECLIENT_25_009: [**"SetSASTokenExpiryTime" should have value type long**.]**
415422
long validTimeInSeconds;
@@ -502,6 +509,7 @@ public void setOption(String optionName, Object value) {
502509
setOption_SetMinimumPollingInterval(value);
503510

504511
} else {
512+
logger.LogError("optionName is unknown = %s for %s, method name is %s ", optionName, this.transport.getClass(), logger.getMethodName());
505513
// Codes_SRS_DEVICECLIENT_02_001: [If optionName is null or not an option
506514
// handled by the client, then it shall throw IllegalArgumentException.]
507515
throw new IllegalArgumentException("optionName is unknown = " + optionName
@@ -519,6 +527,7 @@ public void setOption(String optionName, Object value) {
519527
setOption_SetCertificatePath(value);
520528

521529
} else {
530+
logger.LogError("optionName is unknown = %s for %s, method name is %s ", optionName, this.transport.getClass(), logger.getMethodName());
522531
// Codes_SRS_DEVICECLIENT_02_001: [If optionName is null or not an option handled by the
523532
// client, then it shall throw IllegalArgumentException.]
524533
throw new IllegalArgumentException("optionName is unknown = " + optionName +
@@ -535,6 +544,7 @@ public void setOption(String optionName, Object value) {
535544
setOption_SetSASTokenExpiryTime(value);
536545

537546
} else {
547+
logger.LogError("optionName is unknown = %s for %s, method name is %s ", optionName, this.transport.getClass(), logger.getMethodName());
538548
// Codes_SRS_DEVICECLIENT_02_001: [If optionName is null or not an option handled by the
539549
// client, then it shall throw IllegalArgumentException.]
540550
throw new IllegalArgumentException("optionName is unknown = " + optionName +

device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/DeviceClientConfig.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public final class DeviceClientConfig
4040
/** The context to be passed in to the message callback. */
4141
protected Object messageContext;
4242

43+
protected CustomLogger logger;
4344
/**
4445
* Constructor.
4546
*
@@ -79,7 +80,8 @@ public DeviceClientConfig(String iotHubHostname, String deviceId,
7980
this.deviceKey = deviceKey;
8081
// Codes_SRS_DEVICECLIENTCONFIG_25_017: [**The constructor shall save sharedAccessToken.**] **
8182
this.sharedAccessToken = sharedAccessToken;
82-
83+
this.logger = new CustomLogger(this.getClass());
84+
logger.LogInfo("DeviceClientConfig object is created successfully with IotHubName=%s, deviceID=%s , method name is %s ", this.iotHubName, this.deviceId, logger.getMethodName());
8385
}
8486

8587
/**

device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/Message.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ public void setProperty(String name, String value) {
248248
this.properties.remove(messageProperty);
249249
}
250250

251+
logger.LogInfo("Setting message property with name=%s and value=%s, method name is %s ", name, value, logger.getMethodName());
251252
this.properties.add(new MessageProperty(name, value));
252253
}
253254

@@ -372,6 +373,6 @@ public void setExpiryTime(long timeOut)
372373
{
373374
long currentTime = System.currentTimeMillis();
374375
this.expiryTime = currentTime + timeOut;
375-
logger.LogInfo("The message with messageid %s has expiry time as %s, method name is %s ", this.getMessageId(), new Date(this.expiryTime), logger.getMethodName());
376+
logger.LogInfo("The message with messageid %s has expiry time as %s milliseconds and the message will expire on %s, method name is %s ", this.getMessageId(), timeOut, new Date(this.expiryTime), logger.getMethodName());
376377
}
377378
}

device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/MessageProperty.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public final class MessageProperty {
4141
protected final String name;
4242
/** The property value. */
4343
protected final String value;
44-
44+
protected CustomLogger logger;
4545
/**
4646
* Constructor.
4747
*
@@ -54,29 +54,35 @@ public final class MessageProperty {
5454
* A message property name cannot be one of the reserved property names.
5555
*/
5656
public MessageProperty(String name, String value) {
57+
this.logger = new CustomLogger(this.getClass());
5758
if (name == null) {
59+
logger.LogError("Property argument 'name' cannot be null, method name is %s ", logger.getMethodName());
5860
throw new IllegalArgumentException("Property argument 'name' cannot be null.");
5961
}
6062

6163
if (value == null) {
64+
logger.LogError("Property argument 'value' cannot be null, method name is %s ", logger.getMethodName());
6265
throw new IllegalArgumentException("Property argument 'value' cannot be null.");
6366
}
6467

6568
// Codes_SRS_MESSAGEPROPERTY_11_002: [If the name contains a character that is not in US-ASCII printable characters or is one of: ()<>@,;:\"/[]?={} (space) (horizontal tab), the function shall throw an IllegalArgumentException.]
6669
if (!usesValidChars(name)) {
67-
String errMsg = String.format("%s is not a valid IoT Hub message property name.\n", name);
70+
logger.LogError("%s is not a valid IoT Hub message property name, method name is %s ", name, logger.getMethodName());
71+
String errMsg = String.format("%s is not a valid IoT Hub message property name.\n", name);
6872
throw new IllegalArgumentException(errMsg);
6973
}
7074

7175
// Codes_SRS_MESSAGEPROPERTY_11_008: [If the name is a reserved property name, the function shall throw an IllegalArgumentException.]
7276
if (RESERVED_PROPERTY_NAMES.contains(name)) {
73-
String errMsg = String.format("%s is a reserved IoT Hub message property name.\n", name);
77+
logger.LogError("%s is a reserved IoT Hub message property name, method name is %s ", name, logger.getMethodName());
78+
String errMsg = String.format("%s is a reserved IoT Hub message property name.\n", name);
7479
throw new IllegalArgumentException(errMsg);
7580
}
7681

7782
// Codes_SRS_MESSAGEPROPERTY_11_003: [If the value contains a character that is not in US-ASCII printable characters or is one of: ()<>@,;:\"/[]?={} (space) (horizontal tab), the function shall throw an IllegalArgumentException.]
7883
if (!usesValidChars(value))
7984
{
85+
logger.LogError("%s is a reserved IoT Hub message property name, method name is %s ", name, logger.getMethodName());
8086
String errMsg = String.format("%s is not a valid IoT Hub message property value.\n", value);
8187
throw new IllegalArgumentException(errMsg);
8288
}

device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/transport/amqps/AmqpsIotHubConnection.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,13 @@ public Integer sendMessage(Message message)
337337
byte[] msgData = new byte[1024];
338338
int length;
339339

340+
logger.LogInfo("Started encoding of message - entering in while loop, method name is %s ", logger.getMethodName());
340341
while (true)
341342
{
342343
try
343344
{
344345
length = message.encode(msgData, 0, msgData.length);
346+
logger.LogInfo("Completed encoding of message, length is %s - breaking the while loop to come out, method name is %s ", length, logger.getMethodName());
345347
break;
346348
}
347349
catch (BufferOverflowException e)
@@ -354,15 +356,17 @@ public Integer sendMessage(Message message)
354356
byte[] tag = String.valueOf(this. nextTag++).getBytes();
355357
Delivery dlv = sender.delivery(tag);
356358

359+
logger.LogInfo("Attempting to send the message using the sender link, method name is %s ", logger.getMethodName());
357360
// Codes_SRS_AMQPSIOTHUBCONNECTION_15_018: [The function shall attempt to send the message using the sender link.]
358361
sender.send(msgData, 0, length);
359-
362+
363+
logger.LogInfo("Advancing the sender link, method name is %s ", logger.getMethodName());
360364
// Codes_SRS_AMQPSIOTHUBCONNECTION_15_019: [The function shall advance the sender link.]
361365
sender.advance();
362366

363367
// Codes_SRS_AMQPSIOTHUBCONNECTION_15_020: [The function shall set the delivery hash to the value returned by the sender link.]
364368
deliveryHash = dlv.hashCode();
365-
369+
logger.LogInfo("Delivery hash returned by the sender link %s, method name is %s ", deliveryHash, logger.getMethodName());
366370
}
367371

368372
// Codes_SRS_AMQPSIOTHUBCONNECTION_15_021: [The function shall return the delivery hash.]
@@ -384,6 +388,7 @@ public Boolean sendMessageResult(AmqpsMessage message, IotHubMessageResult resul
384388
{
385389
try
386390
{
391+
logger.LogInfo("Acknowledgement for received message is %s, method name is %s ", result.name(), logger.getMethodName());
387392
// Codes_SRS_AMQPSIOTHUBCONNECTION_15_023: [If the message result is COMPLETE, ABANDON, or REJECT,
388393
// the function shall acknowledge the last message with acknowledgement type COMPLETE, ABANDON, or REJECT respectively.]
389394
switch (result)
@@ -399,6 +404,7 @@ public Boolean sendMessageResult(AmqpsMessage message, IotHubMessageResult resul
399404
break;
400405
default:
401406
// should never happen.
407+
logger.LogError("Invalid IoT Hub message result (%s), method name is %s ", result.name(), logger.getMethodName());
402408
throw new IllegalStateException("Invalid IoT Hub message result.");
403409
}
404410

@@ -545,23 +551,26 @@ public void onDelivery(Event event)
545551
logger.LogDebug("Entered in method %s", logger.getMethodName());
546552
if(event.getLink().getName().equals(receiveTag))
547553
{
554+
logger.LogInfo("Reading the receiver link, method name is %s ", logger.getMethodName());
548555
// Codes_SRS_AMQPSIOTHUBCONNECTION_15_034: [If this link is the Receiver link, the event handler shall get the Receiver and Delivery (Proton) objects from the event.]
549556
Receiver receiveLink = (Receiver) event.getLink();
550557
Delivery delivery = receiveLink.current();
551558
if (delivery.isReadable() && !delivery.isPartial()) {
559+
logger.LogInfo("Reading the received buffer, method name is %s ", logger.getMethodName());
552560
// Codes_SRS_AMQPSIOTHUBCONNECTION_15_035: [The event handler shall read the received buffer.]
553561
int size = delivery.pending();
554562
byte[] buffer = new byte[size];
555563
int read = receiveLink.recv(buffer, 0, buffer.length);
556564
receiveLink.advance();
557-
565+
logger.LogInfo("Reading the received buffer completed, method name is %s ", logger.getMethodName());
558566
// Codes_SRS_AMQPSIOTHUBCONNECTION_15_036: [The event handler shall create an AmqpsMessage object from the decoded buffer.]
559567
AmqpsMessage msg = new AmqpsMessage();
560568

561569
// Codes_SRS_AMQPSIOTHUBCONNECTION_15_037: [The event handler shall set the AmqpsMessage Deliver (Proton) object.]
562570
msg.setDelivery(delivery);
571+
logger.LogInfo("Decoding the received message , method name is %s ", logger.getMethodName());
563572
msg.decode(buffer, 0, read);
564-
573+
logger.LogInfo("Decoding the received message completed , method name is %s ", logger.getMethodName());
565574
// Codes_SRS_AMQPSIOTHUBCONNECTION_15_049: [All the listeners shall be notified that a message was received from the server.]
566575
this.messageReceivedFromServer(msg);
567576
}
@@ -571,12 +580,15 @@ public void onDelivery(Event event)
571580
//Sender specific section for dispositions it receives
572581
if(event.getType() == Event.Type.DELIVERY)
573582
{
583+
logger.LogInfo("Reading the delivery event in Sender link, method name is %s ", logger.getMethodName());
574584
// Codes_SRS_AMQPSIOTHUBCONNECTION_15_038: [If this link is the Sender link and the event type is DELIVERY, the event handler shall get the Delivery (Proton) object from the event.]
575585
Delivery d = event.getDelivery();
576586
DeliveryState remoteState = d.getRemoteState();
577587

578588
// Codes_SRS_AMQPSIOTHUBCONNECTION_15_039: [The event handler shall note the remote delivery state and use it and the Delivery (Proton) hash code to inform the AmqpsIotHubConnection of the message receipt.]
579589
boolean state = remoteState.equals(Accepted.getInstance());
590+
logger.LogInfo("Is state of remote Delivery COMPLETE ? %s, method name is %s ", state, logger.getMethodName());
591+
logger.LogInfo("Inform listener that a message has been sent to IoT Hub along with remote state, method name is %s ", logger.getMethodName());
580592
//let any listener know that the message was received by the server
581593
for(ServerListener listener : listeners)
582594
{
@@ -597,6 +609,7 @@ public void onLinkFlow(Event event)
597609
logger.LogDebug("Entered in method %s", logger.getMethodName());
598610
// Codes_SRS_AMQPSIOTHUBCONNECTION_15_040: [The event handler shall save the remaining link credit.]
599611
this.linkCredit = event.getLink().getCredit();
612+
logger.LogDebug("The link credit value is %s, method name is %s", this.linkCredit, logger.getMethodName());
600613
logger.LogDebug("Exited from method %s", logger.getMethodName());
601614
}
602615

@@ -732,6 +745,7 @@ private void startReconnect()
732745
*/
733746
private void messageReceivedFromServer(AmqpsMessage msg)
734747
{
748+
logger.LogInfo("All the listeners are informed that a message has been received, method name is %s ", logger.getMethodName());
735749
for(ServerListener listener : listeners)
736750
{
737751
listener.messageReceived(msg);

0 commit comments

Comments
 (0)