Created
October 18, 2012 15:44
-
-
Save mcculloughsean/3912670 to your computer and use it in GitHub Desktop.
process
This file contains 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
diff --git a/spec/lumber/transports/process-test.coffee b/spec/lumber/transports/process-test.coffee | |
new file mode 100644 | |
index 0000000..c60b0f9 | |
--- /dev/null | |
+++ b/spec/lumber/transports/process-test.coffee | |
@@ -0,0 +1,53 @@ | |
+fs = require "fs" | |
+path = require "path" | |
+mocha = require "mocha" | |
+assert = require("chai").assert | |
+dateFormat = require "dateformat" | |
+tk = require 'timekeeper' | |
+lumber = require "../../../src/lumber" | |
+ | |
+trans = logger = undefined | |
+mainLogFilePath = path.resolve 'app.log' | |
+describe "Process Transport", -> | |
+ before -> | |
+ trans = new lumber.transports.Process | |
+ command: [ __dirname + "/../../piper", mainLogFilePath ] | |
+ | |
+ it "has the correct defaults", -> | |
+ assert.instanceOf trans.encoder, lumber.encoders.Json | |
+ assert.isFunction trans.encoder.encode | |
+ assert.equal trans.level, "info" | |
+ | |
+ it "the correct functions", -> | |
+ assert.isFunction trans.log | |
+ | |
+ describe "functionally", -> | |
+ logResponse = undefined | |
+ before (done) -> | |
+ logger = new lumber.Logger(transports: [trans]) | |
+ logger.on "log", (err, msg, level, name, filename) -> | |
+ return unless msg? | |
+ logResponse = { msg, level, name } | |
+ setTimeout () -> | |
+ done err | |
+ , 1000 | |
+ logger.log "info", "A message" | |
+ | |
+ after -> | |
+ try | |
+ fs.unlinkSync mainLogFilePath | |
+ | |
+ it "creates the proper file", () -> | |
+ f = undefined | |
+ try | |
+ f = fs.statSync mainLogFilePath | |
+ assert.isTrue !!f | |
+ | |
+ it "passes the correct params", () -> | |
+ assert.equal logResponse.level, "info" | |
+ assert.equal logResponse.name, "process" | |
+ | |
+ it "writes properly enocoded data", () -> | |
+ assert.equal logResponse.msg.trim(), fs.readFileSync(mainLogFilePath, "utf8").trim() | |
+ | |
+ | |
diff --git a/spec/piper b/spec/piper | |
new file mode 100755 | |
index 0000000..b19b148 | |
--- /dev/null | |
+++ b/spec/piper | |
@@ -0,0 +1,15 @@ | |
+#!/usr/bin/env node | |
+ | |
+var fs = require('fs'); | |
+ | |
+var outputFile, outputStream; | |
+ | |
+if (process.argv[1] == __filename) | |
+ outputFile = process.argv[2]; | |
+ | |
+if (!outputFile) | |
+ throw "Must specify a filename"; | |
+ | |
+outputStream = fs.createWriteStream(outputFile); | |
+process.stdin.pipe(outputStream); | |
+ | |
diff --git a/spec/piper.js b/spec/piper.js | |
deleted file mode 100755 | |
index 6d5d626..0000000 | |
--- a/spec/piper.js | |
+++ /dev/null | |
@@ -1,16 +0,0 @@ | |
-#!/usr/bin/env node | |
- | |
-var fs = require('fs'); | |
- | |
-var outputFile, outputStream; | |
- | |
-if (process.argv[1] == __filename) | |
- outputFile = process.argv[2]; | |
- | |
-if (!outputFile) { | |
- throw "Must specify a filename"; | |
-} | |
- | |
-outputStream = fs.createWriteStream(outputFile); | |
-process.stdin.pipe(outputStream); | |
- | |
diff --git a/src/lumber/transports/process.coffee b/src/lumber/transports/process.coffee | |
new file mode 100644 | |
index 0000000..f34fb6b | |
--- /dev/null | |
+++ b/src/lumber/transports/process.coffee | |
@@ -0,0 +1,39 @@ | |
+util = require "util" | |
+cp = require "child_process" | |
+path = require "path" | |
+events = require "events" | |
+dateFormat = require "dateformat" | |
+lumber = require "../../lumber" | |
+ | |
+class Process extends events.EventEmitter | |
+ constructor: (options={}) -> | |
+ super() | |
+ | |
+ # Format [ "command", "list", "of", "args" ] | |
+ @command = lumber.util.checkOption options.command, ['tee', '/dev/null'] | |
+ @encoder = lumber.util.checkOption options.encoder, "json" | |
+ @level = lumber.util.checkOption options.level, "info" | |
+ @name = "process" | |
+ if typeof (@encoder) is "string" | |
+ e = lumber.util.titleCase(@encoder) | |
+ if lumber.encoders[e] | |
+ @encoder = new lumber.encoders[e]() | |
+ else | |
+ throw new Error("Unknown encoder passed: " + @encoder) | |
+ @encoding = @encoder.encoding | |
+ | |
+ command = @command.shift() | |
+ @_childProcess = cp.spawn command, @command | |
+ @_childProcess.stdout.pipe(process.stdout) | |
+ | |
+ log: (args, cb) -> | |
+ msg = @encoder.encode args.level, args.msg, args.meta | |
+ @_write msg + "\n", (err) => | |
+ cb err, msg, args.level, @name if cb | |
+ | |
+ | |
+ _write: (data, cb) -> | |
+ @_childProcess.stdin.write data, @encoding | |
+ cb null | |
+ | |
+module.exports = { Process } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment