index.js
// and sends to Loggly using bulk endpoint
and sends to Loggly using bulk endpoint
and sends to Loggly using bulk endpoint
function postEventsToLoggly(parsedEvents) {
        if (!logglyConfiguration.customerToken) {
            if (logglyConfiguration.tokenInitError) {
                console.log('error in decrypt the token. Not retrying.');
                return context.fail(logglyConfiguration.tokenInitError);
            }
            console.log('Cannot flush logs since authentication token has not been initialized yet. Trying again in 100 ms.');
            setTimeout(function () { postEventsToLoggly(parsedEvents) }, 100);
            return;
        }
        var finalEvent = parsedEvents.map(JSON.stringify).join('\n');
        try {
            var options = {
                hostname: logglyConfiguration.hostName,
                path: '/bulk/' + logglyConfiguration.customerToken + '/tag/' + encodeURIComponent(logglyConfiguration.tags),
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Content-Length': finalEvent.length
                }
            };
            var req = http.request(options, function (res) {
                res.on('data', function (result) {
                    result = JSON.parse(result.toString());
                    if (result.response === 'ok') {
                        context.succeed('all events are sent to Loggly');
                    } else {
                        console.log(result.response);
                    }
                });
                res.on('end', function () {
                    console.log('No more data in response.');
                    context.done();
                });
            });
            req.on('error', function (e) {
                console.log('problem with request: ' + e.toString());
                context.fail(e);
            });
            req.write(finalEvent);
            req.end();
        } catch (ex) {
            console.log(ex.message);
            context.fail(ex.message);
        }
    }
function postEventsToLoggly ( parsedEvents ) { if ( ! logglyConfiguration . customerToken ) { if ( logglyConfiguration . tokenInitError ) { console . log ( 'error in decrypt the token. Not retrying.' ) ; return context . fail ( logglyConfiguration . tokenInitError ) ; } console . log ( 'Cannot flush logs since authentication token has not been initialized yet. Trying again in 100 ms.' ) ; setTimeout ( function ( ) { postEventsToLoggly ( parsedEvents ) } , 100 ) ; return ; } var finalEvent = parsedEvents . map ( JSON . stringify ) . join ( '\n' ) ; try { var options = { hostname : logglyConfiguration . hostName , path : '/bulk/' + logglyConfiguration . customerToken + '/tag/' + encodeURIComponent ( logglyConfiguration . tags ) , method : 'POST' , headers : { 'Content-Type' : 'application/json' , 'Content-Length' : finalEvent . length } } ; var req = http . request ( options , function ( res ) { res . on ( 'data' , function ( result ) { result = JSON . parse ( result . toString ( ) ) ; if ( result . response === 'ok' ) { context . succeed ( 'all events are sent to Loggly' ) ; } else { console . log ( result . response ) ; } } ) ; res . on ( 'end' , function ( ) { console . log ( 'No more data in response.' ) ; context . done ( ) ; } ) ; } ) ; req . on ( 'error' , function ( e ) { console . log ( 'problem with request: ' + e . toString ( ) ) ; context . fail ( e ) ; } ) ; req . write ( finalEvent ) ; req . end ( ) ; } catch ( ex ) { console . log ( ex . message ) ; context . fail ( ex . message ) ; } }
and sends to Loggly using bulk endpoint
and sends to Loggly using bulk endpoint
// get all the events, stringify them and join them // with the new line character which can be sent to Loggly // via bulk endpoint // creating logglyURL at runtime, so that user can change the tag or customer token in the go // by modifying the current script // create request options to send logs // write data to request body