Skip to content
Browse files

Add and integrated standard logger

  • Loading branch information...
1 parent 5b7fc82 commit 24bed42cfe49d970b88846b45e79691167c70ae8 @skidding skidding committed Jul 19, 2013
View
3 conf/general.js
@@ -4,7 +4,8 @@ App.general = {
USE_MOCKS: false,
FRONTEND_URL: 'http://127.0.0.1:8000',
STATIC_URL: '/static/app',
- LOG_LEVEL: 1000, // Everything
+ LOGGER_MODULE: 'standard_logger',
+ LOG_LEVEL: 2, // ERROR & WARN
LOGIN_PAGE: '{{FRONTEND_URL}}/front/login',
LOGIN_URL: '{{FRONTEND_URL}}/front-api/v1/login/',
LOGOUT_URL: '{{FRONTEND_URL}}/front-api/v1/logout/',
View
4 conf/general.user.example.js
@@ -23,5 +23,5 @@ var LOG_LEVEL = {
App.user = {
FRONTEND_URL: FRONTEND.TEST,
STATIC_URL: 'http://ubervu.localhost',
- LOG_LEVEL: LOG_LEVEL.INFO
-};
+ LOG_LEVEL: LOG_LEVEL.WARN
+};
View
2 core/base_widgets/base_form.coffee
@@ -289,7 +289,7 @@ define ['cs!widget', 'cs!channels_utils'], (Widget, channels_utils) ->
if errors.errors?
@addFormErrors(errors.errors)
catch exception
- logger.error(exception)
+ logger.handleException(exception)
finally
@enableForm()
View
1 core/core_modules.js
@@ -9,6 +9,7 @@ App.main_modules = {
'loader': 'core/loader',
'logger': 'core/logging/server_logger',
'logger_store': 'core/logging/logger_store',
+ 'standard_logger': 'core/logging/standard_logger',
'mozaic_module': 'core/module',
'auth': 'core/auth',
'base_model': 'core/base_model',
View
62 core/logging/standard_logger.coffee
@@ -0,0 +1,62 @@
+define [], ->
+ class StandardLogger
+ ###
+ Default Logger class, prints logs to browser console directly, if
+ their level is the same or above the one it was configured to
+ ###
+ # Possible log levels for a logger, higher values include the ones
+ # lower than themselves, as well
+ @LOG_LEVEL:
+ NONE: 0
+ ERROR: 1
+ WARN: 2
+ INFO: 3
+
+ constructor: (logLevel) ->
+ # The log level of a logger instance can be set when creating it,
+ # otherwise it defaults to only logging warn or error messages
+ @logLevel = if logLevel? then logLevel \
+ else @constructor.LOG_LEVEL.WARN
+
+ error: ->
+ ###
+ Log a message at INFO level
+ ###
+ @_log(@constructor.LOG_LEVEL.ERROR, arguments...)
+
+ warn: ->
+ ###
+ Log a message at INFO level
+ ###
+ @_log(@constructor.LOG_LEVEL.WARN, arguments...)
+
+ info: ->
+ ###
+ Log a message at INFO level
+ ###
+ @_log(@constructor.LOG_LEVEL.INFO, arguments...)
+
+ handleException: (e, params...) ->
+ ###
+ Handle caught Error instance.
+
+ In this standard implementation the error message will simply
+ be logged at ERROR level. Exceptions should be left uncaught in
+ dev environment in order to intercept their stack trace
+ ###
+ # Allow extra params to be passed along an exception
+ @_log(@constructor.LOG_LEVEL.ERROR, e.toString(), params...)
+
+ _log: (logLevel, params...) ->
+ ###
+ Log a message to browser console, if its level meets the
+ current configuration
+ ###
+ return if logLevel > @logLevel
+ # Get the most appropriate console method based on the log level of
+ # this message
+ method = switch logLevel
+ when @constructor.LOG_LEVEL.ERROR then 'error'
+ when @constructor.LOG_LEVEL.WARN then 'warn'
+ else 'log'
+ console[method](params...)
View
3 mozaic.js
@@ -2,11 +2,12 @@ require([
'cs!utils/mozaic',
'cs!core/constants',
'cs!core/utils',
- 'cs!logger'
+ 'cs!' + App.general.LOGGER_MODULE
], function(MozaicLib, Constants, Utils, Logger) {
require(['cs!loader'], function() {
// Start Mozaic namespaces with core lib
window.Mozaic = _.extend({}, MozaicLib);
+ window.logger = new Logger(App.general.LOG_LEVEL);
loader.load_module("cs!router", // Class to load & instantiate
function(router) { // Run this when class was loaded and instantiated
Backbone.history.start();

0 comments on commit 24bed42

Please sign in to comment.
Something went wrong with that request. Please try again.