Created
January 19, 2015 02:16
-
-
Save tylerFowler/1d18b24e9e7cbbfd1a4e to your computer and use it in GitHub Desktop.
Snippet for creating a custom logging system for NodeJS server applications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### Setup ### | |
| # src/coffee/server/utlity/logger.coffee | |
| colors = require 'colors' # npm install colors | |
| config = require './config' # optional custo config manager (loads from config.json) | |
| class Logger | |
| constructor: (@moduleName) -> | |
| getTimeString: -> | |
| time = new Date() | |
| return "#{time.getHours()}:#{time.getMinutes()}:#{time.getSeconds()}" | |
| error: (message, err) -> | |
| console.log "#{@moduleName} #{@.getTimeString()}".red + | |
| " | [ERROR]: #{message};\n\terr=#{err}".red | |
| warning: (message) -> | |
| console.log "#{@moduleName} #{@.getTimeString()} | [WARNING]: #{message}".yellow | |
| info: (message) -> | |
| unless config.app.log_level isnt 'verbose' | |
| console.log "#{@moduleName} #{@.getTimeString()} | [INFO]: #{message}".blue | |
| # this should act like a singleton for each module that uses it | |
| module.exports = (moduleName) -> | |
| moduleName ||= 'Unnamed' | |
| return new Logger moduleName | |
| ### Usage ### | |
| # src/coffee/server/app.coffee | |
| logger = require('./utiltity/logger') 'App' | |
| # do some setup for the app... | |
| # phew, everything went well | |
| logger.info 'Successfully created Express app' | |
| # check yourself before you wreck yourself | |
| logger.warn 'You want ants? Cause that\'s how you get ants.' | |
| # meltdown | |
| logger.error 'God, I said the cap on the poison pen slips off for no reason didn\'t I?', new Error 'Now he\'s fetching a rug. Happy, Cyril?!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment