Skip to content

Commit

Permalink
feat:added log forwarding for cordova.
Browse files Browse the repository at this point in the history
  • Loading branch information
ndesai-newrelic committed Jun 5, 2024
1 parent 539f3f9 commit 3022538
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 5 deletions.
5 changes: 4 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
<preference name="CONSOLE_LOGS_ENABLED" default="true" />
<preference name="NEW_EVENT_SYSTEM_ENABLED" default="true" />
<preference name="BACKGROUND_REPORTING_ENABLED" default="false" />
<preference name="LOG_REPORTING_ENABLED" default="true" />


<platform name="ios">
<preference name="IOS_APP_TOKEN" default="x" />
Expand All @@ -59,6 +61,7 @@
<preference name="CONSOLE_LOGS_ENABLED" value="$CONSOLE_LOGS_ENABLED" />
<preference name="NEW_EVENT_SYSTEM_ENABLED" value="$NEW_EVENT_SYSTEM_ENABLED" />
<preference name="BACKGROUND_REPORTING_ENABLED" value="$BACKGROUND_REPORTING_ENABLED" />
<preference name="LOG_REPORTING_ENABLED" value="$LOG_REPORTING_ENABLED" />
<param name="ios-package" value="NewRelicCordovaPlugin" onload="true" />
</feature>
</config-file>
Expand Down Expand Up @@ -118,7 +121,7 @@
<preference name="CONSOLE_LOGS_ENABLED" value="$CONSOLE_LOGS_ENABLED" />
<preference name="NEW_EVENT_SYSTEM_ENABLED" value="$NEW_EVENT_SYSTEM_ENABLED" />
<preference name="BACKGROUND_REPORTING_ENABLED" value="$BACKGROUND_REPORTING_ENABLED" />

<preference name="LOG_REPORTING_ENABLED" value="$LOG_REPORTING_ENABLED" />
</config-file>

<source-file src="src/android/NewRelicCordovaPlugin.java"
Expand Down
88 changes: 86 additions & 2 deletions src/android/NewRelicCordovaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
import com.newrelic.agent.android.FeatureFlag;
import com.newrelic.com.google.gson.Gson;
import com.newrelic.com.google.gson.JsonArray;

import com.newrelic.agent.android.logging.LogLevel;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.newrelic.agent.android.logging.LogReporting;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -84,6 +85,12 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
NewRelic.enableFeature(FeatureFlag.FedRampEnabled);
}

if (preferences.getString("log_reporting_enabled", "true").equalsIgnoreCase("true")) {
NewRelic.enableFeature(FeatureFlag.LogReporting);
} else {
NewRelic.disableFeature(FeatureFlag.LogReporting);
}

