Improved log backend.

This commit is contained in:
Martin Edenhofer 2015-05-27 14:31:49 +02:00
parent f933523c8d
commit 11082ab8fb
8 changed files with 121 additions and 40 deletions

View file

@ -1,20 +1,20 @@
class Widget class Widget
constructor: -> constructor: ->
return if App.Config.get('developer_mode') return if !App.Config.get('developer_mode')
return if App.Log.config('banner') is false
banner = """ 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' ) App.Config.set( 'dev_banner', Widget, 'Widgets' )

View file

@ -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' )

View file

@ -22,24 +22,83 @@ class App.Log
_instance ?= new _Singleton _instance ?= new _Singleton
_instance.log( module, 'error', args ) _instance.log( module, 'error', args )
@config: ( type, regex ) ->
if _instance == undefined
_instance ?= new _Singleton
_instance.config( type, regex )
class _Singleton class _Singleton
constructor: -> constructor: ->
@config = {} @moduleColorsMap = {}
# @config['Collection'] = true @currentConfig = {}
# Session: true if window.localStorage
# ControllerForm: true 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 ) -> log: ( module, level, args ) ->
if !@config || level isnt 'debug' if level is 'debug'
@_log( module, level, args ) return if !@currentConfig.module && !@currentConfig.content
else if @config[ module ] 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 )
_log: ( module, level, args ) -> _log: ( module, level, args ) ->
if level is 'error' prefixLength = 28
console.error "App.#{module}(#{level})", args prefix = "App.#{module}(#{level})"
else if level is 'debug' if prefix.length < prefixLength
console.debug "App.#{module}(#{level})", args prefix += Array(prefixLength - prefix.length).join(' ')
else prefix += '|'
console.log "App.#{module}(#{level})", args 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

View file

@ -102,6 +102,7 @@ class _trackSingleton
) )
log: ( facility, level, args ) -> log: ( facility, level, args ) ->
return if !App.Config.get('developer_mode')
return if !App.Config.get('ui_send_client_stats') return if !App.Config.get('ui_send_client_stats')
info = info =
time: Math.round( new Date().getTime() / 1000 ) time: Math.round( new Date().getTime() / 1000 )
@ -112,6 +113,7 @@ class _trackSingleton
@data.push info @data.push info
send: (async = true) => send: (async = true) =>
return if !App.Config.get('developer_mode')
return if !App.Config.get('ui_send_client_stats') return if !App.Config.get('ui_send_client_stats')
return if _.isEmpty @data return if _.isEmpty @data
newData = _.clone( @data ) newData = _.clone( @data )

View file

@ -21,7 +21,7 @@ class App.Auth
) )
@loginCheck: -> @loginCheck: ->
App.Log.notice 'Auth', 'loginCheck' App.Log.debug 'Auth', 'loginCheck'
App.Ajax.request( App.Ajax.request(
id: 'login_check' id: 'login_check'
async: false async: false
@ -37,7 +37,7 @@ class App.Auth
) )
@logout: -> @logout: ->
App.Log.notice 'Auth', 'logout' App.Log.debug 'Auth', 'logout'
App.Ajax.request( App.Ajax.request(
id: 'logout' id: 'logout'
type: 'DELETE' type: 'DELETE'
@ -52,7 +52,7 @@ class App.Auth
) )
@_login: (data, type) -> @_login: (data, type) ->
App.Log.notice 'Auth', '_login:success', data App.Log.debug 'Auth', '_login:success', data
# if session is not valid # if session is not valid
if data.error if data.error
@ -129,7 +129,7 @@ class App.Auth
@_logout: (data) -> @_logout: (data) ->
App.Log.notice 'Auth', '_logout' App.Log.debug 'Auth', '_logout', data
# empty session # empty session
App.Session.init() App.Session.init()
@ -139,8 +139,8 @@ class App.Auth
App.Event.trigger( 'ui:rerender' ) App.Event.trigger( 'ui:rerender' )
App.Event.trigger( 'clearStore' ) App.Event.trigger( 'clearStore' )
@_loginError: (xhr, statusText, error) -> @_loginError: ->
App.Log.notice 'Auth', '_loginError:error' App.Log.error 'Auth', '_loginError:error'
# empty session # empty session
App.Session.init() App.Session.init()

View file

@ -29,7 +29,7 @@ class App.Browser
# define min. required browser version # define min. required browser version
map = map =
Chrome2: 37 Chrome: 37
Firefox: 31 Firefox: 31
Explorer: 10 Explorer: 10
Safari: 6 Safari: 6

View file

@ -54,7 +54,7 @@ class App.Content extends App.Controller
do (route, callback) => do (route, callback) =>
@route(route, (params) -> @route(route, (params) ->
@log 'notice', 'execute page controller', route, params @log 'debug', 'execute page controller', route, params
# remove events for page # remove events for page
App.Event.unbindLevel('page') App.Event.unbindLevel('page')

View file

@ -159,7 +159,7 @@ class _webSocketSingleton extends App.Controller
# check if ping is back within 2 min # check if ping is back within 2 min
App.Delay.clear 'websocket-ping-check', 'ws' App.Delay.clear 'websocket-ping-check', 'ws'
check = => check = =>
@log 'notice', 'no websocket ping response, reconnect...' @log 'debug', 'no websocket ping response, reconnect...'
@close() @close()
App.Delay.set check, 90000, 'websocket-ping-check', 'ws' App.Delay.set check, 90000, 'websocket-ping-check', 'ws'
@ -175,7 +175,7 @@ class _webSocketSingleton extends App.Controller
if !window.WebSocket if !window.WebSocket
@backend = 'ajax' @backend = 'ajax'
@log 'notice', 'no support of websocket, use ajax long polling' @log 'debug', 'no support of websocket, use ajax long polling'
@_ajaxInit() @_ajaxInit()
return return
@ -195,7 +195,7 @@ class _webSocketSingleton extends App.Controller
@ws.onopen = => @ws.onopen = =>
if @backend_port if @backend_port
port = ":#{@backend_port}" port = ":#{@backend_port}"
@log 'notice', "new websocket (#{@channel()}#{port}) connection open" @log 'debug', "new websocket (#{@channel()}#{port}) connection open"
@connectionEstablished = true @connectionEstablished = true
@connectionWasEstablished = true @connectionWasEstablished = true
@ -224,7 +224,7 @@ class _webSocketSingleton extends App.Controller
@_receiveMessage(pipe) @_receiveMessage(pipe)
@ws.onclose = (e) => @ws.onclose = (e) =>
@log 'notice', 'close websocket connection' @log 'debug', 'close websocket connection'
# take connection down and keep it down # take connection down and keep it down
return if @connectionKeepDown return if @connectionKeepDown
@ -237,7 +237,7 @@ class _webSocketSingleton extends App.Controller
# use ws dedicated port fallback if no connection was possible # use ws dedicated port fallback if no connection was possible
if @backend is 'websocket' if @backend is 'websocket'
@log 'notice', 'no websocket connection on /ws, use :port/' @log 'debug', 'no websocket connection on /ws, use :port/'
@backend = 'websocketPort' @backend = 'websocketPort'
@connect() @connect()
return return
@ -246,7 +246,7 @@ class _webSocketSingleton extends App.Controller
if @backend is 'websocketPort' if @backend is 'websocketPort'
if @backend_port if @backend_port
port = ":#{@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' @backend = 'ajax'
@connect() @connect()
return return
@ -317,7 +317,7 @@ class _webSocketSingleton extends App.Controller
queue: false queue: false
success: (data) => success: (data) =>
if data.client_id 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 @client_id = data.client_id
@_ajaxReceive() @_ajaxReceive()
@_ajaxSendQueue() @_ajaxSendQueue()
@ -369,7 +369,7 @@ class _webSocketSingleton extends App.Controller
data: JSON.stringify({ client_id: @client_id }) data: JSON.stringify({ client_id: @client_id })
processData: false processData: false
success: (data) => success: (data) =>
@log 'notice', 'ajax:onmessage', data @log 'debug', 'ajax:onmessage', data
@_receiveMessage(data) @_receiveMessage(data)
if data && data.error if data && data.error
@client_id = undefined @client_id = undefined