Giter Site home page Giter Site logo

jslogger's Introduction

jslogger

Logging Javascript logs to server.

Dependencies

Jslogger need jquery to be imported.

Usage

Download the src/jslogger-bundle.js and copy to desired location. Include jslogger.js in your page using below code.

<script type="text/javascript" src="jslogger-bundle.js"></script>

Initialize jslogger in your webpage

Initializing jslogger.js

<script type="text/javascript">
    var log = new jslogger({url:url, max_logs:max_logs, time_ms:time_ms});
</script>
  • url - Replace this with backend url where a function is listening for post request with a parameter log.
  • max_logs - Maximum number of logs to be packed and send to server at a time (optional default 1000).
  • time_ms - At what time interval all the logs needs to be checked and sent to server (optional default 5000ms).

Jslogger can bind user(string), if it is a login based website, by using:

log.bind({user:user, key:'additional info about user'})

The additional information will be logged with the key specified.

Jslogger supports the following console methods for logging in below way:

log.message(message, {data:"anykind of data"});
log.info(message, {data:"anykind of data"});
log.log(message, {data:"anykind of data"});
log.error(message, {data:"anykind of data"});
log.debug(message, {data:"anykind of data"});
log.warn(message, {data:"anykind of data"});

Example

log.info('testing log.info', {data:(new Date()).getTime()})

Jslogger can log exceptions using log.exception

Example

try {
    throw new Error('testing jslogger');
}
catch(e){
    log.exception(e.message);
}

Log ajax calls

  • Jslogger can log ajax calls by calling a function as log.ajax similar to $.ajax but in addition to it, jslogger log the time taken, start time, end time, ajax url, etc.
log.ajax({
    url: url,
    type: GET/POST,
    data: data,
    success: function(response){
        log.log('any message if needed' + {response: response});
    }
});

Jslogger even logs ajax call failure automatically.

  • Can log getJSON calls by calling a function as log.getJSON instead of $.getJSON
 log.getJSON(url, function(response){
      // your code goes here
 })
  • Can also log $.get and $.post in the above manner using log.get and log.post

  • When ever there is a javascript error it captures and logs it.

  • It uses localstorage in browser to store all the logs, even if the browser crashes the logs will not be destroyed. They will be logged to server when the page loads again.

  • Jslogger uses Epoch time(Unix time) for logging.

Server side

  • The logs will be logged via ajax call in post method with a variable log. The server side script should be able to take the request with argument log. Below is a example of form data for ajax call.
{
log:[
        {
        UUID: '181393b0-8702-11e7-9ca6-a7e5a0572c550.8393907533887879', //sortable unique number
        level : info/log/debug/error/user message/warn/exception/ajax,
        message: 'message',
        user: 'user if specified',
        url: 'url where log happened',
        host_url: 'host url(domain)',
        misc: 'any extra data user specifies that is used while logging'
        },
        {}, {}, ....
    ]
}

jslogger's People

Contributors

jaswanth098 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.