Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## logger.coffee
- ## console.log for debug prints, filterable with flags in env.DEBUG.
- ###?//
- To do:
- 3. Cache parsed flags internally; since easily mapped to memoized generated functions, can make namespace full blown regexps (strings only, unless RE objects work as hash keys) over flags, so can support grouping, or whatever craziness.
- 4. Publish?!
- 5. Colors on TTY/logfiles problem?
- 6. Unit tests.
- 7. Levels: add optional methods for levels, and some syntax for specifying level per namespace. Use "standard" levels, ie Unix syslog.
- 8. Option to use different DEBUG delimiter than ','? Why?
- #?// Q&D implementation of API I like! Should really parse DEBUG and use
- #?// Missing "prefix:*" capability yet! Is grouping that important? Meh. Regexp even better!
- ###
- require 'colors'
- ## Logger.
- d=(f)->
- #?//exports.loggers[f]?=if exports.all or f of exports.flags
- #?//=(f,m,p...)->if debug.all or f of debug.flags then console.log f.blue+' '+m,p...
- unless d.loggers[f]?
- if (d.all or typeof f is 'string' and f of d.flags or typeof f is 'object' and 'test' of f and d.flags.find (f)->f.test f)
- f.replace '%','%%' # Don't parse format specifiers in flag.
- d.loggers[f]=(m,p...)->console.log f.blue+' '+m,p...
- else
- d.loggers[f]=->undefined # No-op.
- return d.loggers[f]
- ###?//
- d.loggers[f]?=if (d.all or typeof f is 'string' and f of d.flags or typeof f is 'object' and 'test' of f and d.flags.find (f)->f.test f)
- f.replace '%','%%' # Don't parse format specifiers in flag.
- (m,p...)->console.log f.blue+' '+m,p...
- else
- ->undefined # No-op.
- ###
- ## Set flags from string.
- d.init=(s,append=no)->
- unless append
- d.flags={}
- d.loggers={}
- for v in s.split ',' #?// .map (s)->s.trim(), and then .filter out ''. Or too foolproof?
- d.flags[v]=true
- d.all='*' of d.flags
- # Initialize with env immediately when required.
- d.init process.env.DEBUG or ''
- ###?// Much nicer if on Object's prototype!?
- compact=(o)->CSON.stringify(o).split('\n').join ',' #?// Shouldn't this remove indentation whitespace too?
- ###
- ## Export API: logger (anonymous function) and other APIs as properties set on it.
- module.exports=d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement