创建一个日志和追踪服务层,使用一个通用的JavaScript类库log4javascript.
http://log4javascript.org
它能使用各类appender和分层格式将日志写入到各种形式中,下面我们使用基本的BrowserConsoleAppender
将日志达到浏览器的控制台上。
angular.module('brew-everywhere').factory('logging',
function (messaging, events) {
var log = null;
var init = function (logName) {
log = log4javascript.getLogger(logName);
};
var setLogLevel = function (level) {
log.setLevel(level);
};
var setLogAppender = function (appender) {
log.addAppender(appender);
};
var trace = function (message) {
log.trace(message);
};
messaging.subscribe(events.message._LOG_TRACE_, trace);
var debug = function (message) {
log.debug(message);
};
messaging.subscribe(events.message._LOG_DEBUG_, debug);
var info = function (message) {
log.info(message);
};
messaging.subscribe(events.message._LOG_INFO_, info);
var warn = function (message) {
log.warn(message);
};
messaging.subscribe(events.message._LOG_WARNING_, warn);
var error = function (message) {
log.error(message);
};
messaging.subscribe(events.message._LOG_ERROR_, error);
var fatal = function (message) {
log.fatal(message);
};
messaging.subscribe(events.message._LOG_FATAL_, fatal);
var service = {
init: init,
setLogLevel: setLogLevel,
setLogAppender: setLogAppender
};
return service;
})
.run(['logging', function (logging) {
logging.init('main');
logging.setLogLevel(log4javascript.Level.ALL);
logging.setLogAppender(new log4javascript.
BrowserConsoleAppender());
}]);
下面将日志服务应用于一个基础Controller的定义中:
angular.module('brew-everywhere')
.controller('BaseCtrl',function($scope,messaging,events){
$scope.trace = function(message){
messaging.publish(events.message._LOG_TRACE_, [message]);
};
$scope.traceDebug = function(message){
messaging.publish(events.message._LOG_DEBUG_, [message]);
};
$scope.traceInfo = function(message){
messaging.publish(events.message._LOG_INFO_, [message]);
};
$scope.traceWarning = function(message){
messaging.publish(events.message._LOG_WARNING_, [message]);
};
$scope.traceError = function(message){
messaging.publish(events.message._LOG_ERROR_, [message]);
};
$scope.traceFatal = function(message){
messaging.publish(events.message._LOG_FATAL_, [message]);
};
$scope.messagingHandles = [];
$scope.subscribe = function(topic, callback){
var handle = messaging.subscribe(topic, callback);
if(handle)
{
$scope.messagingHandles.push(handle);
}
};
$scope.publish = function(topic, data){
messaging.publish(topic, data);
};
$scope.$on('$destroy', function(){
angular.foreach($scope.messagingHandles, function(handle){
messaging.unsubscribe(handle);
});
});
});
如此我们就可以改造LoginCtrl,让其继承BaseController
angular.module('brew-anywhere').controller('LoginCtrl',
function($scope, $controller, events){
// this call to $controller adds the base controller's methods
// and properties to the controller's scope
$controller('BaseCtrl', {$scope: $scope});
//#region Internal models
$scope.username = '';
$scope.password = '';
$scope.currentUser = {};
$scope.userAuthenticatedHandle = null;
//#endregion Internal models
//#region message handlers
$scope.authenticateUserCompletedHandler = function(user) {
$scope.currentUser = user;
$scope.traceInfo("authenticateUserCompletedHandler received: " + angular.toJson(user));
};
$scope.subscribe(events.message._AUTHENTICATE_USER_COMPLETE_,$scope.authenticateUserCompletedHandler);
//#endregion message handlers
$scope.onLogin = function() {
$scope.traceInfo("onLogin authenticating user: " + $scope.username);
$scope.publish(events.message._AUTHENTICATE_USER_, [$scope.username, $scope.password]);
};
});
创建基于OAuth 2.0的用户身份验证服务层:authenticate service
angular.module('brew-everywhere').factory('authenticate',
function (messaging, events) {
var currentUser = {};
var loginWithGooglePlus = function(){
messaging.publish(events.message._SERVER_REQUEST_STARTED_);
messaging.publish(events.message._GPLUS_AUTHENTICATE_);
};
messaging.subscribe(events.message._AUTHENTICATE_USER_GPLUS_,loginWithGooglePlus);
var googlePlusAuthenticatedHandler = function(user) {
currentUser = user;
messaging.publish(events.message._AUTHENTICATE_USER_COMPLETE_,[currentUser]);
messaging.publish(events.message._SERVER_REQUEST_ENDED_);
};
messaging.subscribe(events.message._GPLUS_AUTHENTICATED_,googlePlusAuthenticatedHandler);
var authenticationFailureHandler = function() {
messaging.publish(events.message._AUTHENTICATE_USER_FAILED_);
messaging.publish(events.message._SERVER_REQUEST_ENDED_);
messaging.publish(events.message._ADD_ERROR_MESSAGE_,['Log In Failed.', 'alert-warning']);
};
messaging.subscribe(events.message._GPLUS_FAILED_,authenticationFailureHandler);
var login = function(username, password){
currentUser = {name: {givenName: "Test", surname: "User"}};
messaging.publish(events.message._AUTHENTICATE_USER_COMPLETE_,[currentUser]);
};
messaging.subscribe(events.message._AUTHENTICATE_USER_,login);
var init = function(){
currentUser = {};
};
var authenticate = {
init: init
};
return authenticate;
})
.run(function(authenticate){
authenticate.init();
});