Skip to content
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

injecting log to any javascript function #51

Open
wants to merge 10 commits into
base: master
from

modified readme to include the code injection

  • Loading branch information
dhewzulla committed Nov 8, 2017
commit e1acf44dc255f102d99eedcb0c1426dad69ae6bd
@@ -1,14 +1,15 @@
loggly-jslogger
===============

Client-side (browser) logger to use with Loggly gen2. Check out Loggly's [Javascript logging documentation](https://www.loggly.com/docs/javascript/) to learn more.
Client-side (browser) logger to use with Loggly gen2. Check out Loggly's [Javascript logging documentation](https://www.loggly.com/docs/javascript/) to learn more.

Installation
------------


Place the following on your page, and replace the logglyKey value with the key provided by the website:
```html
<script type="text/javascript" src="//cloudfront.loggly.com/js/loggly.tracker-latest.min.js" async></script>
<script type="text/javascript" src="https://rawgit.com/aws-logger/loggly-jslogger/master/src/loggly.tracker.js" async></script>
<script>
var _LTracker = _LTracker || [];
_LTracker.push({
@@ -48,7 +49,7 @@ myBetterLogger.push({'logglyKey': 'your-customer-token' }); // push a loggly ke

Send Console Errors to Loggly
----
Keeping <strong>sendConsoleErrors</strong> value to <i>true</i> will send all the unhandled errors to the Loggly with the detailed information like error message, URL, line number and column number. This script also take cares of all the previously defined window.onerror functions.
Keeping <strong>sendConsoleErrors</strong> value to <i>true</i> will send all the unhandled errors to the Loggly with the detailed information like error message, URL, line number and column number. This script also take cares of all the previously defined window.onerror functions.

Send Tags to Loggly
----
@@ -98,3 +99,81 @@ You can build min and map file by using the command below:
npm install
grunt
```

Intercept and log any JavaScript function calls
----------

You may not like the mixing the Loggly logging codes everywhere inside your application codes. This creates undesirable dependencies on the Loggly and makes your codes ugly.

Instead you may rather prefer transparently send messages passed into the console.log, console.warn, console.info and console.error to the Loggly. Or you may even not like the console.log() functions as well. You prefer to decide which part of the code you would like to log when some issues happens on live instead of making decisition at development time. So you would like to use the configuration to control the logging behaviour transparently from the applications.

Also you may like to enable this automatic logging for only some percentage of user sessions, instead of all users.


For example if you would to log the console.error() function, you just need to put the code below as soon as you have initialised the Loggly:
```Javascript
_LTracker.injectLog({
enable:100
target:"console.error",
name:"error"
});
```
The value of ```enable``` attribute is set 100. This means thar the Loggly will log the console.error() function for 100% percent of the users, which means all user sessions.

The value of the ```target``` attribute specifies which javascript function it needs to monitor. It has to have atleast one "." to specify the target object and target method the Loggly needs to monitor.

The functions that the Loggly intercept will not be affected. For example, console.error() will still continually print messages to the console as usual. The injected codes will simply send the messages to loggly after executing the original console function. For example, if the application executes the following code:
```Javascript
console.error("Something wrong");
```
Then the injected code will transparently executes the following code:

```Javascript
_LTracker.push({
error:"Something wrong"
});
```

The name of the variable ```error``` is coming from the ```name``` attribute passed into the ```_LTracker.injectLog``` in the previous example.

The input parameter is an object, for example:
```Javascript
console.error({errorcode:"Error01", description:"Something goes wrong"});
```
The injected code will execute:

```Javascript
_LTracker.push({
error:{errorcode:"Error01", description:"Something goes wrong"}
})
```
If multiple parameters are passed in:
```Javascript
console.error("Something wrong", {errorCode:"001", errorCotent:"wierd error"});
```
The injected code will execute:
```Javascript
_LTracker.push({
error:{param1:"Something wrong", param2:{errorCode:"001", errorCotent:"wierd error"}}
})
```

On other hand if the injected function has the return value, for example if we have the following:

LTracker.injectLog({
name:"LocalStorageLog"
target:"localStorage.getItem",
enable:100
});

if the application executes:

var username=localStorage.getItem("username");

and if the returned value from the localStorage is "dilshat"

then injected code executes the following:

_LTracker.push({
LocalStorageLog:{input:"username", output:"dilshat}
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.