LEVELS =
trace: 10
information: 20
debug: 30
warning: 40
error: 50Template
(c) 2014 Alasdair Mercer
Freely distributable under the MIT license:
http://template-extension.org/license
Define the different logging levels privately first.
LEVELS =
trace: 10
information: 20
debug: 30
warning: 40
error: 50Ensure that all logs are sent to the background pages console.
{console} = chrome.extension.getBackgroundPage()Determine whether or not logging is enabled for the specified level.
loggable = (level) ->
log.config.enabled and level >= log.config.level
log = window.log = new class Log extends utils.ClassExpose the available logging levels.
TRACE: LEVELS.trace
INFORMATION: LEVELS.information
DEBUG: LEVELS.debug
WARNING: LEVELS.warning
ERROR: LEVELS.errorA collection of all of the levels to allow iteration.
LEVELS: ({name, value} for own name, value of LEVELS).sort (a, b) ->
a.value - b.valueHold the current conguration for the logger.
config:
enabled: no
level: LEVELS.debugCreate/increment a counter and output its current count for all names.
count: (names...) ->
console.count name for name in names if loggable @DEBUGOutput all debug entries.
debug: (entries...) ->
console.debug entry for entry in entries if loggable @DEBUGDisplay an interactive listing of the properties of all entries.
dir: (entries...) ->
console.dir entry for entry in entries if loggable @DEBUGOutput all error entries.
error: (entries...) ->
console.error entry for entry in entries if loggable @ERROROutput all informative entries.
info: (entries...) ->
console.info entry for entry in entries if loggable @INFORMATIONOutput all general entries.
out: (entries...) ->
console.log entry for entry in entries if @config.enabledStart a timer for all names.
time: (names...) ->
console.time name for name in names if loggable @DEBUGStop a timer and output its elapsed time in milliseconds for all names.
timeEnd: (names...) ->
console.timeEnd name for name in names if loggable @DEBUGOutput a stack trace.
trace: (caller = @trace) ->
console.log new @Trace(caller).stack if loggable @TRACEOutput all warning entries.
warn: (entries...) ->
console.warn entry for entry in entries if loggable @WARNINGTrace allows the current stack trace to be retrieved in the easiest way possible.
class log.Trace extends utils.ClassCreate a new instance of Trace for the caller.
constructor: (caller = log.Trace) ->Create the stack trace and assign it to a new stack property.
Error.captureStackTrace this, caller
@stack = @stack.replace /^Error/, 'Trace'Initialize logging.
if store?
store.init 'logger', {}
store.modify 'logger', (logger) ->
logger.enabled ?= no
logger.level ?= LEVELS.debug
log.config = logger