Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging Module #1425

Merged
merged 2 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/core/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require([
'core/js/views/navigationView',
'core/js/accessibility',
'core/js/offlineStorage',
'core/js/logging',
'core/js/device',
'core/js/drawer',
'core/js/notify',
Expand All @@ -24,7 +25,7 @@ require([
// Append loading template and show
window.Handlebars = _.extend(require("handlebars"), window.Handlebars);

var template = Handlebars.templates['loading'];
var template = Handlebars.templates['loading'];
$('#wrapper').append(template());

Adapt.config = new ConfigModel(null, {url: "course/config.json", reset:true});
Expand Down Expand Up @@ -67,19 +68,21 @@ require([
// Replace the existing property
Adapt.course.set('_buttons', buttons);
}


Adapt.log.debug('Firing app:dataLoaded');

try {
Adapt.trigger('app:dataLoaded');// Triggered to setup model connections in AdaptModel.js
} catch(e) {
outputError(e);
Adapt.log.error('Error during app:dataLoading trigger', e);
}

Adapt.setupMapping();

try {
Adapt.trigger('app:dataLoaded');
} catch(e) {
outputError(e);
Adapt.log.error('Error during app:dataLoaded trigger', e);
}

if (!Adapt.isWaitingForPlugins()) triggerDataReady(newLanguage);
Expand All @@ -101,31 +104,28 @@ require([
if (startController.isEnabled()) {
hash = startController.getStartHash(true);
}

Backbone.history.navigate(hash, { trigger: true, replace: true });
});
}


Adapt.log.debug('Firing app:dataReady');

try {
Adapt.trigger('app:dataReady');
} catch(e) {
outputError(e);
Adapt.log.error('Error during app:dataReady trigger', e);
}

Adapt.navigation = new NavigationView();// This should be triggered after 'app:dataReady' as plugins might want to manipulate the navigation

Adapt.initialize();

Adapt.off('adaptCollection:dataLoaded courseModel:dataLoaded');
}

function outputError(e) {
//Allow plugin loading errors to output without stopping Adapt from loading
console.error(e);
}

function configureInview() {

var adaptConfig = Adapt.config.get("_inview");

var allowScrollOver = (adaptConfig && adaptConfig._allowScrollOver === false ? false : true);
Expand Down Expand Up @@ -230,5 +230,4 @@ require([

// Events that are triggered by the main Adapt content collections and models
Adapt.once('configModel:loadCourseData', onLoadCourseData);

});
92 changes: 92 additions & 0 deletions src/core/js/logging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
define(['core/js/adapt'], function(Adapt) {
var Logging = Backbone.Model.extend({
_config: {
_isEnabled: true,
_level: 'info', // Default log level
_console: true, // Log to console
},

// Used to determine if log call should be printed based on log level
_levels: {
debug: 0,
info: 1,
warn: 2,
error: 3,
fatal: 4
},

// Tracks if query string overrode config
_override: false,

initialize: function() {
// Override default log level with level present in query string
var matches = window.location.search.match(/[?&]loglevel=([a-z]*)/i);
if (matches && matches.length >= 2) {
var level = matches[1].toLowerCase();
if (this._levels[level] >= 0) {
this._config._level = level;
this._override = this._config._level;
this.debug('Loglevel override in query string:', this._config._level);
}
}

Adapt.once('configModel:loadCourseData', this.onLoadCourseData.bind(this));
},

onLoadCourseData: function() {
if (Adapt.config.has('_logging')) {
this._config = Adapt.config.get('_logging');
}

// Query string log level wins over the config
if (this._override) {
this._config._level = this._override;
}

this.debug('Logging config loaded');
this.trigger('log:ready');
},

debug: function() {
this._log('debug', Array.prototype.slice.call(arguments));
},

info: function() {
this._log('info', Array.prototype.slice.call(arguments));
},

warn: function() {
this._log('warn', Array.prototype.slice.call(arguments));
},

error: function() {
this._log('error', Array.prototype.slice.call(arguments));
},

fatal: function() {
this._log('fatal', Array.prototype.slice.call(arguments));
},

_log: function(level, data) {
if (!this._config._isEnabled) {
return;
}

if (this._levels[level] < this._levels[this._config._level]) {
return;
}

if (this._config._console) {
var log = [level.toUpperCase() + ':'];
data && log.push.apply(log, data);

console.log.apply(console, log);
}

// Allow error reporting plugins to hook and report to logging systems
this.trigger('log:' + level, data);
}
});

Adapt.log = new Logging();
});
16 changes: 15 additions & 1 deletion src/core/js/scriptLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,28 @@

//5. Load IE 8 shim
function loadShim() {

var isIE8 = (IE == 8);

Modernizr.load([
{
test: IE == 8,
test: isIE8,
yep: 'libraries/es5-shim.min.js',
nope: '',
complete: loadFoundationLibraries()
}
]);

if (isIE8) {
fixIE8ConsoleLog();
}

}

function fixIE8ConsoleLog() {

console.log = Function.prototype.call.bind(console.log, console);

}

//6. Load foundation libraries and templates
Expand Down
44 changes: 44 additions & 0 deletions src/core/schema/config.model.schema
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,50 @@
"validators": [],
"title": "Enforce route locking",
"help": "If menu locking is enabled, this setting prevents navigating to locked routes"
},
"_logging": {
"type": "object",
"title": "Logging",
"properties": {
"_isEnabled": {
"type": "bool",
"default": "true",
"required": true,
"inputType": {
"type": "Boolean",
"options": [true, false]
},
"validators": [],
"title": "Enable logging"
},
"_level": {
"type": "string",
"required": true,
"default" : "info",
"title" : "Log Level",
"validators": [],
"inputType": {
"type": "Select",
"options": [
"debug",
"info",
"warn",
"error",
"fatal"
]
}
},
"_console": {
"type": "bool",
"default": "true",
"inputType": {
"type": "Boolean",
"options": [true, false]
},
"validators": [],
"title": "Write to console"
}
}
}
}
}
5 changes: 5 additions & 0 deletions src/course/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"_defaultLanguage": "en",
"_defaultDirection": "ltr",
"_questionWeight": 1,
"_logging": {
"_isEnabled": true,
"_level": "debug",
"_console": true
},
"_accessibility": {
"_isEnabled": true,
"_isDisabledOnTouchDevices": false,
Expand Down