Map<String, Integer> strToLogLevel = new HashMap<>();
strToLogLevel.put("ERROR", AgentLog.ERROR);
strToLogLevel.put("WARNING", AgentLog.WARN);
Expand All @@ -99,7 +106,8 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {

String collectorAddress = preferences.getString("collector_address", null);
String crashCollectorAddress = preferences.getString("crash_collector_address", null);

LogReporting.setLogLevel(LogLevel.VERBOSE);
NewRelic.setEntityGuid("MXxNT0JJTEV8QVBQTElDQVRJT058NjAxMzQ0MTMy");
NewRelic newRelic = NewRelic.withApplicationToken(appToken)
.withApplicationFramework(ApplicationFramework.Cordova, pluginVersion)
.withLoggingEnabled(preferences.getString("logging_enabled", "true").toLowerCase().equals("true"))
Expand Down Expand Up @@ -477,7 +485,71 @@ public void run() {
}
headers.put("headersList", array);
callbackContext.success(headers);
break;
}
case "logInfo": {
final String message = args.getString(0);
NewRelic.logInfo(message);
break;
}
case "logError": {
final String message = args.getString(0);
NewRelic.logError(message);
break;
}
case "logWarn": {
final String message = args.getString(0);
NewRelic.logWarning(message);
break;
}
case "logVerbose": {
final String message = args.getString(0);
NewRelic.logVerbose(message);
break;
}
case "logDebug": {
final String message = args.getString(0);
NewRelic.logDebug(message);
break;
}
case "log" : {
final String message = args.getString(0);
final String level = args.getString(1);

if(message == null || message.isEmpty()) {
Log.w(TAG, "Empty message given to log");
return false;
}

if(level == null) {
Log.w(TAG, "Null logLevel given to log");
return false;
}

Map<String, LogLevel> strToLogLevel = new HashMap<>();
strToLogLevel.put("ERROR", LogLevel.ERROR);
strToLogLevel.put("WARNING", LogLevel.WARN);
strToLogLevel.put("INFO", LogLevel.INFO);
strToLogLevel.put("VERBOSE", LogLevel.VERBOSE);
strToLogLevel.put("AUDIT", LogLevel.DEBUG);

LogLevel logLevel = strToLogLevel.get(level);

NewRelic.log(logLevel, message);
break;
}
case "logAll": {
final String message = args.getString(0);
final JSONObject attributesASJson = args.getJSONObject(1);
logAttributesFromJson(attributesASJson, message);
break;
}
case "logAttributes": {
final JSONObject attributesASJson = args.getJSONObject(0);
logAttributesFromJson(attributesASJson, null);
break;
}


}
} catch (Exception e) {
Expand All @@ -488,6 +560,18 @@ public void run() {
return true;

}
private void logAttributesFromJson(JSONObject attributesAsJson, String message) {
final Map attributes = new Gson().fromJson(String.valueOf(attributesAsJson), Map.class);
if(attributes == null) {
Log.e(TAG,"Null attributes given to logAttributes");
return;
}
if (message != null) {
attributes.put("message", message);
}
NewRelic.logAttributes(attributes);
}


protected static final class NRTraceConstants {
public static final String TRACE_PARENT = "traceparent";
Expand Down
18 changes: 18 additions & 0 deletions src/ios/NewRelicCordovaPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,22 @@

- (void)generateDistributedTracingHeaders:(CDVInvokedUrlCommand *)command;

- (void)logInfo:(CDVInvokedUrlCommand *)command;

- (void)logError:(CDVInvokedUrlCommand *)command;

- (void)logWarn:(CDVInvokedUrlCommand *)command;

- (void)logDebug:(CDVInvokedUrlCommand *)command;

- (void)logVerbose:(CDVInvokedUrlCommand *)command;

- (void)log:(CDVInvokedUrlCommand *)command;

- (void)logAttributes:(CDVInvokedUrlCommand *)command;

- (void)logAll:(CDVInvokedUrlCommand *)command;



@end
81 changes: 81 additions & 0 deletions src/ios/NewRelicCordovaPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ - (void)pluginInitialize
if (![self shouldDisableFeature:config[@"new_event_system_enabled"]]) {
[NewRelic enableFeatures:NRFeatureFlag_NewEventSystem];
}

if (![self shouldDisableFeature:config[@"log_reporting_enabled"]]) {
[NewRelic enableFeatures:NRFeatureFlag_LogReporting];
} else {
[NewRelic disableFeatures:NRFeatureFlag_LogReporting];
}

// Set log level depending on loggingEnabled and logLevel
NRLogLevels logLevel = NRLogLevelWarning;
Expand All @@ -92,6 +98,8 @@ - (void)pluginInitialize

[NewRelic setPlatform:NRMAPlatform_Cordova];
[NewRelic setPlatformVersion:config[@"plugin_version"]];
[NRLogger setLogTargets: NRLogTargetFile];
[NRLogger setLogEntityGuid:@"MXxNT0JJTEV8QVBQTElDQVRJT058NjAxMzQ0MTMy"];

if ([self isEmptyConfigParameter:collectorAddress] && [self isEmptyConfigParameter:crashCollectorAddress]) {
[NewRelic startWithApplicationToken:applicationToken];
Expand Down Expand Up @@ -451,5 +459,78 @@ - (void)generateDistributedTracingHeaders:(CDVInvokedUrlCommand *)command {
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

}
- (void)logInfo:(CDVInvokedUrlCommand *)command {
NSString* message = [command.arguments objectAtIndex:0];
[NewRelic logInfo:message];

}

- (void)logError:(CDVInvokedUrlCommand *)command {
NSString* message = [command.arguments objectAtIndex:0];
[NewRelic logError:message];
}

- (void)logWarn:(CDVInvokedUrlCommand *)command {
NSString* message = [command.arguments objectAtIndex:0];
[NewRelic logWarning:message];
}

- (void)logDebug:(CDVInvokedUrlCommand *)command {
NSString* message = [command.arguments objectAtIndex:0];
[NewRelic logDebug:message];
}

- (void)logVerbose:(CDVInvokedUrlCommand *)command {
NSString* message = [command.arguments objectAtIndex:0];
[NewRelic logVerbose:message];
}

- (void)log:(CDVInvokedUrlCommand *)command {
NSString* level = [command.arguments objectAtIndex:0];
NSString* message = [command.arguments objectAtIndex:1];

if(message == nil || message.length == 0) {
return;
}

if(level == nil || level.length == 0) {
return;
}

NRLogLevels logLevel = NRLogLevelWarning;
NSDictionary *logDict = @{
@"ERROR": [NSNumber numberWithInt:NRLogLevelError],
@"WARNING": [NSNumber numberWithInt:NRLogLevelWarning],
@"INFO": [NSNumber numberWithInt:NRLogLevelInfo],
@"VERBOSE": [NSNumber numberWithInt:NRLogLevelVerbose],
@"AUDIT": [NSNumber numberWithInt:NRLogLevelAudit],
};
if ([logDict objectForKey:[level uppercaseString]]) {
NSString* configLogLevel = [level uppercaseString];
NSNumber* newLogLevel = [logDict valueForKey:configLogLevel];
logLevel = [newLogLevel intValue];
}

[NewRelic log:message level:logLevel];
}

- (void)logAttributes:(CDVInvokedUrlCommand *)command {
NSDictionary *attributes = [command.arguments objectAtIndex:0];

[NewRelic logAll:attributes];

}

- (void)logAll:(CDVInvokedUrlCommand *)command {

NSString* message = [command.arguments objectAtIndex:0];
NSDictionary *attributes = [command.arguments objectAtIndex:1];

NSMutableDictionary *mutableDictionary = [attributes mutableCopy]; //Make the dictionary mutable to change/add

mutableDictionary[@"message"] = message;

[NewRelic logAll:mutableDictionary];
}

@end
Loading

0 comments on commit 3022538

Please sign in to comment.