logger.coffee | |
|---|---|
| Pow's | fs = require "fs"
{dirname} = require "path"
Log = require "log"
{mkdirp} = require "./util"
module.exports = class Logger |
| Log level method names that will be forwarded to the underlying
| @LEVELS: ["debug", "info", "notice", "warning", "error",
"critical", "alert", "emergency"] |
| Create a | constructor: (@path, @level = "debug") ->
@readyCallbacks = [] |
| Invoke | ready: (callback) ->
if @state is "ready"
callback.call @
else
@readyCallbacks.push callback
unless @state
@state = "initializing" |
| Make the log file's directory if it doesn't already exist. Reset the logger's state if an error is thrown. | mkdirp dirname(@path), (err) =>
if err
@state = null
else |
| Open a write stream for the log file and create the
underlying | @stream = fs.createWriteStream @path, flags: "a"
@stream.on "open", =>
@log = new Log @level, @stream
@state = "ready"
for callback in @readyCallbacks
callback.call @
@readyCallbacks = [] |
| Define the log level methods as wrappers around the corresponding
| for level in Logger.LEVELS then do (level) ->
Logger::[level] = (args...) ->
@ready -> @log[level].apply @log, args
|