Skip to content

Commit

Permalink
feat: seclude AwsLambdaLogWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Apr 9, 2019
1 parent 261c7ff commit b6e64c0
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 20 deletions.
12 changes: 1 addition & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
"use strict";

const formatParts = require("sprintf-kit/format-parts")
, LogWriter = require("log/lib/writer")
, resolveParts = require("log-node/lib/resolve-format-parts");

class AwsLambdaLogWriter extends LogWriter {
constructor(options = {}) { super(process.env, options); }
resolveMessageContent(event) {
event.messageContent = formatParts(resolveParts(...event.messageTokens));
}
writeMessage(event) { console.error(event.message); }
}
const AwsLambdaLogWriter = require("./lib/writer");

module.exports = (options = {}) => new AwsLambdaLogWriter(options);
13 changes: 13 additions & 0 deletions lib/writer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

const formatParts = require("sprintf-kit/format-parts")
, LogWriter = require("log/lib/writer")
, resolveParts = require("log-node/lib/resolve-format-parts");

module.exports = class AwsLambdaLogWriter extends LogWriter {
constructor(options = {}) { super(process.env, options); }
resolveMessageContent(event) {
event.messageContent = formatParts(resolveParts(...event.messageTokens));
}
writeMessage(event) { console.error(event.message); }
};
15 changes: 6 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ const resolveUncached = () => {
return requireUncached(
[
require.resolve("log"), require.resolve("log/lib/private/logger-prototype"),
require.resolve("log/lib/emitter"), require.resolve("log/lib/register-master"),
require.resolve("log/lib/setup-visibility"), require.resolve("../")
require.resolve("log/lib/writer"), require.resolve("log/lib/emitter"),
require.resolve("log/lib/register-master"),
require.resolve("log/lib/setup-visibility"), require.resolve("../"),
require.resolve("../lib/writer")
],
() => ({ log: require("log"), initializeWriter: require("../") })
);
Expand All @@ -23,7 +25,6 @@ test("log-aws-lambda", t => {
const { log, initializeWriter } = resolveUncached();
initializeWriter();
const originalWrite = console.error;
let isInvoked = false;
console.error = string => {
t.equal(
string,
Expand All @@ -32,12 +33,8 @@ test("log-aws-lambda", t => {
} foo bar`,
"Should write logs for enabled loggers to stderr"
);
isInvoked = true;
console.error = originalWrite;
t.end();
};
log("not enabled");
t.equal(isInvoked, false, "Should not write logs of disabled loggers");
log.error.get("elo")("foo bar");
t.equal(isInvoked, true, "Should write logs immediately");
console.error = originalWrite;
t.end();
});
48 changes: 48 additions & 0 deletions test/lib/writer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use strict";

const test = require("tape")
, requireUncached = require("ncjsm/require-uncached")
, overrideEnv = require("process-utils/override-env");

const resolveUncached = () => {
const { restoreEnv } = overrideEnv();
try {
return requireUncached(
[
require.resolve("log"), require.resolve("log/lib/private/logger-prototype"),
require.resolve("log/lib/emitter"), require.resolve("log/lib/register-master"),
require.resolve("log/lib/writer"), require.resolve("log/lib/setup-visibility"),
require.resolve("../../lib/writer")
],
() => {
const LogWriter = require("../../lib/writer");
return { log: require("log"), initializeWriter: () => new LogWriter() };
}
);
} finally {
restoreEnv();
}
};

test("log-aws-lambda", t => {
const { log, initializeWriter } = resolveUncached();
initializeWriter();
const originalWrite = console.error;
let isInvoked = false;
console.error = string => {
t.equal(
string,
`${ log.error.get("elo").levelMessagePrefix } ${
log.error.get("elo").namespaceMessagePrefix
} foo bar`,
"Should write logs for enabled loggers to stderr"
);
isInvoked = true;
};
log("not enabled");
t.equal(isInvoked, false, "Should not write logs of disabled loggers");
log.error.get("elo")("foo bar");
t.equal(isInvoked, true, "Should write logs immediately");
console.error = originalWrite;
t.end();
});

0 comments on commit b6e64c0

Please sign in to comment.