-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
68 additions
and
20 deletions.
There are no files selected for viewing
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
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); |
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
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); } | ||
}; |
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
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
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(); | ||
}); |