Skip to content

Instantly share code, notes, and snippets.

@liveweird liveweird/nebulaAppCtrl.js Secret
Last active Jan 2, 2016

Embed
What would you like to do?
Angular.js service used to send buffered client-side logs to the server-side Web API.
'use strict';
nebulaApp.controller('nebulaAppCtrl',
function($scope, $rootScope, $location, ...) {
// ...
$scope.$on("$routeChangeSuccess", function (angularEvent, current, previous) {
$scope.$emit("log:nebula", "Route changed: [" + $location.path() + "]");
//...
});
$rootScope.$on("log:nebula", function (e, data) {
nebulaLogSvc.log(data, ...);
});
});
'use strict';
nebulaApp.service('nebulaLogSvc', ['$http', 'NEBULA_URL', function($http, nebulaUrl) {
var logBufferLength = 60;
var logBufferCpty = 10;
var idx = 1;
var apiLogger = Rx.Observer.create(function(entries) {
idx = idx + 1;
$http.post('' + nebulaUrl + 'api/Nebula/Log',
{
id: idx,
entries: entries
}
);
});
var loggedEvents = new Rx.Subject();
loggedEvents.bufferWithTimeOrCount(logBufferLength * 1000, logBufferCpty)
.filter(function (array) {
return (array.length > 0);
})
.subscribe(apiLogger);
function LogEntry(message, loginName) {
idx = idx + 1;
this.id = idx;
this.timeStamp = moment().toJSON();
this.message = message;
this.loginName = loginName;
};
this.log = function(message, loginName) {
loggedEvents.onNext(new LogEntry(message, loginName));
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.