From 11082ab8fb0deee6f9515cb11bfc57eff0d4c899 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 27 May 2015 14:31:49 +0200 Subject: [PATCH] Improved log backend. --- .../controllers/widget/dev_banner.js.coffee | 20 ++--- .../controllers/widget/hello_banner.js.coffee | 20 +++++ .../app/lib/app_init/log.js.coffee | 87 ++++++++++++++++--- .../app/lib/app_init/track.js.coffee | 2 + .../app/lib/app_post/auth.js.coffee | 12 +-- .../app/lib/app_post/browser.coffee | 2 +- .../lib/app_post/interface_handle.js.coffee | 2 +- .../app/lib/app_post/websocket.js.coffee | 16 ++-- 8 files changed, 121 insertions(+), 40 deletions(-) create mode 100644 app/assets/javascripts/app/controllers/widget/hello_banner.js.coffee diff --git a/app/assets/javascripts/app/controllers/widget/dev_banner.js.coffee b/app/assets/javascripts/app/controllers/widget/dev_banner.js.coffee index 6136c6787..8d26e7d00 100644 --- a/app/assets/javascripts/app/controllers/widget/dev_banner.js.coffee +++ b/app/assets/javascripts/app/controllers/widget/dev_banner.js.coffee @@ -1,20 +1,20 @@ class Widget constructor: -> - return if App.Config.get('developer_mode') + return if !App.Config.get('developer_mode') + return if App.Log.config('banner') is false banner = """ -| _____ _ -| / _ / __ _ _ __ ___ _ __ ___ __ _ __| | -| \\// / / _` | '_ ` _ \\| '_ ` _ \\ / _` |/ _` | -| / //\\ (_| | | | | | | | | | | | (_| | (_| | -| /____/\\__,_|_| |_| |_|_| |_| |_|\\__,_|\\__,_| | -| Hi there, nice to meet you! +| Welcome Zammad Developer! +| You can enable debugging by the following examples (value is a regex): | -| Visit %chttp://zammad.com/jobs%c to learn about our current job openings. +| App.Log.config('module', 'i18n|websocket') // enable debugging for i18n and websocket class +| App.Log.config('content', 'send') // enable debugging for messages which contains the string 'send' +| App.Log.config('banner', false) // disable this banner | -| Your Zammad Team! +| App.Log.config() // current settings +| App.Log.config('banner') // current setting for banner | """ - console.log(banner, "text-decoration: underline;", "text-decoration: none;") + console.log(banner) App.Config.set( 'dev_banner', Widget, 'Widgets' ) diff --git a/app/assets/javascripts/app/controllers/widget/hello_banner.js.coffee b/app/assets/javascripts/app/controllers/widget/hello_banner.js.coffee new file mode 100644 index 000000000..0d537b506 --- /dev/null +++ b/app/assets/javascripts/app/controllers/widget/hello_banner.js.coffee @@ -0,0 +1,20 @@ +class Widget + constructor: -> + return if App.Config.get('developer_mode') + banner = """ +| _____ _ +| / _ / __ _ _ __ ___ _ __ ___ __ _ __| | +| \\// / / _` | '_ ` _ \\| '_ ` _ \\ / _` |/ _` | +| / //\\ (_| | | | | | | | | | | | (_| | (_| | +| /____/\\__,_|_| |_| |_|_| |_| |_|\\__,_|\\__,_| +| +| Hi there, nice to meet you! +| +| Visit %chttp://zammad.com/jobs%c to learn about our current job openings. +| +| Your Zammad Team! +| +""" + console.log(banner, "text-decoration: underline;", "text-decoration: none;") + +App.Config.set( 'hello_banner', Widget, 'Widgets' ) diff --git a/app/assets/javascripts/app/lib/app_init/log.js.coffee b/app/assets/javascripts/app/lib/app_init/log.js.coffee index 24ba6b268..533858b02 100644 --- a/app/assets/javascripts/app/lib/app_init/log.js.coffee +++ b/app/assets/javascripts/app/lib/app_init/log.js.coffee @@ -22,24 +22,83 @@ class App.Log _instance ?= new _Singleton _instance.log( module, 'error', args ) + @config: ( type, regex ) -> + if _instance == undefined + _instance ?= new _Singleton + _instance.config( type, regex ) + class _Singleton constructor: -> - @config = {} -# @config['Collection'] = true -# Session: true -# ControllerForm: true + @moduleColorsMap = {} + @currentConfig = {} + if window.localStorage + raw = window.localStorage.getItem('log_config') + if raw + @currentConfig = JSON.parse(raw) + + # example config to enable debugging + #@config('module', 'i18n|websocket') + #@config('content', 'send') + + # detect color support + @colorSupport = false + data = App.Browser.detection() + if data && (data.browser is 'Chrome' || ( data.browser is 'Firefox' && data.version >= 31.0 ) ) + @colorSupport = true + + config: (type = undefined, value = undefined) -> + + # get + if value is undefined + if type + return @currentConfig[type] + return @currentConfig + + # set + if type is 'module' || type is 'content' + @currentConfig[type] = new RegExp(value, 'i') + else + @currentConfig[type] = value + if window.localStorage + window.localStorage.setItem('log_config', JSON.stringify(@currentConfig)) log: ( module, level, args ) -> - if !@config || level isnt 'debug' - @_log( module, level, args ) - else if @config[ module ] - @_log( module, level, args ) + if level is 'debug' + return if !@currentConfig.module && !@currentConfig.content + return if @currentConfig.module && !module.match(@currentConfig.module) + return if @currentConfig.content && !args.toString().match(@currentConfig.content) + @_log( module, level, args ) _log: ( module, level, args ) -> - if level is 'error' - console.error "App.#{module}(#{level})", args - else if level is 'debug' - console.debug "App.#{module}(#{level})", args - else - console.log "App.#{module}(#{level})", args + prefixLength = 28 + prefix = "App.#{module}(#{level})" + if prefix.length < prefixLength + prefix += Array(prefixLength - prefix.length).join(' ') + prefix += '|' + prefix = '%c' + prefix + if @colorSupport + if !@moduleColorsMap[module] + @moduleColorsMap[module]= @yieldColor() + color = @moduleColorsMap[module] + colorString = "color: hsl(" + (color) + ",99%,40%); font-weight: bold"; + logArgs = [prefix, colorString].concat(args) + else + logArgs = [prefix].concat(args) + + if level is 'error' + console.error.apply console, logArgs + else if level is 'debug' + console.debug.apply console, logArgs + else + console.log.apply console, logArgs + + # used inpirations from http://latentflip.com/bows/ + yieldColor: => + if !@hue + @hue = 0 + @hue += 1 + goldenRatio = 0.618033988749895 + @hue += goldenRatio + @hue = @hue % 1 + @hue * 360 \ No newline at end of file diff --git a/app/assets/javascripts/app/lib/app_init/track.js.coffee b/app/assets/javascripts/app/lib/app_init/track.js.coffee index fcad10c99..4ab84f716 100644 --- a/app/assets/javascripts/app/lib/app_init/track.js.coffee +++ b/app/assets/javascripts/app/lib/app_init/track.js.coffee @@ -102,6 +102,7 @@ class _trackSingleton ) log: ( facility, level, args ) -> + return if !App.Config.get('developer_mode') return if !App.Config.get('ui_send_client_stats') info = time: Math.round( new Date().getTime() / 1000 ) @@ -112,6 +113,7 @@ class _trackSingleton @data.push info send: (async = true) => + return if !App.Config.get('developer_mode') return if !App.Config.get('ui_send_client_stats') return if _.isEmpty @data newData = _.clone( @data ) diff --git a/app/assets/javascripts/app/lib/app_post/auth.js.coffee b/app/assets/javascripts/app/lib/app_post/auth.js.coffee index 5ffb3093c..6d4225960 100644 --- a/app/assets/javascripts/app/lib/app_post/auth.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/auth.js.coffee @@ -21,7 +21,7 @@ class App.Auth ) @loginCheck: -> - App.Log.notice 'Auth', 'loginCheck' + App.Log.debug 'Auth', 'loginCheck' App.Ajax.request( id: 'login_check' async: false @@ -37,7 +37,7 @@ class App.Auth ) @logout: -> - App.Log.notice 'Auth', 'logout' + App.Log.debug 'Auth', 'logout' App.Ajax.request( id: 'logout' type: 'DELETE' @@ -52,7 +52,7 @@ class App.Auth ) @_login: (data, type) -> - App.Log.notice 'Auth', '_login:success', data + App.Log.debug 'Auth', '_login:success', data # if session is not valid if data.error @@ -129,7 +129,7 @@ class App.Auth @_logout: (data) -> - App.Log.notice 'Auth', '_logout' + App.Log.debug 'Auth', '_logout', data # empty session App.Session.init() @@ -139,8 +139,8 @@ class App.Auth App.Event.trigger( 'ui:rerender' ) App.Event.trigger( 'clearStore' ) - @_loginError: (xhr, statusText, error) -> - App.Log.notice 'Auth', '_loginError:error' + @_loginError: -> + App.Log.error 'Auth', '_loginError:error' # empty session App.Session.init() diff --git a/app/assets/javascripts/app/lib/app_post/browser.coffee b/app/assets/javascripts/app/lib/app_post/browser.coffee index d9dc3304b..880aa528f 100644 --- a/app/assets/javascripts/app/lib/app_post/browser.coffee +++ b/app/assets/javascripts/app/lib/app_post/browser.coffee @@ -29,7 +29,7 @@ class App.Browser # define min. required browser version map = - Chrome2: 37 + Chrome: 37 Firefox: 31 Explorer: 10 Safari: 6 diff --git a/app/assets/javascripts/app/lib/app_post/interface_handle.js.coffee b/app/assets/javascripts/app/lib/app_post/interface_handle.js.coffee index 23df47a44..8ba574f06 100644 --- a/app/assets/javascripts/app/lib/app_post/interface_handle.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/interface_handle.js.coffee @@ -54,7 +54,7 @@ class App.Content extends App.Controller do (route, callback) => @route(route, (params) -> - @log 'notice', 'execute page controller', route, params + @log 'debug', 'execute page controller', route, params # remove events for page App.Event.unbindLevel('page') diff --git a/app/assets/javascripts/app/lib/app_post/websocket.js.coffee b/app/assets/javascripts/app/lib/app_post/websocket.js.coffee index 3e0f4fe53..907907f73 100644 --- a/app/assets/javascripts/app/lib/app_post/websocket.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/websocket.js.coffee @@ -159,7 +159,7 @@ class _webSocketSingleton extends App.Controller # check if ping is back within 2 min App.Delay.clear 'websocket-ping-check', 'ws' check = => - @log 'notice', 'no websocket ping response, reconnect...' + @log 'debug', 'no websocket ping response, reconnect...' @close() App.Delay.set check, 90000, 'websocket-ping-check', 'ws' @@ -175,7 +175,7 @@ class _webSocketSingleton extends App.Controller if !window.WebSocket @backend = 'ajax' - @log 'notice', 'no support of websocket, use ajax long polling' + @log 'debug', 'no support of websocket, use ajax long polling' @_ajaxInit() return @@ -195,7 +195,7 @@ class _webSocketSingleton extends App.Controller @ws.onopen = => if @backend_port port = ":#{@backend_port}" - @log 'notice', "new websocket (#{@channel()}#{port}) connection open" + @log 'debug', "new websocket (#{@channel()}#{port}) connection open" @connectionEstablished = true @connectionWasEstablished = true @@ -224,7 +224,7 @@ class _webSocketSingleton extends App.Controller @_receiveMessage(pipe) @ws.onclose = (e) => - @log 'notice', 'close websocket connection' + @log 'debug', 'close websocket connection' # take connection down and keep it down return if @connectionKeepDown @@ -237,7 +237,7 @@ class _webSocketSingleton extends App.Controller # use ws dedicated port fallback if no connection was possible if @backend is 'websocket' - @log 'notice', 'no websocket connection on /ws, use :port/' + @log 'debug', 'no websocket connection on /ws, use :port/' @backend = 'websocketPort' @connect() return @@ -246,7 +246,7 @@ class _webSocketSingleton extends App.Controller if @backend is 'websocketPort' if @backend_port port = ":#{@backend_port}" - @log 'notice', "no websocket connection on port #{port}, use ajax long polling as fallback" + @log 'debug', "no websocket connection on port #{port}, use ajax long polling as fallback" @backend = 'ajax' @connect() return @@ -317,7 +317,7 @@ class _webSocketSingleton extends App.Controller queue: false success: (data) => if data.client_id - @log 'notice', 'ajax:new client_id', data.client_id + @log 'debug', 'ajax:new client_id', data.client_id @client_id = data.client_id @_ajaxReceive() @_ajaxSendQueue() @@ -369,7 +369,7 @@ class _webSocketSingleton extends App.Controller data: JSON.stringify({ client_id: @client_id }) processData: false success: (data) => - @log 'notice', 'ajax:onmessage', data + @log 'debug', 'ajax:onmessage', data @_receiveMessage(data) if data && data.error @client_id = undefined