Skip to content

Instantly share code, notes, and snippets.

@liveweird
Last active January 2, 2016 23:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liveweird/14e04592c05a7a746dd9 to your computer and use it in GitHub Desktop.
Save liveweird/14e04592c05a7a746dd9 to your computer and use it in GitHub Desktop.
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